Skip to content

Commit

Permalink
Merge pull request #302 from open5e/api_v2_items
Browse files Browse the repository at this point in the history
Api v2 items
  • Loading branch information
augustjohnson authored Aug 7, 2023
2 parents c042ff0 + aa97c7f commit cb86c1c
Show file tree
Hide file tree
Showing 34 changed files with 35,350 additions and 1,490 deletions.
10 changes: 9 additions & 1 deletion api/management/commands/quicksetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ def handle(self, *args, **options):
self.stdout.write('Collecting static files...')
collect_static()

self.stdout.write('Populating the database...')
self.stdout.write('Populating the v1 database...')
quickload.populate_db()

self.stdout.write('Populating the v2 database...')
import_v2()

if options["noindex"]:
self.stdout.write('Skipping search index rebuild due to --noindex...')
else:
Expand All @@ -39,6 +42,11 @@ def handle(self, *args, **options):
self.stdout.write(self.style.SUCCESS('API setup complete.'))


def import_v2() -> None:
"""Import the v2 apps' database models."""
call_command('import', '--dir', 'data/v2')


def migrate_db() -> None:
"""Migrate the local database as needed to incorporate new model updates."""
call_command('makemigrations')
Expand Down
6 changes: 3 additions & 3 deletions api/tests/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,9 @@ def test_get_monster_data(self):
self.assertEqual(
in_monster['stealth'],
out_monster['skills']['stealth']) # MISALIGNED
self.assertEqual("", out_monster['reactions']) # Empty string?
self.assertEqual("", out_monster['legendary_desc']) # Empty string?
self.assertEqual("", out_monster['legendary_actions']) # Empty string?
#self.assertEqual("", out_monster['reactions']) # Empty string? - Fixed with #309
#self.assertEqual("", out_monster['legendary_desc']) # Empty string? - Fixed with #309
#self.assertEqual("", out_monster['legendary_actions']) # Empty string? - Fixed with #309
self.assertEqual(
in_monster['special_abilities'][0]['name'],
out_monster['special_abilities'][0]['name'])
Expand Down
8 changes: 6 additions & 2 deletions api_v2/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from api_v2.models import Weapon
from api_v2.models import Armor
from api_v2.models import Item
from api_v2.models import Item, ItemSet

from api_v2.models import Document
from api_v2.models import License
Expand All @@ -16,10 +16,14 @@ class FromDocumentModelAdmin(admin.ModelAdmin):
list_display = ['key', '__str__']


class ItemModelAdmin(admin.ModelAdmin):
list_display = ['key','category','name']

admin.site.register(Weapon, admin_class=FromDocumentModelAdmin)
admin.site.register(Armor, admin_class=FromDocumentModelAdmin)

admin.site.register(Item, admin_class=FromDocumentModelAdmin)
admin.site.register(Item, admin_class=ItemModelAdmin)
admin.site.register(ItemSet, admin_class=FromDocumentModelAdmin)

admin.site.register(Document)
admin.site.register(License)
Expand Down
92 changes: 0 additions & 92 deletions api_v2/management/commands/dumpbyorg.py

This file was deleted.

128 changes: 128 additions & 0 deletions api_v2/management/commands/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@


import os
import json
import time

from django.core.management import call_command
from django.core.management.base import BaseCommand

from django.core import serializers

from django.apps import apps
from django.apps import AppConfig

from api_v2.models import *


class Command(BaseCommand):
"""Implementation for the `manage.py `export` subcommand."""

help = 'Export all v2 model data in structured directory.'

def add_arguments(self, parser):
parser.add_argument("-d",
"--dir",
type=str,
help="Directory to write files to.")

def handle(self, *args, **options) -> None:
self.stdout.write('Checking if directory exists.')
if os.path.exists(options['dir']) and os.path.isdir(options['dir']):
self.stdout.write('Directory {} exists.'.format(options['dir']))
else:
self.stdout.write(self.style.ERROR(
'Directory {} does not exist.'.format(options['dir'])))
exit(0)

