Commit 55e5fb93 authored by Serge S. Koval's avatar Serge S. Koval

Ability to override automatically generated endpoint name

parent 86d0170a
......@@ -187,7 +187,7 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
"""
self.name = name
self.category = category
self.endpoint = endpoint
self.endpoint = self._get_endpoint(endpoint)
self.url = url
self.static_folder = static_folder
self.static_url_path = static_url_path
......@@ -205,6 +205,16 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
if self._default_view is None:
raise Exception(u'Attempted to instantiate admin view %s without default view' % self.__class__.__name__)
def _get_endpoint(self, endpoint):
"""
Generate Flask endpoint name. By default converts class name to lower case if endpoint is
not explicitly provided.
"""
if endpoint:
return endpoint
return self.__class__.__name__.lower()
def create_blueprint(self, admin):
"""
Create Flask blueprint.
......@@ -212,10 +222,6 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
# Store admin instance
self.admin = admin
# If endpoint name is not provided, get it from the class name
if self.endpoint is None:
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
......@@ -233,16 +239,14 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
if not self.url.startswith('/'):
self.url = '%s/%s' % (self.admin.url, self.url)
# If we're working from the root of the site, set prefix to None
if self.url == '/':
self.url = None
# prevent admin static files from conflicting with flask static files
if not self.static_url_path:
self.static_folder='static'
self.static_url_path='/static/admin'
self.static_folder = 'static'
self.static_url_path = '/static/admin'
# If name is not povided, use capitalized endpoint name
if self.name is None:
self.name = self._prettify_class_name(self.__class__.__name__)
......@@ -396,8 +400,8 @@ class AdminIndexView(BaseView):
return self.render('admin/myhome.html', arg1=arg1)
admin = Admin(index_view=MyHomeView())
Also, you can change the root url from /admin to / with the following::
admin = Admin(
......
......@@ -561,28 +561,30 @@ class BaseModelView(BaseView, ActionsMixin):
:param menu_icon_value:
Icon glyph name or URL, depending on `menu_icon_type` setting
"""
self.model = model
# If name not provided, it is model name
if name is None:
name = '%s' % self._prettify_class_name(model.__name__)
# If endpoint not provided, it is model name
if endpoint is None:
endpoint = model.__name__.lower()
super(BaseModelView, self).__init__(name, category, endpoint, url, static_folder,
menu_class_name=menu_class_name,
menu_icon_type=menu_icon_type,
menu_icon_value=menu_icon_value)
self.model = model
# Actions
self.init_actions()
# Scaffolding
self._refresh_cache()
# Endpoint
def _get_endpoint(self, endpoint):
if endpoint:
return super(BaseModelView, self)._get_endpoint(endpoint)
return self.__class__.__name__.lower()
# Caching
def _refresh_forms_cache(self):
# Forms
......@@ -972,7 +974,7 @@ class BaseModelView(BaseView, ActionsMixin):
Instantiate model delete form and return it.
Override to implement custom behavior.
The delete form originally used a GET request, so delete_form
accepts both GET and POST request for backwards compatibility.
"""
......
......@@ -76,7 +76,7 @@ def test_baseview_defaults():
view = MockView()
eq_(view.name, None)
eq_(view.category, None)
eq_(view.endpoint, None)
eq_(view.endpoint, 'mockview')
eq_(view.url, None)
eq_(view.static_folder, None)
eq_(view.admin, None)
......@@ -132,11 +132,11 @@ def test_admin_customizations():
client = app.test_client()
rv = client.get('/foobar/')
eq_(rv.status_code, 200)
# test custom static_url_path
with app.test_request_context('/'):
rv = client.get(url_for('admin.static', filename='bootstrap/bootstrap2/css/bootstrap.css'))
eq_(rv.status_code, 200)
eq_(rv.status_code, 200)
def test_baseview_registration():
......@@ -364,7 +364,7 @@ def test_root_mount():
client = app.test_client()
rv = client.get('/mockview/')
eq_(rv.data, b'Success!')
# test static files when url='/'
with app.test_request_context('/'):
rv = client.get(url_for('admin.static', filename='bootstrap/bootstrap2/css/bootstrap.css'))
......@@ -388,3 +388,12 @@ def test_menu_links():
def check_class_name():
view = MockView()
eq_(view.name, 'Mock View')
def check_endpoint():
class CustomView(MockView):
def _get_endpoint(self, endpoint):
return 'admin.' + super(CustomView, self)._get_endpoint(endpoint)
view = CustomView()
eq_(view.endpoint, 'admin.customview')
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