summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2017-12-21 14:19:52 +0100
committerGuido Günther <agx@sigxcpu.org>2017-12-21 14:19:52 +0100
commitdaaf6a721efddcab51fb3fe66a2c8916cfc60319 (patch)
tree3e60d5e75a569cb7ced2f4dee16806773c588b02
parent4b60fa66ab11c1ac4913700c78ed2c90e2bc251d (diff)
Add a default route for /xstatic
so the user doesn't have to install it's own.
-rw-r--r--README.rst26
-rw-r--r--dev_requirements.txt1
-rw-r--r--flask_xstatic_files.py23
-rw-r--r--tests/test_xstatic.py22
4 files changed, 50 insertions, 22 deletions
diff --git a/README.rst b/README.rst
index 9a70001..b213303 100644
--- a/README.rst
+++ b/README.rst
@@ -17,27 +17,35 @@ use. This example uses JQuery and D3::
app = Flask(__name__)
app.config.from_object(__name__)
- app.config.['XSTATIC_MODULES'] = "jquery,d3"
+ app.config.['XSTATIC_MODULES'] = ['jquery', 'd3']
xsf = XStaticFiles(app)
+The modules can be given as list or comma separated (``jquery,d3``).
Serving Files
-------------
-Install a minimal route handler to serve the files::
+The extension already installs a route handler for you so files under
+``/xstatic/<module>/`` are served automatically. To generate these
+URLs you can use ``url_for``::
- @app.route('/xstatic/<module>/<path:filename>')
- def xstatic(module, filename):
- return xsf.serve_or_404(module, filename)
+ xsf.url_for('jquery', 'jquery.min.js')
-In your templates you can then use ``xstatic_url_for``:
+In templates you can use ``xstatic_url_for``:
.. code-block:: html
<script type=text/javascript src="{{ xstatic_url_for(module='jquery', path='jquery.min.js') }}"></script>
-This has the advantage that you can later serve files from a static
-web server by adjusting ``XSTATIC_ROOT`` and ``XSTATIC_PROTO`` without
-having to modify any code.
+to generate these URLs. This has the advantage that you can later
+serve files from a static web server by adjusting ``XSTATIC_ROOT`` and
+``XSTATIC_PROTO`` without having to modify any code.
+
+In case you want to serve XStatic files from other URLs use ``serve``
+or ``serve_or_404``::
+
+ @app.route('/somewhere-else/jquery/jquery.min.js')
+ def serve_jquery():
+ return xsf.serve_or_404('jquery', 'jquery.min.js')
.. _Flask: http://flask.pocoo.org/
.. _XStatic: https://xstatic.readthedocs.io/en/latest/
diff --git a/dev_requirements.txt b/dev_requirements.txt
index 1795b27..37cf232 100644
--- a/dev_requirements.txt
+++ b/dev_requirements.txt
@@ -3,3 +3,4 @@
flake8==3.5.0
pytest
XStatic-jQuery
+XStatic-d3
diff --git a/flask_xstatic_files.py b/flask_xstatic_files.py
index 1a4ae53..ee8691b 100644
--- a/flask_xstatic_files.py
+++ b/flask_xstatic_files.py
@@ -18,7 +18,6 @@
Tiny flask extionsion to serve xstatic files
"""
-import os
from flask import current_app, abort
from flask.helpers import send_from_directory
import xstatic.main
@@ -59,26 +58,34 @@ class XStaticFiles(object):
self.app.jinja_env.globals['xstatic_url_for'] = self.url_for
+ @app.route('/xstatic/<module>/<path:filename>')
+ def xstatic(module, filename):
+ return self.serve_or_404(module, filename)
+
def _load(self):
modules = current_app.config['XSTATIC_MODULES']
if not modules:
return {}
- modules = modules.split(',')
+
+ # Allow for comma separated or list
+ if isinstance(modules, str):
+ modules = modules.split(',')
+
pkg = __import__('xstatic.pkg', fromlist=modules)
url = current_app.config['XSTATIC_ROOT']
proto = current_app.config['XSTATIC_PROTO']
- xsf = {}
+ loaded_modules = {}
for name in modules:
mod = getattr(pkg, name)
xs = xstatic.main.XStatic(mod, root_url=url, protocol=proto)
- xsf[xs.name] = xs
- return xsf
+ loaded_modules[xs.name] = xs
+ return loaded_modules
def teardown(self, exception):
pass
@property
- def xsf(self):
+ def modules(self):
ctx = stack.top
if ctx is not None:
if not hasattr(ctx, 'xsf'):
@@ -86,10 +93,10 @@ class XStaticFiles(object):
return ctx.xsf
def url_for(self, module, path):
- return self.xsf[module].url_for(path)
+ return self.modules[module].url_for(path)
def serve(self, module, path):
- return send_from_directory(self.xsf[module].base_dir, path)
+ return send_from_directory(self.modules[module].base_dir, path)
def serve_or_404(self, module, path):
try:
diff --git a/tests/test_xstatic.py b/tests/test_xstatic.py
index 442714a..dc96ffd 100644
--- a/tests/test_xstatic.py
+++ b/tests/test_xstatic.py
@@ -7,12 +7,24 @@ def test_url_for(app):
assert xsf.url_for('jquery', 'jquery.min.js') == '/xstatic/jquery/jquery.min.js'
-def test_serve(app):
- xsf = fx.XStaticFiles(app)
+def test_init_string(app):
+ with app.test_request_context():
+ app.config['XSTATIC_MODULES'] = 'jquery,d3'
+ xsf = fx.XStaticFiles(app)
+ assert 2 == len(xsf.modules)
+ assert ['d3', 'jquery'] == sorted(xsf.modules.keys())
+
- @app.route('/xstatic/<module>/<path:filename>')
- def xstatic(module, filename):
- return xsf.serve_or_404(module, filename)
+def test_init_list(app):
+ with app.test_request_context():
+ app.config['XSTATIC_MODULES'] = ['jquery', 'd3']
+ xsf = fx.XStaticFiles(app)
+ assert 2 == len(xsf.modules)
+ assert ['d3', 'jquery'] == sorted(xsf.modules.keys())
+
+
+def test_serve(app):
+ fx.XStaticFiles(app)
c = app.test_client()
resp = c.get('/xstatic/jquery/exist')