# Start V2 output.
rulesets = Ruleset.objects.all()
ruleset_path = get_filepath_by_model(
'Ruleset',
'api_v2',
base_path=options['dir'])
write_queryset_data(ruleset_path, rulesets)

license_path = get_filepath_by_model(
'License',
'api_v2',
base_path=options['dir'])
licenses = License.objects.all()
write_queryset_data(license_path, licenses)

# Create a folder and Publisher fixture for each pubishing org.
for pub in Publisher.objects.order_by('key'):
pubq = Publisher.objects.filter(key=pub.key).order_by('pk')
pub_path = get_filepath_by_model(
"Publisher",
"api_v2",
pub_key=pub.key,
base_path=options['dir'])
write_queryset_data(pub_path, pubq)

# Create a Document fixture for each document.
for doc in Document.objects.filter(publisher=pub):
docq = Document.objects.filter(key=doc.key).order_by('pk')
doc_path = get_filepath_by_model(
"Document",
"api_v2",
pub_key=pub.key,
doc_key=doc.key,
base_path=options['dir'])
write_queryset_data(doc_path, docq)

app_models = apps.get_models()

for model in app_models:
SKIPPED_MODEL_NAMES = ['Document']
if model._meta.app_label == 'api_v2' and model.__name__ not in SKIPPED_MODEL_NAMES:
modelq = model.objects.filter(document=doc).order_by('pk')
model_path = get_filepath_by_model(
model.__name__,
model._meta.app_label,
pub_key=pub.key,
doc_key=doc.key,
base_path=options['dir'])
write_queryset_data(model_path, modelq)

self.stdout.write(self.style.SUCCESS(
'Wrote {} to {}'.format(doc.key, doc_path)))

self.stdout.write(self.style.SUCCESS('Data for v2 data complete.'))

def get_filepath_by_model(model_name, app_label, pub_key=None, doc_key=None, base_path=None):

if app_label == "api_v2":
root_folder_name = 'v2'
root_models = ['License', 'Ruleset']
pub_models = ['Publisher']

if model_name in root_models:
return "/".join((base_path,root_folder_name,model_name+".json"))

if model_name in pub_models:
return "/".join((base_path,root_folder_name,pub_key,model_name+".json"))

else:
return "/".join((base_path,root_folder_name,pub_key,doc_key,model_name+".json"))

if app_label == "api":
root_folder_name = 'v1'
root_models = ['Manifest']
doc_folder_name = doc_key

if model_name in root_models:
return "/".join((base_path,root_folder_name, model_name+".json"))

else:
return "/".join((base_path,root_folder_name, doc_key, model_name+".json"))

def write_queryset_data(filepath, queryset):
if queryset.count() > 0:
dir = os.path.dirname(filepath)
if not os.path.exists(dir):
os.makedirs(dir)

output_filepath = filepath
with open(output_filepath, 'w', encoding='utf-8') as f:
serializers.serialize("json", queryset, indent=2, stream=f)
32 changes: 32 additions & 0 deletions api_v2/management/commands/import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


import os
import json
import glob

from django.core.management import call_command
from django.core.management.base import BaseCommand

class Command(BaseCommand):
"""Implementation for the `manage.py `dumpbyorg` subcommand."""

help = 'Import all v2 model data recursively in structured directory.'

def add_arguments(self, parser):
parser.add_argument("-d",
"--dir",
type=str,
help="Directory to write files to.")

def handle(self, *args, **options) -> None:
self.stdout.write('Checking if directory exists.')
if os.path.exists(options['dir']) and os.path.isdir(options['dir']):
self.stdout.write('Directory {} exists.'.format(options['dir']))
else:
self.stdout.write(self.style.ERROR(
'Directory {} does not exist.'.format(options['dir'])))
exit(0)

fixture_filepaths = glob.glob(options['dir'] + '/**/*.json', recursive=True)

call_command('loaddata', fixture_filepaths)
Loading

0 comments on commit cb86c1c

Please sign in to comment.