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

Possibility to pass Flask app to the constructor. Renamed setup_app to init_app.

parent bf2e25a1
- Core
- Pregenerate URLs for menu
- Flask app in constructor
- Calendar - add validation for time without seconds (automatically add seconds)
- Model Admin
- Ability to sort by fields that are not visible?
......
......@@ -34,5 +34,5 @@
.. automethod:: __init__
.. automethod:: add_view
.. automethod:: setup_app
.. automethod:: init_app
.. automethod:: menu
......@@ -42,8 +42,8 @@ To start using Flask-AdminEx, you have to create `Admin` class instance and asso
app = Flask(__name__)
admin = Admin()
admin.setup_app(app)
admin = Admin(app)
# Add administrative views here
app.run()
......@@ -52,11 +52,15 @@ you should see empty "Home" page with a navigation bar on top.
You can change application name by passing `name` parameter to the `Admin` class constructor::
admin = Admin(name='My App')
admin.setup_app(app)
admin = Admin(app, name='My App')
Name is displayed in the menu section.
You don't have to pass Flask application object to the constructor - you can call `init_app` later::
admin = Admin(name='My App')
# Add views here
admin.init_app(app)
Adding view
-----------
......@@ -73,9 +77,8 @@ Now, lets add a view. To do this, you need to derive from `BaseView` class::
app = Flask(__name__)
admin = Admin()
admin = Admin(app)
admin.add_view(MyView(name='Hello'))
admin.setup_app(app)
app.run()
......@@ -133,9 +136,8 @@ If you want to generate URL to the particular view method from outside, followin
1. You have ability to override endpoint name by passing `endpoint` parameter to the view class
constructor::
admin = Admin()
admin = Admin(app)
admin.add_view(MyView(endpoint='testadmin'))
admin.setup_app(app)
In this case, you can generate links by concatenating view method name with a endpoint::
......@@ -160,9 +162,8 @@ Flask-AdminEx comes with built-in SQLAlchemy model administrative interface. It
# Flask and Flask-SQLAlchemy initialization here
admin = Admin()
admin = Admin(app)
admin.add_view(ModelBase(User, db.session))
admin.setup_app(app)
This will create administrative interface for `User` model with default settings.
......@@ -184,10 +185,8 @@ you can do something like this::
def __init__(self, session):
__super__(MyView, self).__init__(User, session)
admin = Admin()
admin = Admin(app)
admin.add_view(UserView(db.session))
admin.setup_app(app)
It is very easy to add support for different database backends (Mongo, etc) by inheriting from `BaseModelView`
class and implementing database-related methods.
......@@ -207,11 +206,10 @@ Here is simple example::
# Flask setup here
admin = Admin()
admin = Admin(app)
path = op.join(op.dirname(__file__), 'static')
admin.add_view(path, '/static/', name='Static Files')
admin.setup_app(app)
You can disable uploads, disable file or directory deletion, restrict file uploads to certain types and so on.
Check :mod:`flask.ext.adminex.ext.fileadmin` documentation on how to do it.
......
......@@ -137,14 +137,11 @@ if __name__ == '__main__':
init_login()
# Create admin
admin = adminex.Admin('Auth', index_view=MyAdminIndexView())
admin = adminex.Admin(app, 'Auth', index_view=MyAdminIndexView())
# Add view
admin.add_view(MyModelView(User, db.session))
# Associate with an app
admin.setup_app(app)
# Create DB
db.create_all()
......
......@@ -29,9 +29,8 @@ if __name__ == '__main__':
pass
# Create admin interface
admin = adminex.Admin()
admin = adminex.Admin(app)
admin.add_view(fileadmin.FileAdmin(path, '/files/', name='Files'))
admin.setup_app(app)
# Start app
app.debug = True
......
......@@ -31,10 +31,9 @@ def index():
if __name__ == '__main__':
# Create admin interface
admin = adminex.Admin()
admin = adminex.Admin(app)
admin.add_view(MyAdminView(category='Test'))
admin.add_view(AnotherAdminView(category='Test'))
admin.setup_app(app)
# Start app
app.debug = True
......
......@@ -70,15 +70,12 @@ class PostAdmin(sqlamodel.ModelView):
if __name__ == '__main__':
# Create admin
admin = adminex.Admin('Simple Models')
admin = adminex.Admin(app, 'Simple Models')
# Add views
admin.add_view(sqlamodel.ModelView(User, db.session))
admin.add_view(PostAdmin(db.session))
# Associate with an app
admin.setup_app(app)
# Create DB
db.create_all()
......
......@@ -264,10 +264,12 @@ class Admin(object):
"""
Collection of the views. Also manages menu structure.
"""
def __init__(self, name=None, url=None, index_view=None):
def __init__(self, app=None, name=None, url=None, index_view=None):
"""
Constructor.
`app`
Flask application object
`name`
Application name. Will be displayed in main menu and as a page title. If not provided, defaulted to "Admin"
`index_view`
......@@ -275,6 +277,7 @@ class Admin(object):
"""
self._views = []
self._menu = []
self._menu_categories = dict()
if name is None:
name = 'Admin'
......@@ -287,6 +290,8 @@ class Admin(object):
if index_view is None:
index_view = AdminIndexView()
self.app = app
# Add predefined index view
self.add_view(index_view)
......@@ -297,40 +302,41 @@ class Admin(object):
`view`
View to add.
"""
# Add to views
self._views.append(view)
def setup_app(self, app):
# Update menu
if view.category:
category = self._menu_categories.get(view.category)
if category is None:
category = MenuItem(view.category)
self._menu_categories[view.category] = category
self._menu.append(category)
category.add_child(MenuItem(view.name, view))
else:
self._menu.append(MenuItem(view.name, view))
# If app was provided in constructor, register view with Flask app
if self.app is not None:
self.app.register_blueprint(view.create_blueprint(self))
def init_app(self, app):
"""
Register all views with Flask application.
`app`
Flask application instance
"""
if self.app is not None:
raise Exception('Flask-AdminEx is already associated with an application.')
self.app = app
for v in self._views:
app.register_blueprint(v.create_blueprint(self))
self._refresh_menu()
def _refresh_menu(self):
categories = dict()
self._menu = []
for v in self._views:
if v.category is None:
self._menu.append(MenuItem(v.name, v))
else:
category = categories.get(v.category)
if category is None:
category = MenuItem(v.category)
categories[v.category] = category
self._menu.append(category)
category.add_child(MenuItem(v.name, v))
def menu(self):
"""
Return menu hierarchy.
......
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