Skip to content

Commit

Permalink
Merge ecc8001 into 3666cfa
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Aug 1, 2019
2 parents 3666cfa + ecc8001 commit edd010a
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ django_js_reverse/tests/tmp/
.project
.pydevproject
.settings/

index.js
index.mjs
2 changes: 2 additions & 0 deletions .python-version
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
3.5.4
3.6.3
3.7.0
pypy2.7-5.9.0
pypy3-2.4.0
34 changes: 29 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
sudo: true

dist: xenial

sudo: false
language: python
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "pypy"
- "pypy3"
matrix:
include:
- language: python
python: "3.7"
dist: xenial
sudo: true

install:
- nvm install --lts
Expand All @@ -28,3 +32,23 @@ cache: pip
notifications:
email:
- boerni@gmail.com

before_deploy:
- git stash --all
- ./prepare_npm.py
_deploy_provider: &_deploy_provider
skip_cleanup: true
on:
tags: true
repo: ierror/django-js-reverse
python: "3.7"
deploy:
- <<: *_deploy_provider
provider: pypi
distributions: sdist bdist_wheel
user: boerni
password: "TODO: add password"
- <<: *_deploy_provider
provider: npm
email: mboerni@gmail.com
api_key: "TODO: add api_key"
55 changes: 55 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,61 @@ Add ``'django_js_reverse'`` to your ``INSTALLED_APPS`` setting.
)


Usage with webpack
------------------

Install using ``npm``

::

npm install --save django-js-reverse


Include none-cached view …

::

urlpatterns = patterns('',
url(r'^jsreverse.json$', 'django_js_reverse.views.urls_json', name='js_reverse'),
)

… or a cached one that delivers the urls JSON

::

from django_js_reverse import views
urlpatterns = patterns('',
url(r'^jsreverse.json$', cache_page(3600)(views.urls_json), name='js_reverse'),
)

Include JavaScript in your bundle:

::

// utils/djangoReverse.mjs
import _ from 'lodash/fp';
import djangoJsReverse from 'django-js-reverse';

export default _.once(
async () => {
const res = await fetch('/jsreverse.json');
const data = await res.json():
return djangoJsReverse(data);
}
)

::

// somePlace.mjs
import djangoReverse from './utils/djangoReverse';

(async () => {
const urls = await djangoReverse;
const url = urls.someViewName('some-arg');
...
})();


Usage as static file
--------------------

Expand Down
7 changes: 7 additions & 0 deletions django_js_reverse/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,10 @@ def generate_js(default_urlresolver):
if minfiy:
js_content = rjsmin.jsmin(js_content)
return js_content


def generate_cjs_module():
return loader.render_to_string('django_js_reverse/urls_js.tpl', {
'data': mark_safe("'false'"),
'js_name': 'module.exports',
})
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"author": "Bernhard Janetzki",
"bugs": {
"url": "https://github.com/ierror/django-js-reverse/issues"
},
"description": "JavaScript url handling for Django that doesn't hurt.",
"files": [
"index.js",
"index.mjs"
],
"homepage": "https://github.com/ierror/django-js-reverse#readme",
"keywords": [
"django",
"js",
"reverse",
"url"
],
"license": "MIT",
"main": "index.js",
"name": "django-js-reverse",
"repository": {
"type": "git",
"url": "git://github.com/ierror/django-js-reverse.git"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "0.0.0"
}
38 changes: 38 additions & 0 deletions prepare_npm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
import json
import os

import django
from django.conf import settings

import django_js_reverse
from django_js_reverse import core

if __name__ == '__main__':
settings.configure(
INSTALLED_APPS = ['django_js_reverse'],
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
},
],
)
django.setup()

module = core.generate_cjs_module()
with open('index.js', 'w') as cjs:
cjs.write(module)

with open('index.mjs', 'w') as cjs:
cjs.write('const module = {};')
cjs.write(module)
cjs.write('export default module.exports;')

with open('package.json', 'r+') as f:
data = json.load(f)
f.seek(0)
data['version'] = '.'.join([str(v) for v in django_js_reverse.VERSION])
json.dump(data, f, sort_keys=True, indent=2)
f.write('\n');
f.truncate()

0 comments on commit edd010a

Please sign in to comment.