From fea711ce5ba5faad9374e4a29e2f505d4e9a8a37 Mon Sep 17 00:00:00 2001 From: Patrik Beck Date: Fri, 24 Jun 2022 11:24:02 +0200 Subject: [PATCH 1/2] Add a django command: collectstatic_json_reverse which generates a JSON file for use in reactjs directly. Utilizing same code as collectstatic_js_reverse --- .../management/commands/__init__.py | 20 +++++++++++ .../commands/collectstatic_js_reverse.py | 19 ++--------- .../commands/collectstatic_json_reverse.py | 33 +++++++++++++++++++ 3 files changed, 56 insertions(+), 16 deletions(-) create mode 100755 django_js_reverse/management/commands/collectstatic_json_reverse.py diff --git a/django_js_reverse/management/commands/__init__.py b/django_js_reverse/management/commands/__init__.py index 882555b..1214e47 100755 --- a/django_js_reverse/management/commands/__init__.py +++ b/django_js_reverse/management/commands/__init__.py @@ -1 +1,21 @@ __author__ = 'boerni' + +import os + +from django.core.management.base import BaseCommand +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured +from django_js_reverse.js_reverse_settings import JS_OUTPUT_PATH + +class CollectStaticCommand(BaseCommand): + requires_system_checks = False + def get_location(self): + output_path = getattr(settings, 'JS_REVERSE_OUTPUT_PATH', JS_OUTPUT_PATH) + if output_path: + return output_path + + if not hasattr(settings, 'STATIC_ROOT') or not settings.STATIC_ROOT: + raise ImproperlyConfigured( + 'The collectstatic_js_reverse command needs settings.JS_REVERSE_OUTPUT_PATH or settings.STATIC_ROOT to be set.') + + return os.path.join(settings.STATIC_ROOT, 'django_js_reverse', 'js') diff --git a/django_js_reverse/management/commands/collectstatic_js_reverse.py b/django_js_reverse/management/commands/collectstatic_js_reverse.py index d1e758a..9d8b600 100755 --- a/django_js_reverse/management/commands/collectstatic_js_reverse.py +++ b/django_js_reverse/management/commands/collectstatic_js_reverse.py @@ -1,14 +1,11 @@ # -*- coding: utf-8 -*- -import os import sys from django.conf import settings -from django.core.exceptions import ImproperlyConfigured from django.core.files.base import ContentFile from django.core.files.storage import FileSystemStorage -from django.core.management.base import BaseCommand from django_js_reverse.core import generate_js -from django_js_reverse.js_reverse_settings import JS_OUTPUT_PATH +from django_js_reverse.management.commands import CollectStaticCommand try: from django.urls import get_resolver @@ -16,19 +13,9 @@ from django.core.urlresolvers import get_resolver -class Command(BaseCommand): +class Command(CollectStaticCommand): help = 'Creates a static urls-js file for django-js-reverse' requires_system_checks = False - def get_location(self): - output_path = getattr(settings, 'JS_REVERSE_OUTPUT_PATH', JS_OUTPUT_PATH) - if output_path: - return output_path - - if not hasattr(settings, 'STATIC_ROOT') or not settings.STATIC_ROOT: - raise ImproperlyConfigured( - 'The collectstatic_js_reverse command needs settings.JS_REVERSE_OUTPUT_PATH or settings.STATIC_ROOT to be set.') - - return os.path.join(settings.STATIC_ROOT, 'django_js_reverse', 'js') def handle(self, *args, **options): location = self.get_location() @@ -42,4 +29,4 @@ def handle(self, *args, **options): content = generate_js(default_urlresolver) fs.save(file, ContentFile(content)) if len(sys.argv) > 1 and sys.argv[1] in ['collectstatic_js_reverse']: - self.stdout.write('js-reverse file written to %s' % (location)) # pragma: no cover + self.stdout.write('%s file written to %s' % (file, location)) # pragma: no cover diff --git a/django_js_reverse/management/commands/collectstatic_json_reverse.py b/django_js_reverse/management/commands/collectstatic_json_reverse.py new file mode 100755 index 0000000..cee38ad --- /dev/null +++ b/django_js_reverse/management/commands/collectstatic_json_reverse.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +import sys +import json + +from django.conf import settings +from django.core.files.base import ContentFile +from django.core.files.storage import FileSystemStorage +from django_js_reverse.core import generate_json +from django_js_reverse.management.commands import CollectStaticCommand + +try: + from django.urls import get_resolver +except ImportError: + from django.core.urlresolvers import get_resolver + + +class Command(CollectStaticCommand): + help = 'Creates a static urls-json file for django-js-reverse' + requires_system_checks = False + + def handle(self, *args, **options): + location = self.get_location() + file = 'reverse.json' + fs = FileSystemStorage(location=location) + if fs.exists(file): + fs.delete(file) + + urlconf = getattr(settings, 'ROOT_URLCONF', None) + default_urlresolver = get_resolver(urlconf) + content = json.dumps(generate_json(default_urlresolver)) + fs.save(file, ContentFile(content)) + if len(sys.argv) > 1 and sys.argv[1] in ['collectstatic_json_reverse']: + self.stdout.write('%s file written to %s' % (file, location)) # pragma: no cover From eef2b427efceafc3d341aa5ef75455edb6ce6d41 Mon Sep 17 00:00:00 2001 From: Patrik Beck Date: Fri, 24 Jun 2022 11:26:11 +0200 Subject: [PATCH 2/2] documentation updated --- README.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.rst b/README.rst index e8662ba..b158322 100644 --- a/README.rst +++ b/README.rst @@ -165,6 +165,21 @@ After this add the file to your template +Usage as a static JSON file +-------------------- + +First generate static file by + +:: + + ./manage.py collectstatic_json_reverse + +If you change some urls or add an app and want to update the reverse.json file, +run the command again. + +The file can be imported into your reactjs/angularjs/vue JS code directly. + + Usage with views ----------------