Skip to content

Commit 80a8fe1

Browse files
authored
Merge pull request #19 from bckohan/v1.1
V1.1
2 parents 7e37fac + fe40351 commit 80a8fe1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2593
-350
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ jobs:
2323
python-version: ${{ matrix.python-version }}
2424

2525
- name: Install Poetry
26-
uses: snok/[email protected].1
26+
uses: snok/[email protected].6
2727
with:
2828
virtualenvs-create: true
2929
virtualenvs-in-project: true
3030
- name: Install Release Dependencies
3131
run: |
3232
poetry config virtualenvs.in-project true
3333
poetry run pip install --upgrade pip
34-
poetry install
34+
poetry install -E all
3535
poetry run pip install -U "${{ matrix.django-version }}"
3636
- name: Run Unit Tests
3737
run: |

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ __pycache__/
66
# C extensions
77
*.so
88

9+
# MAC garbage
10+
.DS_Store
11+
12+
# pycharm
13+
.idea
14+
915
# Distribution / packaging
1016
.Python
1117
build/
@@ -115,6 +121,7 @@ venv/
115121
ENV/
116122
env.bak/
117123
venv.bak/
124+
.python-version
118125

119126
# Spyder project settings
120127
.spyderproject
@@ -140,4 +147,6 @@ dmypy.json
140147
# Cython debug symbols
141148
cython_debug/
142149
/render_static/tests/app1/static/
150+
/render_static/tests/app2/static/exclusive
143151
/render_static/tests/global_static/
152+
/render_static/tests/resources/*.pickle

README.rst

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ defines, or enum like structures in server side Python code that are simply repl
3535
side JavaScript. Single-sourcing these structures by generating client side code from the server
3636
side code keeps the stack bone DRY.
3737

38-
Have you ever wished you could replicate Django's ``reverse`` function in a JavaScript library for
39-
your site? Now you can with the ``urls_to_js`` template tag included with `django-render-static`.
38+
`django-render-static` includes builtins for:
39+
- Replicating Django's `reverse` function in JavaScript (`urls_to_js`)
40+
- Auto-translating basic Python class and module structures into JavaScript
41+
(`modules_to_js`, `classes_to_js`)
4042

4143
You can report bugs and discuss features on the
4244
`issues page <https://github.com/bckohan/django-render-static/issues>`_.
@@ -77,11 +79,11 @@ Installation
7779
}
7880
7981
80-
4. Run ``render_static`` preceding every run of ``collectstatic`` :
82+
4. Run ``renderstatic`` preceding every run of ``collectstatic`` :
8183
8284
.. code:: bash
8385
84-
$> manage.py render_static
86+
$> manage.py renderstatic
8587
$> manage.py collectstatic
8688
8789
@@ -151,11 +153,11 @@ class their settings file might look like this:
151153
}
152154
153155
154-
And then of course they would call `render_static` before `collectstatic`:
156+
And then of course they would call `renderstatic` before `collectstatic`:
155157
156158
.. code:: bash
157159
158-
$> ./manage.py render_static
160+
$> ./manage.py renderstatic
159161
$> ./manage.py collectstatic
160162
161163
@@ -229,9 +231,9 @@ Your settings file might look like:
229231
}
230232
231233
232-
Then call `render_static` before `collectstatic`::
234+
Then call `renderstatic` before `collectstatic`::
233235
234-
$> ./manage.py render_static
236+
$> ./manage.py renderstatic
235237
$> ./manage.py collectstatic
236238
237239
If your root urls.py looks like this:
@@ -301,6 +303,19 @@ So you can now fetch paths like this:
301303
// /different/143/emma
302304
const urls = new URLResolver();
303305
urls.reverse('different', {'arg1': 143, 'arg2': 'emma'});
306+
307+
// reverse also supports query parameters
308+
// /different/143/emma?intarg=0&listarg=A&listarg=B&listarg=C
309+
url.reverse(
310+
'different',
311+
{
312+
kwargs: {arg1: 143, arg2: 'emma'},
313+
query: {
314+
intarg: 0,
315+
listarg: ['A', 'B', 'C']
316+
}
317+
}
318+
);
304319
305320
306321
URLGenerationFailed Exceptions & Placeholders

doc/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
sphinx-argparse==0.2.5
2-
sphinx-rtd-theme==0.5.1
2+
sphinx-rtd-theme==0.5.2
33
sphinxcontrib-applehelp==1.0.2; python_version >= "3.5"
44
sphinxcontrib-devhelp==1.0.2; python_version >= "3.5"
55
sphinxcontrib-htmlhelp==1.0.3; python_version >= "3.5"
66
sphinxcontrib-jsmath==1.0.1; python_version >= "3.5"
77
sphinxcontrib-qthelp==1.0.3; python_version >= "3.5"
88
sphinxcontrib-serializinghtml==1.1.4; python_version >= "3.5"
9-
django-render-static==0.1.0
9+
django-render-static==1.1.0

doc/source/changelog.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
Change Log
33
==========
44

5+
v1.1.0
6+
====================
7+
8+
* Added `Support batch rendering & glob patterns in template selectors <https://github.com/bckohan/django-render-static/issues/15>`_
9+
* Fixed `Rename render_static -> renderstatic <https://github.com/bckohan/django-render-static/issues/11>`_
10+
* Added `Allow 'lazy' contexts built after Django bootstrapping <https://github.com/bckohan/django-render-static/issues/6>`_
11+
* Added `Flexible context specifiers <https://github.com/bckohan/django-render-static/issues/17>`_
12+
* Added `Add GET query parameters to ClassURLWriter's reverse function <https://github.com/bckohan/django-render-static/issues/12>`_
13+
14+
515
v1.0.1
616
====================
717

doc/source/commands.rst

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
Commands
55
========
66

7-
render_static
8-
---------------
7+
.. _renderstatic:
98

10-
.. automodule:: render_static.management.commands.render_static
9+
renderstatic
10+
------------
11+
12+
.. automodule:: render_static.management.commands.renderstatic
1113

1214
Usage
1315
~~~~~
1416

1517
.. argparse::
16-
:module: render_static.management.commands.render_static
18+
:module: render_static.management.commands.renderstatic
1719
:func: get_parser
1820
:prog: manage.py
1921

@@ -24,12 +26,12 @@ To generate all templates configured in ``STATIC_TEMPLATES`` settings:
2426

2527
.. code::
2628
27-
$ manage.py render_static
29+
$ manage.py renderstatic
2830
2931
Alternatively individual templates can be generated, regardless of their presence
30-
in ``STATIC_TEMPLATES``. They will be given the global context:
32+
in ``STATIC_TEMPLATES``. They will be given the global context with any overriding context
33+
parameters supplied on the command line:
3134

3235
.. code::
3336
34-
$ manage.py render_static name/of/template1.js name/of/template2.js
35-
37+
$ manage.py renderstatic path/*.js -c ./js_context.yaml -d outputdir

doc/source/configuration.rst

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ settings:
1717
'app_dir': 'static_templates', # search this directory in apps for templates
1818
'loaders': [
1919
# search apps for templates
20-
'render_static.loaders.StaticAppDirectoriesLoader',
20+
'render_static.loaders.StaticAppDirectoriesBatchLoader',
2121
# search DIRS for templates
22-
'render_static.loaders.StaticFilesystemLoader'
22+
'render_static.loaders.StaticFilesystemBatchLoader'
2323
],
2424
'builtins': ['render_static.templatetags.render_static']
2525
},
@@ -41,12 +41,14 @@ The ``STATIC_TEMPLATES`` setting closely mirrors the ``TEMPLATES`` setting by de
4141
template backends should be used. It extends the standard setting with a few options needed by the
4242
static engine including a global context available to all static templates and a set of template
4343
specific configuration parameters. It's advisable to first read about Django's ``TEMPLATES``
44-
setting.
44+
setting. The main difference with ``STATIC_TEMPLATES`` is that it supports batch rendering.
45+
Glob-like patterns can be used to select multiple templates for rendering. See :ref:`loaders` for
46+
more details.
4547

4648
Minimal Configuration
4749
---------------------
4850

49-
To run render_static, ``STATIC_TEMPLATES`` must be defined in settings. If it's an empty
51+
To run `renderstatic`, ``STATIC_TEMPLATES`` must be defined in settings. If it's an empty
5052
dictionary (or None):
5153

5254
.. code-block:: python
@@ -59,14 +61,13 @@ then the default engine and loaders will be used which is equivalent to:
5961
.. code-block:: python
6062
6163
STATIC_TEMPALTES = {
62-
'ENGINES': [
64+
'ENGINES': [{
6365
'BACKEND': 'render_static.backends.StaticDjangoTemplates',
64-
'DIRS': [],
6566
'OPTIONS': {
66-
'loaders': ['render_static.loaders.StaticAppDirectoriesLoader'],
67+
'loaders': ['render_static.loaders.StaticAppDirectoriesBatchLoader'],
6768
'builtins': ['render_static.templatetags.render_static']
6869
},
69-
]
70+
}]
7071
}
7172
7273
@@ -100,18 +101,42 @@ A list of configuration parameters to pass to the backend during initialization.
100101
parameters are inherited from the standard Django template backends. One additional parameter
101102
``app_dir`` can be used to change the default search path for static templates within apps.
102103

104+
.. _loaders:
105+
103106
``loaders``
104107
***********
105108

106109
Works the same way as the ``loaders`` parameter on ``TEMPLATES``. Except when using the standard
107110
template backend the loaders have been extended and static specific loaders should be used instead:
108111

109112
- ``render_static.backends.StaticDjangoTemplates``
110-
- ``render_static.loaders.StaticAppDirectoriesLoader``
111-
- ``render_static.loaders.StaticFilesystemLoader``
112-
- ``render_static.loaders.StaticLocMemLoader``
113+
- ``render_static.loaders.django.StaticAppDirectoriesBatchLoader`` **default**
114+
- ``render_static.loaders.django.StaticFilesystemBatchLoader``
115+
- ``render_static.loaders.django.StaticAppDirectoriesLoader``
116+
- ``render_static.loaders.django.StaticFilesystemLoader``
117+
- ``render_static.loaders.django.StaticLocMemLoader``
118+
119+
- ``render_static.backends.StaticJinja2Templates``
120+
- ``render_static.loaders.jinja2.StaticFileSystemBatchLoader`` **default**
121+
- ``render_static.loaders.jinja2.StaticFileSystemLoader``
122+
- ``render_static.loaders.jinja2.StaticPackageLoader``
123+
- ``render_static.loaders.jinja2.StaticPrefixLoader``
124+
- ``render_static.loaders.jinja2.StaticFunctionLoader``
125+
- ``render_static.loaders.jinja2.StaticDictLoader``
126+
- ``render_static.loaders.jinja2.StaticChoiceLoader``
127+
- ``render_static.loaders.jinja2.StaticModuleLoader``
128+
129+
.. note::
130+
The static template engine supports batch rendering. All loaders that have ``Batch`` in the name
131+
support wild cards and glob-like patterns when loading templates. By default, if no loaders are
132+
specified these loaders are used. For instance, if I wanted to render every .js file in a
133+
directory called static_templates/js I could configure templates like so:
113134

114-
The normal Jinja2 loaders are used for the ``StaticJinja2Templates`` backend.
135+
.. code-block:: python
136+
137+
'templates': {
138+
'js/*.js': {}
139+
}
115140
116141
``context``
117142
-----------
@@ -126,31 +151,58 @@ there is no request object to build context off of. Dynamic templates are also o
126151
contextual data built from the database but static templates are only rendered at deployment time,
127152
so stuffing dynamic database information in static template contexts is not advisable.
128153

154+
Context configuration parameters may be any of the following:
155+
156+
- **dictionary**: Simply specify context dictionary inline
157+
- **callable**: That returns a dictionary. This allows lazy context initialization to take
158+
place after Django bootstrapping
159+
- **json**: A path to a JSON file
160+
- **yaml**: A path to a YAML file (yaml supports comments!)
161+
- **pickle**: A path to a python pickled dictionary
162+
- **python**: A path to a python file. The locals defined in the file will
163+
comprise the context.
164+
- **a packaged resource**: Any of the above files imported as a packaged resource via
165+
:ref:`resource` to any of the above files.
166+
- **import string**: to any of the above.
167+
168+
For example:
169+
170+
.. code-block:: python
171+
172+
from render_static import resource
173+
STATIC_TEMPLATES = {
174+
'context': resource('package.module', 'context.yaml')
175+
}
176+
177+
129178
``templates``
130179
-------------
131180

132-
The ``templates`` dictionary lists all templates that should be generated when `render_static` is
181+
The ``templates`` dictionary lists all templates that should be generated when `renderstatic` is
133182
run with no arguments. If specific configuration directives including rendered path and context are
134183
needed for a template they must be specified here.
135184

136185
.. note::
137186

138-
`render_static` will be able to generate templates not listed in ``templates``, but only if
139-
supplied by name on the command line. Only the default context will be available to them.
187+
`renderstatic` will be able to generate templates not listed in ``templates``, but only if
188+
supplied by name on the command line. Contexts may also be augmented/overridden via the command
189+
line.
140190

141191
``dest``
142192
~~~~~~~~
143193

144194
Override the default destination where a template will be rendered. Templates loaded from ``DIRS``
145-
instead of apps do not have a default destination and must be provided one here. The ``dest``
146-
parameter must contain the full path where the template will be rendered including the file name.
147-
195+
instead of apps do not have a default destination and must be provided one here. When rendering a
196+
single template, if the ``dest`` parameter is not an existing directory, it will be assumed to be
197+
the full path including the file name where the template will be rendered. When rendering in batch
198+
mode, ``dest`` will be treated as a directory and created if it does not exist.
148199

149200
``context``
150201
~~~~~~~~~~~
151202

152203
Provide additional parameters for each template in the ``context`` dictionary. Any context variables
153-
specified here that clash with global context variables will override them.
204+
specified here that clash with global context variables will override them. May be specified using
205+
any of the same context specifiers that work for the global context.
154206

155207

156208
``RENDER_STATIC_REVERSAL_LIMIT``

doc/source/index.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ defines, or enum like structures in server side Python code that are simply repl
1111
side JavaScript. Single-sourcing these structures by generating client side code from the server
1212
side code maintains DRYness.
1313

14-
Have you ever wished you could replicate Django's `reverse` function in a JavaScript library for
15-
your site? Now you can with the `urls_to_js` template tag included with `django-render-static`.
14+
`django-render-static` includes builtins for:
15+
- Replicating Django's `reverse` function in JavaScript (:ref:`urls_to_js`)
16+
- Auto-translating Python class and module structures into JavaScript
17+
(:ref:`modules_to_js`, :ref:`classes_to_js`)
1618

1719
You can report bugs and discuss features on the
1820
`issues page <https://github.com/bckohan/django-render-static/issues>`_.

doc/source/installation.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Installation
2525
}
2626
}
2727

28-
4. Run ``render_static`` preceding every run of ``collectstatic`` ::
28+
4. Run ``renderstatic`` preceding every run of ``collectstatic`` ::
2929

30-
manage.py render_static
30+
manage.py renderstatic
3131
manage.py collectstatic
3232

3333

0 commit comments

Comments
 (0)