Skip to content
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

updating build process to handle included urls #6

Open
wants to merge 18 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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist/django_js_utils-0.2.tar.gz
django_js_utils.egg-info/PKG-INFO
django_js_utils.egg-info/SOURCES.txt
django_js_utils.egg-info/dependency_links.txt
django_js_utils.egg-info/top_level.txt
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Dimitri-Gnidash
Contributors:
mlouro
Krzysztof Dorosz (cypreess)
Matt Keeble (beldougie)
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include django_js_utils/static/js/dutils.js
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ Installation
<script src="{{ STATIC_URL }}js/dutils.js"></script>
<script src="{{ STATIC_URL }}js/django-urls.js"></script>

Note: Obtaining the correct dutils.js file should be as simple as running:

python manage.py collectstatic

Usage
*****
1. To 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.

Expand Down
59 changes: 35 additions & 24 deletions django_js_utils/management/commands/js_urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
import sys
import re
from django.core.exceptions import ImproperlyConfigured

from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
from django.core.management.base import BaseCommand
from django.utils import simplejson
from django.utils.datastructures import SortedDict
from django.conf import settings

Expand All @@ -30,7 +30,9 @@ def handle(self, *args, **options):
#output to the file
urls_file = open(URLS_JS_GENERATED_FILE, "w")
urls_file.write("dutils.conf.urls = ")
simplejson.dump(js_patterns, urls_file)
json.dump(js_patterns, urls_file)
urls_file.write(";")
urls_file.close()
print "Done generating Javascript urls file %s" % URLS_JS_GENERATED_FILE

@staticmethod
Expand All @@ -39,32 +41,41 @@ def handle_url_module(js_patterns, module_name, prefix=""):
Load the module and output all of the patterns
Recurse on the included modules
"""

if isinstance(module_name, basestring):
__import__(module_name)
root_urls = sys.modules[module_name]
patterns = root_urls.urlpatterns
else:
elif isinstance(module_name, object):
if hasattr(module_name, 'urlpatterns'):
patterns = module_name.urlpatterns

if not 'patterns' in locals():
root_urls = module_name
patterns = root_urls

for pattern in patterns:
if issubclass(pattern.__class__, RegexURLPattern):
if pattern.name:
full_url = prefix + pattern.regex.pattern
for chr in ["^","$"]:
full_url = full_url.replace(chr, "")
#handle kwargs, args
kwarg_matches = RE_KWARG.findall(full_url)
if kwarg_matches:
for el in kwarg_matches:
#prepare the output for JS resolver
full_url = full_url.replace(el[0], "<%s>" % el[1])
#after processing all kwargs try args
args_matches = RE_ARG.findall(full_url)
if args_matches:
for el in args_matches:
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:
Command.handle_url_module(js_patterns, pattern.urlconf_name, prefix=pattern.regex.pattern)
try:
for pattern in patterns:
if issubclass(pattern.__class__, RegexURLPattern):
if pattern.name:
full_url = prefix + pattern.regex.pattern
for chr in ["^","$"]:
full_url = full_url.replace(chr, "")
#handle kwargs, args
kwarg_matches = RE_KWARG.findall(full_url)
if kwarg_matches:
for el in kwarg_matches:
#prepare the output for JS resolver
full_url = full_url.replace(el[0], "<%s>" % el[1])
#after processing all kwargs try args
args_matches = RE_ARG.findall(full_url)
if args_matches:
for el in args_matches:
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:
Command.handle_url_module(js_patterns, pattern.urlconf_name, prefix=pattern.regex.pattern)
except TypeError:
print "couldn't iterate over patterns"
print patterns
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def find_version(*file_paths):
# maintainer_email='',
# url='',
packages=find_packages(),
include_package_data=True,
package_data={
'static/js': ['*.js']
},
license='MIT',
classifiers=[
'Development Status :: 4 - Beta',
Expand Down