Skip to content
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
17 changes: 12 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@ Example::
Example::
URLS_JS_TO_EXPOSE = ['admin', 'another_namespace', 'a_url_name',]

4. Add dutils.js and urls routing Javascript file to every web page where you plan to use the reverse function
(likely just include in your base.html template)
4. Add entries into your base.html template (or where ever you need) to include the dutils.js file, along with the dutils.conf.urls.js patterns file. If you're using Django's staticfiles app, you'll use something
like::
<script type="text/javascript" src="{{STATIC_URL}}dutils.js"></script>
<script type="text/javascript" src="{{STATIC_URL}}dutils.conf.urls.js"></script>


Usage
*****
1. To generate a list of all available urls in the special
1. Generate a list of all available urls in the special
format::
>>> python manage.js js_urls
>>> python manage.py js_urls

To keep the list of urls up-to-date, it is recommended to include this command as part of the build process.

2. On the web page, reverse url as
2. If you're using Django's staticfiles app, issue the collectstatic command to include dutils.js and the urls list you generated in step 1 in your static
directory::
>>> python manage.py collectstatic

3. On the web page, reverse url as
such::
>>> $.post(dutils.urls.resolve('time_edit', { project_id: 1, time_id: 2 }), ...

Expand Down
25 changes: 13 additions & 12 deletions django_js_utils/management/commands/js_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@
from django.utils import simplejson
from django.utils.datastructures import SortedDict
from django.conf import settings as project_settings
from django_js_utils import settings as app_settings

from django.conf import settings as app_settings


RE_KWARG = re.compile(r"(\(\?P\<(.*?)\>.*?\))") #Pattern for recongnizing named parameters in urls
RE_KWARG = re.compile(r"(\(\?P\<(.*?)\>.*?\)\)?)") #Pattern for recongnizing named parameters in urls
RE_ARG = re.compile(r"(\(.*?\))") #Pattern for recognizing unnamed url parameters


URLS_JS_GENERATED_FILE = getattr(project_settings, 'URLS_JS_GENERATED_FILE', app_settings.URLS_JS_GENERATED_FILE)
URLS_JS_TO_EXPOSE = getattr(project_settings, 'URLS_JS_TO_EXPOSE', app_settings.URLS_JS_TO_EXPOSE)

class Command(BaseCommand):
def handle(self, *args, **options):
"""
Create urls.js file by parsing all of the urlpatterns in the root urls.py file
"""
js_patterns = SortedDict()
print "Generating Javascript urls file %s" % app_settings.URLS_JS_GENERATED_FILE
print "Generating Javascript urls file %s" % URLS_JS_GENERATED_FILE
Command.handle_url_module(js_patterns, project_settings.ROOT_URLCONF)
#output to the file
urls_file = open(app_settings.URLS_JS_GENERATED_FILE, "w")
urls_file.write("dutils.conf.urls = ")
urls_file = open(URLS_JS_GENERATED_FILE, "w")
urls_file.write(";dutils.conf.urls = ")
simplejson.dump(js_patterns, urls_file)
print "Done generating Javascript urls file %s" % app_settings.URLS_JS_GENERATED_FILE

urls_file.write(";")
print "Done generating Javascript urls file %s" % URLS_JS_GENERATED_FILE

@staticmethod
def handle_url_module(js_patterns, module_name, prefix=""):
"""
Expand All @@ -44,7 +45,7 @@ def handle_url_module(js_patterns, module_name, prefix=""):

for pattern in patterns:
if issubclass(pattern.__class__, RegexURLPattern):
if pattern.name and pattern.name in app_settings.URLS_JS_TO_EXPOSE:
if pattern.name and pattern.name in URLS_JS_TO_EXPOSE:
full_url = prefix + pattern.regex.pattern
for chr in ["^","$"]:
full_url = full_url.replace(chr, "")
Expand All @@ -61,5 +62,5 @@ def handle_url_module(js_patterns, module_name, prefix=""):
full_url = full_url.replace(el, "<>")#replace by a empty parameter name
js_patterns[pattern.name] = "/" + full_url
elif issubclass(pattern.__class__, RegexURLResolver):
if pattern.urlconf_name and pattern.urlconf_name in app_settings.URLS_JS_TO_EXPOSE:
if pattern.urlconf_name and pattern.urlconf_name in URLS_JS_TO_EXPOSE:
Command.handle_url_module(js_patterns, pattern.urlconf_name, prefix=pattern.regex.pattern)
1 change: 1 addition & 0 deletions django_js_utils/settings.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Where to put the Javascript URL Routing file
URLS_JS_GENERATED_FILE='static/js/dutils.conf.urls.js'
URLS_JS_TO_EXPOSE=[]
File renamed without changes.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
url='https://github.com/Dimitri-Gnidash/django-js-utils',
packages = find_packages(
exclude = ['ez_setup', 'examples', 'tests']),
package_data = {'django_js_utils': ['static/dutils.js',]},
)