Skip to content

Pb collect static json file generation #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ After this add the file to your template
<script src="{% static 'django_js_reverse/js/reverse.js' %}"></script>


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
----------------

Expand Down
20 changes: 20 additions & 0 deletions django_js_reverse/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -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')
19 changes: 3 additions & 16 deletions django_js_reverse/management/commands/collectstatic_js_reverse.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
# -*- 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
except ImportError:
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()
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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