Refactored static file handling so that extensions can provide static files

This commit is contained in:
2016-09-07 16:36:25 +02:00
parent b2e8711ac4
commit 9e6b998c50
3 changed files with 40 additions and 12 deletions

View File

@@ -231,7 +231,7 @@ class PillarServer(Eve):
paths_list = [ paths_list = [
jinja2.FileSystemLoader(path) jinja2.FileSystemLoader(path)
for path in reversed(self.pillar_extensions_template_paths) for path in reversed(self.pillar_extensions_template_paths)
] ]
# ...then load Pillar paths. # ...then load Pillar paths.
pillar_dir = os.path.dirname(os.path.realpath(__file__)) pillar_dir = os.path.dirname(os.path.realpath(__file__))
@@ -251,23 +251,27 @@ class PillarServer(Eve):
pillar.web.jinja.setup_jinja_env(self.jinja_env) pillar.web.jinja.setup_jinja_env(self.jinja_env)
def _config_static_dirs(self): def _config_static_dirs(self):
pillar_dir = os.path.dirname(os.path.realpath(__file__))
# Setup static folder for the instanced app # Setup static folder for the instanced app
self.static_folder = os.path.join(self.app_root, 'static') self.static_folder = os.path.join(self.app_root, 'static')
# Setup static folder for Pillar # Setup static folder for Pillar
self.pillar_static_folder = os.path.join(pillar_dir, 'web', 'static') pillar_dir = os.path.dirname(os.path.realpath(__file__))
pillar_static_folder = os.path.join(pillar_dir, 'web', 'static')
self.register_static_file_endpoint('/static/pillar', 'static_pillar', pillar_static_folder)
from flask.views import MethodView # Setup static folders for extensions
from flask import send_from_directory for name, ext in self.pillar_extensions.items():
from flask import current_app if not ext.static_path:
continue
self.register_static_file_endpoint('/static/%s' % name,
'static_%s' % name,
ext.static_path)
class PillarStaticFile(MethodView): def register_static_file_endpoint(self, url_prefix, endpoint_name, static_folder):
def get(self, filename): from pillar.web.static import PillarStaticFile
return send_from_directory(current_app.pillar_static_folder,
filename)
self.add_url_rule('/static/pillar/<path:filename>', view_func = PillarStaticFile.as_view(endpoint_name, static_folder=static_folder)
view_func=PillarStaticFile.as_view('static_pillar')) self.add_url_rule('%s/<path:filename>' % url_prefix, view_func=view_func)
def process_extensions(self): def process_extensions(self):
# Re-initialise Eve after we allowed Pillar submodules to be loaded. # Re-initialise Eve after we allowed Pillar submodules to be loaded.

View File

@@ -72,5 +72,17 @@ class PillarExtension(object):
""" """
return None return None
@property
def static_path(self):
"""Returns the path where static files are stored.
Registers an endpoint named 'static_<extension name>', to use like:
`url_for('static_attract', filename='js/somefile.js')`
May return None, in which case the extension will not be able to serve
static files.
"""
return None
def setup_app(self, app): def setup_app(self, app):
"""Called during app startup, after all extensions have loaded.""" """Called during app startup, after all extensions have loaded."""

12
pillar/web/static.py Normal file
View File

@@ -0,0 +1,12 @@
"""Static file handling"""
import flask
import flask.views
class PillarStaticFile(flask.views.MethodView):
def __init__(self, static_folder):
self.static_folder = static_folder
def get(self, filename):
return flask.send_from_directory(self.static_folder, filename)