From aa5e213f25cd0150c0de4ac914e26039f61449ea Mon Sep 17 00:00:00 2001 From: August Johnson Date: Wed, 23 Oct 2024 08:29:35 -0500 Subject: [PATCH 1/4] Adding new method to gamecontent. --- .../0002_document_v2_related_key.py | 18 ++++++ api/models/models.py | 55 ++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 api/migrations/0002_document_v2_related_key.py diff --git a/api/migrations/0002_document_v2_related_key.py b/api/migrations/0002_document_v2_related_key.py new file mode 100644 index 00000000..bbe95e76 --- /dev/null +++ b/api/migrations/0002_document_v2_related_key.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.2 on 2024-10-23 13:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='document', + name='v2_related_key', + field=models.TextField(help_text='Key mapping for v2 document.', null=True), + ), + ] diff --git a/api/models/models.py b/api/models/models.py index d696151c..cd05d10b 100644 --- a/api/models/models.py +++ b/api/models/models.py @@ -2,7 +2,7 @@ import uuid from django.db import models - +from django.template.defaultfilters import slugify class Manifest(models.Model): """A Manifest contains a hash based on the contents of a file. @@ -57,6 +57,11 @@ class Document(models.Model): default="http://open5e.com/legal", help_text='URL reference for the license.') + v2_related_key = models.TextField( + null=True, + help_text='Key mapping for v2 document.' + ) + @staticmethod def plural_str() -> str: """Return a string specifying the plural name of this model.""" @@ -97,6 +102,54 @@ def document__license_url(self): def document__url(self): return self.document.url + def v2_converted_path(self): + # Returns a text string that is the theoretical v2 object url related + # to a given object. Does not guarantee that an object exists at that + # url. + + url_lookup = { #Looks up v1 object type to relevant v2 url. + "Spell":"v2/spells", + "Monster":"v2/creatures", + "Background":"v2/backgrounds", + "Plane":"v2/environments", + "Section":"v2/rules", + "Feat":"v2/feats", + "Condition":"v2/conditions", + "Race":"v2/races", + "CharClass":"v2/classes", + "MagicItem":"v2/items", + "Weapon":"v2/items", + "Armor":"v2/items" + } + + exclude_doc_key = ['Condition'] + + a5e_doc_lookup = { + "Monster":"mmenag", + "MagicItem":"a5e-ddg", + "Spell":"a5e-ag", + "Background":"a5e-ag", + "Plane":"a5e-ag", + "Section":"a5e-ag", + "Feat":"a5e-ag", + "Condition":"a5e-ag", + "Race":"a5e-ag", + "CharClass":"a5e-ag", + "Weapon":"a5e-ag", + "Armor":"a5e-ag" + } + + resource = url_lookup[self.__class__.__name__] + v2_document = self.document.v2_related_key + if v2_document=="a5e": + v2_document = a5e_doc_lookup[self.__class__.__name__] + converted_name=slugify(self.name) + + if self.__class__.__name__ in exclude_doc_key: + return f"{resource}/{converted_name}" + return f"{resource}/{v2_document}_{converted_name}" + + class Meta: abstract = True From 97a7149c3cf2fb71772cb2da96ff86790e50a6f3 Mon Sep 17 00:00:00 2001 From: August Johnson Date: Wed, 23 Oct 2024 08:35:36 -0500 Subject: [PATCH 2/4] Ref to v2 related documents. --- data/v1/a5e/Document.json | 3 ++- data/v1/blackflag/Document.json | 3 ++- data/v1/cc/Document.json | 3 ++- data/v1/dmag-e/Document.json | 3 ++- data/v1/dmag/Document.json | 3 ++- data/v1/kp/Document.json | 3 ++- data/v1/menagerie/Document.json | 3 ++- data/v1/o5e/Document.json | 3 ++- data/v1/taldorei/Document.json | 3 ++- data/v1/tob-2023/Document.json | 3 ++- data/v1/tob/Document.json | 3 ++- data/v1/tob2/Document.json | 3 ++- data/v1/tob3/Document.json | 3 ++- data/v1/toh/Document.json | 3 ++- data/v1/vom/Document.json | 3 ++- data/v1/warlock/Document.json | 3 ++- data/v1/wotc-srd/Document.json | 3 ++- 17 files changed, 34 insertions(+), 17 deletions(-) diff --git a/data/v1/a5e/Document.json b/data/v1/a5e/Document.json index 4f31c453..c0f446a9 100644 --- a/data/v1/a5e/Document.json +++ b/data/v1/a5e/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://a5esrd.com/a5esrd", "copyright": "This work includes material taken from the A5E System Reference Document (A5ESRD) by EN Publishing and available at A5ESRD.com, based on Level Up: Advanced 5th Edition, available at www.levelup5e.com. The A5ESRD is licensed under the Creative Commons Attribution 4.0 International License available at https://creativecommons.org/licenses/by/4.0/legalcode.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "a5e" } } ] diff --git a/data/v1/blackflag/Document.json b/data/v1/blackflag/Document.json index b8781799..b81ca969 100644 --- a/data/v1/blackflag/Document.json +++ b/data/v1/blackflag/Document.json @@ -12,7 +12,8 @@ "version": "0.2", "url": "https://koboldpress.com/black-flag-reference-document/", "copyright": "ORC NOTICE: This product is licensed under the ORC License, located at the Library of Congress at TX 9-307-067, and available online at various locations including koboldpress.com/orclicense and others. All warranties are disclaimed as set forth therein.", - "license_url": "https://paizo.com/orclicense" + "license_url": "https://paizo.com/orclicense", + "v2_related_key": "bfrd" } } ] diff --git a/data/v1/cc/Document.json b/data/v1/cc/Document.json index 350d3a08..d1f10097 100644 --- a/data/v1/cc/Document.json +++ b/data/v1/cc/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product/creature-codex-for-5th-edition-dnd/", "copyright": "Creature Codex. © 2018 Open Design LLC; Authors Wolfgang Baur, Dan Dillon, Richard Green, James Haeck, Chris Harris, Jeremy Hochhalter, James Introcaso, Chris Lockey, Shawn Merwin, and Jon Sawatsky.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "ccdx" } } ] diff --git a/data/v1/dmag-e/Document.json b/data/v1/dmag-e/Document.json index 2a54ab5a..977826bb 100644 --- a/data/v1/dmag-e/Document.json +++ b/data/v1/dmag-e/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com", "copyright": "© Open Design LLC", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "deepmx" } } ] diff --git a/data/v1/dmag/Document.json b/data/v1/dmag/Document.json index 3bd30fea..79389969 100644 --- a/data/v1/dmag/Document.json +++ b/data/v1/dmag/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product/deep-magic-for-5th-edition-hardcover/", "copyright": "Deep Magic for 5th Edition © 2020 Open Design LLC; Authors: Dan Dillon, Chris Harris, and Jeff Lee.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "deepm" } } ] diff --git a/data/v1/kp/Document.json b/data/v1/kp/Document.json index 1140533b..464521ec 100644 --- a/data/v1/kp/Document.json +++ b/data/v1/kp/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com", "copyright": "© Open Design LLC", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "kp" } } ] diff --git a/data/v1/menagerie/Document.json b/data/v1/menagerie/Document.json index 7a74b3e8..199de714 100644 --- a/data/v1/menagerie/Document.json +++ b/data/v1/menagerie/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://www.levelup5e.com", "copyright": "Level Up: Advanced 5th Edition Monstrous Menagerie. Copyright 2021, EN Publishing.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "mmenag" } } ] diff --git a/data/v1/o5e/Document.json b/data/v1/o5e/Document.json index 6250b15b..255df63f 100644 --- a/data/v1/o5e/Document.json +++ b/data/v1/o5e/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "open5e.com", "copyright": "Open5e.com Copyright 2019.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "open5e" } } ] diff --git a/data/v1/taldorei/Document.json b/data/v1/taldorei/Document.json index 7dbeb48c..0da3dbf2 100644 --- a/data/v1/taldorei/Document.json +++ b/data/v1/taldorei/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://https://greenronin.com/blog/2017/09/25/ronin-round-table-integrating-wizards-5e-adventures-with-the-taldorei-campaign-setting/", "copyright": "Critical Role: Tal’Dorei Campaign Setting is ©2017 Green Ronin Publishing, LLC. All rights reserved.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "tdcs" } } ] diff --git a/data/v1/tob-2023/Document.json b/data/v1/tob-2023/Document.json index 580e7e25..96f52133 100644 --- a/data/v1/tob-2023/Document.json +++ b/data/v1/tob-2023/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product/tome-of-beasts-1-2023-edition/", "copyright": "Tome of Beasts. 2023 Open Design LLC. All rights reserved.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "tob-2023" } } ] diff --git a/data/v1/tob/Document.json b/data/v1/tob/Document.json index 52cf3546..052bc027 100644 --- a/data/v1/tob/Document.json +++ b/data/v1/tob/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product/tome-of-beasts-for-5th-edition-print/", "copyright": "Tome of Beasts. Copyright 2016, Open Design; Authors Chris Harris, Dan Dillon, Rodrigo Garcia Carmona, and Wolfgang Baur.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "tob" } } ] diff --git a/data/v1/tob2/Document.json b/data/v1/tob2/Document.json index c713ff94..8cec32fe 100644 --- a/data/v1/tob2/Document.json +++ b/data/v1/tob2/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product/tome-of-beasts-2-for-5th-edition/", "copyright": "Tome of Beasts 2. Copyright 2020 Open Design LLC; Authors Wolfgang Baur, Celeste Conowitch, Darrin Drader, James Introcaso, Philip Larwood, Jeff Lee, Kelly Pawlik, Brian Suskind, Mike Welham.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "tob2" } } ] diff --git a/data/v1/tob3/Document.json b/data/v1/tob3/Document.json index 95f390d5..c185bc7a 100644 --- a/data/v1/tob3/Document.json +++ b/data/v1/tob3/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product/tome-of-beasts-3-for-5th-edition/", "copyright": "Tome of Beasts 3. ©2022 Open Design LLC. All rights reserved.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "tob3" } } ] diff --git a/data/v1/toh/Document.json b/data/v1/toh/Document.json index 7e8a9263..20c06bf6 100644 --- a/data/v1/toh/Document.json +++ b/data/v1/toh/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product/tome-of-heroes-for-5th-edition/", "copyright": "Tome of Heroes. Copyright 2022, Open Design; Authors Kelly Pawlik, Ben Mcfarland, and Briand Suskind.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "toh" } } ] diff --git a/data/v1/vom/Document.json b/data/v1/vom/Document.json index 1eb95aa6..0fee4568 100644 --- a/data/v1/vom/Document.json +++ b/data/v1/vom/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product/vault-of-magic-for-5th-edition/", "copyright": "Vault of Magic. Copyright 2021 Open Design LLC; Authors Phillip Larwood, Jeff Lee, and Christopher Lockey.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "vom" } } ] diff --git a/data/v1/warlock/Document.json b/data/v1/warlock/Document.json index 24db0a1c..1ea6752a 100644 --- a/data/v1/warlock/Document.json +++ b/data/v1/warlock/Document.json @@ -12,7 +12,8 @@ "version": "1.0", "url": "https://koboldpress.com/kpstore/product-category/all-products/warlock-5th-edition-dnd/", "copyright": "© Open Design LLC", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "wz" } } ] diff --git a/data/v1/wotc-srd/Document.json b/data/v1/wotc-srd/Document.json index e1d3c6cc..886baf3f 100644 --- a/data/v1/wotc-srd/Document.json +++ b/data/v1/wotc-srd/Document.json @@ -12,7 +12,8 @@ "version": "5.1", "url": "http://dnd.wizards.com/articles/features/systems-reference-document-srd", "copyright": "System Reference Document 5.1 Copyright 2016, Wizards of the Coast, Inc.; Authors Mike Mearls, Jeremy Crawford, Chris Perkins, Rodney Thompson, Peter Lee, James Wyatt, Robert J. Schwalb, Bruce R. Cordell, Chris Sims, and Steve Townshend, based on original material by E. Gary Gygax and Dave Arneson.", - "license_url": "http://open5e.com/legal" + "license_url": "http://open5e.com/legal", + "v2_related_key": "srd" } } ] From 71adcd873c38581c923450463bf9bcb0d2b6a6db Mon Sep 17 00:00:00 2001 From: August Johnson Date: Wed, 23 Oct 2024 09:49:44 -0500 Subject: [PATCH 3/4] returns a url. --- api/models/models.py | 33 ++++++++++++++++++--------------- api/serializers.py | 10 ++++++++-- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/api/models/models.py b/api/models/models.py index cd05d10b..024b55bc 100644 --- a/api/models/models.py +++ b/api/models/models.py @@ -2,7 +2,10 @@ import uuid from django.db import models +from django.urls import reverse +from django.shortcuts import redirect from django.template.defaultfilters import slugify +#from api_v2 import urls as urls_v2 class Manifest(models.Model): """A Manifest contains a hash based on the contents of a file. @@ -108,18 +111,18 @@ def v2_converted_path(self): # url. url_lookup = { #Looks up v1 object type to relevant v2 url. - "Spell":"v2/spells", - "Monster":"v2/creatures", - "Background":"v2/backgrounds", - "Plane":"v2/environments", - "Section":"v2/rules", - "Feat":"v2/feats", - "Condition":"v2/conditions", - "Race":"v2/races", - "CharClass":"v2/classes", - "MagicItem":"v2/items", - "Weapon":"v2/items", - "Armor":"v2/items" + "Spell":"spell", + "Monster":"creature", + "Background":"background", + "Plane":"environments", + "Section":"rule", + "Feat":"feat", + "Condition":"condition", + "Race":"race", + "CharClass":"class", + "MagicItem":"item", + "Weapon":"item", + "Armor":"item" } exclude_doc_key = ['Condition'] @@ -139,15 +142,15 @@ def v2_converted_path(self): "Armor":"a5e-ag" } - resource = url_lookup[self.__class__.__name__] + resource = url_lookup[self.__class__.__name__]+"-detail" v2_document = self.document.v2_related_key if v2_document=="a5e": v2_document = a5e_doc_lookup[self.__class__.__name__] converted_name=slugify(self.name) if self.__class__.__name__ in exclude_doc_key: - return f"{resource}/{converted_name}" - return f"{resource}/{v2_document}_{converted_name}" + return reverse(resource,kwargs={'pk':f"{converted_name}"}) + return redirect(reverse(resource,kwargs={'pk':f"{v2_document}_{converted_name}"})).url class Meta: diff --git a/api/serializers.py b/api/serializers.py index 50e09e90..9510e4fc 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -54,7 +54,8 @@ class Meta: 'organization', 'version', 'copyright', - 'license_url',) + 'license_url', + 'v2_related_key') class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: @@ -72,6 +73,7 @@ class MonsterSerializer(DynamicFieldsHyperlinkedModelSerializer): legendary_actions = serializers.SerializerMethodField() special_abilities = serializers.SerializerMethodField() img_main = serializers.SerializerMethodField() + v2_converted_path = serializers.SerializerMethodField() def get_img_main(self, monster): request = self.context.get('request') @@ -106,6 +108,9 @@ def get_legendary_actions(self, monster): def get_special_abilities(self, monster): return monster.special_abilities() + def get_v2_converted_path(self, monster): + return monster.v2_converted_path() + class Meta: model = models.Monster @@ -158,7 +163,8 @@ class Meta: 'document__slug', 'document__title', 'document__license_url', - 'document__url' + 'document__url', + 'v2_converted_path' ) class SpellSerializer(DynamicFieldsModelSerializer): From 5a7321aa9f5cab70ed17a52f2f73ec683b36563d Mon Sep 17 00:00:00 2001 From: August Johnson Date: Wed, 23 Oct 2024 09:50:34 -0500 Subject: [PATCH 4/4] Resolving tests. --- .../TestAPIRoot.test_documents.approved.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/api/tests/approved_files/TestAPIRoot.test_documents.approved.json b/api/tests/approved_files/TestAPIRoot.test_documents.approved.json index 3bd92e4f..4dd9f404 100644 --- a/api/tests/approved_files/TestAPIRoot.test_documents.approved.json +++ b/api/tests/approved_files/TestAPIRoot.test_documents.approved.json @@ -13,6 +13,7 @@ "slug": "o5e", "title": "Open5e Original Content", "url": "open5e.com", + "v2_related_key": "open5e", "version": "1.0" }, { @@ -25,6 +26,7 @@ "slug": "wotc-srd", "title": "5e Core Rules", "url": "http://dnd.wizards.com/articles/features/systems-reference-document-srd", + "v2_related_key": "srd", "version": "5.1" }, { @@ -37,6 +39,7 @@ "slug": "tob", "title": "Tome of Beasts", "url": "https://koboldpress.com/kpstore/product/tome-of-beasts-for-5th-edition-print/", + "v2_related_key": "tob", "version": "1.0" }, { @@ -49,6 +52,7 @@ "slug": "cc", "title": "Creature Codex", "url": "https://koboldpress.com/kpstore/product/creature-codex-for-5th-edition-dnd/", + "v2_related_key": "ccdx", "version": "1.0" }, { @@ -61,6 +65,7 @@ "slug": "tob2", "title": "Tome of Beasts 2", "url": "https://koboldpress.com/kpstore/product/tome-of-beasts-2-for-5th-edition/", + "v2_related_key": "tob2", "version": "1.0" }, { @@ -73,6 +78,7 @@ "slug": "dmag", "title": "Deep Magic 5e", "url": "https://koboldpress.com/kpstore/product/deep-magic-for-5th-edition-hardcover/", + "v2_related_key": "deepm", "version": "1.0" }, { @@ -85,6 +91,7 @@ "slug": "menagerie", "title": "Level Up Advanced 5e Monstrous Menagerie", "url": "https://www.levelup5e.com", + "v2_related_key": "mmenag", "version": "1.0" }, { @@ -97,6 +104,7 @@ "slug": "tob3", "title": "Tome of Beasts 3", "url": "https://koboldpress.com/kpstore/product/tome-of-beasts-3-for-5th-edition/", + "v2_related_key": "tob3", "version": "1.0" }, { @@ -109,6 +117,7 @@ "slug": "a5e", "title": "Level Up Advanced 5e", "url": "https://a5esrd.com/a5esrd", + "v2_related_key": "a5e", "version": "1.0" }, { @@ -121,6 +130,7 @@ "slug": "kp", "title": "Kobold Press Compilation", "url": "https://koboldpress.com", + "v2_related_key": "kp", "version": "1.0" }, { @@ -133,6 +143,7 @@ "slug": "dmag-e", "title": "Deep Magic Extended", "url": "https://koboldpress.com", + "v2_related_key": "deepmx", "version": "1.0" }, { @@ -145,6 +156,7 @@ "slug": "warlock", "title": "Warlock Archives", "url": "https://koboldpress.com/kpstore/product-category/all-products/warlock-5th-edition-dnd/", + "v2_related_key": "wz", "version": "1.0" }, { @@ -157,6 +169,7 @@ "slug": "vom", "title": "Vault of Magic", "url": "https://koboldpress.com/kpstore/product/vault-of-magic-for-5th-edition/", + "v2_related_key": "vom", "version": "1.0" }, { @@ -169,6 +182,7 @@ "slug": "toh", "title": "Tome of Heroes", "url": "https://koboldpress.com/kpstore/product/tome-of-heroes-for-5th-edition/", + "v2_related_key": "toh", "version": "1.0" }, { @@ -181,6 +195,7 @@ "slug": "taldorei", "title": "Critical Role: Tal\u2019Dorei Campaign Setting", "url": "https://https://greenronin.com/blog/2017/09/25/ronin-round-table-integrating-wizards-5e-adventures-with-the-taldorei-campaign-setting/", + "v2_related_key": "tdcs", "version": "1.0" }, { @@ -193,6 +208,7 @@ "slug": "blackflag", "title": "Black Flag SRD", "url": "https://koboldpress.com/black-flag-reference-document/", + "v2_related_key": "bfrd", "version": "0.2" }, { @@ -205,6 +221,7 @@ "slug": "tob-2023", "title": "Tome of Beasts 2023", "url": "https://koboldpress.com/kpstore/product/tome-of-beasts-1-2023-edition/", + "v2_related_key": "tob-2023", "version": "1.0" } ]