Commit 831c38f3 authored by michael lynch's avatar michael lynch

added Admin view updates to the init_app call, so users can add the admin view at that time.

parent c945f214
import os.path as op import os.path as op
import warnings
from functools import wraps from functools import wraps
...@@ -440,7 +439,7 @@ class AdminIndexView(BaseView): ...@@ -440,7 +439,7 @@ class AdminIndexView(BaseView):
super(AdminIndexView, self).__init__(name or babel.lazy_gettext('Home'), super(AdminIndexView, self).__init__(name or babel.lazy_gettext('Home'),
category, category,
endpoint or 'admin', endpoint or 'admin',
'/admin' if url is None else url, url or '/admin',
'static', 'static',
menu_class_name=menu_class_name, menu_class_name=menu_class_name,
menu_icon_type=menu_icon_type, menu_icon_type=menu_icon_type,
...@@ -509,9 +508,6 @@ class Admin(object): ...@@ -509,9 +508,6 @@ class Admin(object):
name = 'Admin' name = 'Admin'
self.name = name self.name = name
self.index_view = index_view or AdminIndexView(endpoint=endpoint, url=url)
self.endpoint = endpoint or self.index_view.endpoint
self.url = url or self.index_view.url
self.static_url_path = static_url_path self.static_url_path = static_url_path
self.subdomain = subdomain self.subdomain = subdomain
self.base_template = base_template or 'admin/base.html' self.base_template = base_template or 'admin/base.html'
...@@ -519,12 +515,32 @@ class Admin(object): ...@@ -519,12 +515,32 @@ class Admin(object):
self.category_icon_classes = category_icon_classes or dict() self.category_icon_classes = category_icon_classes or dict()
# Add predefined index view # Add predefined index view
self.add_view(self.index_view) self._set_admin_index_view(index_view=index_view, endpoint=endpoint, url=url)
# Register with application # Register with application
if app is not None: if app is not None:
self._init_extension() self._init_extension()
def _set_admin_index_view(self, index_view=None,
endpoint=None, url=None):
"""
Add the admin index view.
:param index_view:
Home page view to use. Defaults to `AdminIndexView`.
:param url:
Base URL
:param endpoint:
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.
"""
self.index_view = index_view or AdminIndexView(endpoint=endpoint, url=url)
self.endpoint = endpoint or self.index_view.endpoint
self.url = url or self.index_view.url
# Add predefined index view
self.add_view(self.index_view)
def add_view(self, view): def add_view(self, view):
""" """
Add a view to the collection. Add a view to the collection.
...@@ -565,7 +581,7 @@ class Admin(object): ...@@ -565,7 +581,7 @@ class Admin(object):
Link to add. Link to add.
""" """
if link.category: if link.category:
self.add_menu_item(link, link.category) self._add_menu_item(link, link.category)
else: else:
self._menu_links.append(link) self._menu_links.append(link)
...@@ -585,15 +601,7 @@ class Admin(object): ...@@ -585,15 +601,7 @@ class Admin(object):
for link in args: for link in args:
self.add_link(link) self.add_link(link)
def add_menu_item(self, menu_item, target_category=None): def _add_menu_item(self, menu_item, target_category):
"""
Add menu item to menu tree hierarchy.
:param menu_item:
MenuItem class instance
:param target_category:
Target category name
"""
if target_category: if target_category:
cat_text = as_unicode(target_category) cat_text = as_unicode(target_category)
...@@ -611,10 +619,6 @@ class Admin(object): ...@@ -611,10 +619,6 @@ class Admin(object):
else: else:
self._menu.append(menu_item) self._menu.append(menu_item)
def _add_menu_item(self, menu_item, target_category):
warnings.warn('Admin._add_menu_item is obsolete - use Admin.add_menu_item instead.')
return self.add_menu_item(menu_item, target_category)
def _add_view_to_menu(self, view): def _add_view_to_menu(self, view):
""" """
Add a view to the menu tree Add a view to the menu tree
...@@ -622,12 +626,13 @@ class Admin(object): ...@@ -622,12 +626,13 @@ class Admin(object):
:param view: :param view:
View to add View to add
""" """
self.add_menu_item(MenuView(view.name, view), view.category) self._add_menu_item(MenuView(view.name, view), view.category)
def get_category_menu_item(self, name): def get_category_menu_item(self, name):
return self._menu_categories.get(name) return self._menu_categories.get(name)
def init_app(self, app): def init_app(self, app, index_view=None,
endpoint=None, url=None):
""" """
Register all views with the Flask application. Register all views with the Flask application.
...@@ -638,10 +643,16 @@ class Admin(object): ...@@ -638,10 +643,16 @@ class Admin(object):
self._init_extension() self._init_extension()
self._views = []
# Register views # Register views
for view in self._views: for view in self._views:
app.register_blueprint(view.create_blueprint(self)) app.register_blueprint(view.create_blueprint(self))
# Register Index view
self._set_admin_index_view(index_view=index_view, endpoint=endpoint, url=url)
def _init_extension(self): def _init_extension(self):
if not hasattr(self.app, 'extensions'): if not hasattr(self.app, 'extensions'):
self.app.extensions = dict() self.app.extensions = dict()
......
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