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