Commit e06d55d3 authored by Serge S. Koval's avatar Serge S. Koval

Merge pull request #181 from chrishaines/static

Can set the static_url_path for underlying blueprints.
parents e0f3d3b8 c7dd5736
...@@ -128,7 +128,7 @@ class BaseView(object): ...@@ -128,7 +128,7 @@ class BaseView(object):
return args return args
def __init__(self, name=None, category=None, endpoint=None, url=None, static_folder=None): def __init__(self, name=None, category=None, endpoint=None, url=None, static_folder=None, static_url_path=None):
""" """
Constructor. Constructor.
...@@ -146,12 +146,15 @@ class BaseView(object): ...@@ -146,12 +146,15 @@ class BaseView(object):
is "test", the resulting URL will look like "/admin/test/". If not provided, will is "test", the resulting URL will look like "/admin/test/". If not provided, will
use endpoint as a base url. However, if URL starts with '/', absolute path is assumed use endpoint as a base url. However, if URL starts with '/', absolute path is assumed
and '/admin/' prefix won't be applied. and '/admin/' prefix won't be applied.
:param static_url_path:
Static URL Path. If provided, this specifies the path to the static url directory.
""" """
self.name = name self.name = name
self.category = category self.category = category
self.endpoint = endpoint self.endpoint = endpoint
self.url = url self.url = url
self.static_folder = static_folder self.static_folder = static_folder
self.static_url_path = static_url_path
# Initialized from create_blueprint # Initialized from create_blueprint
self.admin = None self.admin = None
...@@ -171,6 +174,10 @@ class BaseView(object): ...@@ -171,6 +174,10 @@ class BaseView(object):
# If endpoint name is not provided, get it from the class name # If endpoint name is not provided, get it from the class name
if self.endpoint is None: if self.endpoint is None:
self.endpoint = self.__class__.__name__.lower() self.endpoint = self.__class__.__name__.lower()
# If the static_url_path is not provided, use the admin's
if not self.static_url_path:
self.static_url_path = admin.static_url_path
# If url is not provided, generate it from endpoint name # If url is not provided, generate it from endpoint name
if self.url is None: if self.url is None:
...@@ -198,7 +205,8 @@ class BaseView(object): ...@@ -198,7 +205,8 @@ class BaseView(object):
url_prefix=self.url, url_prefix=self.url,
subdomain=self.admin.subdomain, subdomain=self.admin.subdomain,
template_folder='templates', template_folder='templates',
static_folder=self.static_folder) static_folder=self.static_folder,
static_url_path=self.static_url_path)
for url, name, methods in self._urls: for url, name, methods in self._urls:
self.blueprint.add_url_rule(url, self.blueprint.add_url_rule(url,
...@@ -376,7 +384,8 @@ class Admin(object): ...@@ -376,7 +384,8 @@ class Admin(object):
url=None, subdomain=None, url=None, subdomain=None,
index_view=None, index_view=None,
translations_path=None, translations_path=None,
endpoint=None): endpoint=None,
static_url_path=None):
""" """
Constructor. Constructor.
...@@ -396,6 +405,9 @@ class Admin(object): ...@@ -396,6 +405,9 @@ class Admin(object):
:param endpoint: :param endpoint:
Base endpoint name for index view. If you use multiple instances of the `Admin` class with Base endpoint name for index view. If you use multiple instances of the `Admin` class with
a single Flask application, you have to set a unique endpoint name for each instance. a single Flask application, you have to set a unique endpoint name for each instance.
:param static_url_path:
Static URL Path. If provided, this specifies the default path to the static url directory for
all its views. Can be overriden in view configuration.
""" """
self.app = app self.app = app
...@@ -413,6 +425,7 @@ class Admin(object): ...@@ -413,6 +425,7 @@ class Admin(object):
self.index_view = index_view or AdminIndexView(endpoint=endpoint, url=url) self.index_view = index_view or AdminIndexView(endpoint=endpoint, url=url)
self.endpoint = endpoint or self.index_view.endpoint self.endpoint = endpoint or self.index_view.endpoint
self.url = url or self.index_view.url self.url = url or self.index_view.url
self.static_url_path = static_url_path
self.subdomain = subdomain self.subdomain = subdomain
# Add predefined index view # Add predefined index view
......
...@@ -105,9 +105,10 @@ def test_base_registration(): ...@@ -105,9 +105,10 @@ def test_base_registration():
def test_admin_customizations(): def test_admin_customizations():
app = Flask(__name__) app = Flask(__name__)
admin = base.Admin(app, name='Test', url='/foobar') admin = base.Admin(app, name='Test', url='/foobar', static_url_path='/static/my/admin')
eq_(admin.name, 'Test') eq_(admin.name, 'Test')
eq_(admin.url, '/foobar') eq_(admin.url, '/foobar')
eq_(admin.index_view.blueprint.static_url_path, '/static/my/admin')
client = app.test_client() client = app.test_client()
rv = client.get('/foobar/') rv = client.get('/foobar/')
...@@ -154,6 +155,10 @@ def test_baseview_registration(): ...@@ -154,6 +155,10 @@ def test_baseview_registration():
view = MockView(endpoint='test') view = MockView(endpoint='test')
view.create_blueprint(base.Admin(url='/')) view.create_blueprint(base.Admin(url='/'))
eq_(view.url, '/test') eq_(view.url, '/test')
view = MockView(static_url_path='/static/my/test')
view.create_blueprint(base.Admin())
eq_(view.blueprint.static_url_path, '/static/my/test')
def test_baseview_urls(): def test_baseview_urls():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment