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)): ...@@ -187,7 +187,7 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
""" """
self.name = name self.name = name
self.category = category self.category = category
self.endpoint = endpoint self.endpoint = self._get_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 self.static_url_path = static_url_path
...@@ -205,6 +205,16 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)): ...@@ -205,6 +205,16 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
if self._default_view is None: if self._default_view is None:
raise Exception(u'Attempted to instantiate admin view %s without default view' % self.__class__.__name__) 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): def create_blueprint(self, admin):
""" """
Create Flask blueprint. Create Flask blueprint.
...@@ -212,10 +222,6 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)): ...@@ -212,10 +222,6 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
# Store admin instance # Store admin instance
self.admin = admin 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 the static_url_path is not provided, use the admin's
if not self.static_url_path: if not self.static_url_path:
self.static_url_path = admin.static_url_path self.static_url_path = admin.static_url_path
...@@ -233,15 +239,13 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)): ...@@ -233,15 +239,13 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
if not self.url.startswith('/'): if not self.url.startswith('/'):
self.url = '%s/%s' % (self.admin.url, self.url) self.url = '%s/%s' % (self.admin.url, self.url)
# If we're working from the root of the site, set prefix to None # If we're working from the root of the site, set prefix to None
if self.url == '/': if self.url == '/':
self.url = None self.url = None
# prevent admin static files from conflicting with flask static files # prevent admin static files from conflicting with flask static files
if not self.static_url_path: if not self.static_url_path:
self.static_folder='static' self.static_folder = 'static'
self.static_url_path='/static/admin' self.static_url_path = '/static/admin'
# If name is not povided, use capitalized endpoint name # If name is not povided, use capitalized endpoint name
if self.name is None: if self.name is None:
......
...@@ -561,28 +561,30 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -561,28 +561,30 @@ class BaseModelView(BaseView, ActionsMixin):
:param menu_icon_value: :param menu_icon_value:
Icon glyph name or URL, depending on `menu_icon_type` setting Icon glyph name or URL, depending on `menu_icon_type` setting
""" """
self.model = model
# If name not provided, it is model name # If name not provided, it is model name
if name is None: if name is None:
name = '%s' % self._prettify_class_name(model.__name__) 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, super(BaseModelView, self).__init__(name, category, endpoint, url, static_folder,
menu_class_name=menu_class_name, menu_class_name=menu_class_name,
menu_icon_type=menu_icon_type, menu_icon_type=menu_icon_type,
menu_icon_value=menu_icon_value) menu_icon_value=menu_icon_value)
self.model = model
# Actions # Actions
self.init_actions() self.init_actions()
# Scaffolding # Scaffolding
self._refresh_cache() self._refresh_cache()
# Endpoint
def _get_endpoint(self, endpoint):
if endpoint:
return super(BaseModelView, self)._get_endpoint(endpoint)
return self.__class__.__name__.lower()
# Caching # Caching
def _refresh_forms_cache(self): def _refresh_forms_cache(self):
# Forms # Forms
......
...@@ -76,7 +76,7 @@ def test_baseview_defaults(): ...@@ -76,7 +76,7 @@ def test_baseview_defaults():
view = MockView() view = MockView()
eq_(view.name, None) eq_(view.name, None)
eq_(view.category, None) eq_(view.category, None)
eq_(view.endpoint, None) eq_(view.endpoint, 'mockview')
eq_(view.url, None) eq_(view.url, None)
eq_(view.static_folder, None) eq_(view.static_folder, None)
eq_(view.admin, None) eq_(view.admin, None)
...@@ -388,3 +388,12 @@ def test_menu_links(): ...@@ -388,3 +388,12 @@ def test_menu_links():
def check_class_name(): def check_class_name():
view = MockView() view = MockView()
eq_(view.name, 'Mock View') 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