Commit 532be52b authored by Serge S. Koval's avatar Serge S. Koval

Merge pull request #352 from petrus-jvrensburg/docs

More updates to Docs.
parents 71799b60 eeacef04
Localization Localization
============ ============
Flask-Admin uses the `Flask-BabelEx <http://github.com/mrjoes/flask-babelex/>`_ package to handle translations. Flask-Admin makes it possible for you to serve your application in more than one language. To do this, it makes use of
the `Flask-BabelEx <http://github.com/mrjoes/flask-babelex/>`_ package for handling translations. This package is a
Flask-BabelEx is a fork of Flask-Babel with following features: fork of the popular `Flask-Babel <http://github.com/mitshuhiko/flask-babel/>`_ package, with the following features:
1. It is API-compatible with Flask-Babel 1. It is API-compatible with Flask-Babel
2. It allows distribution of translations with Flask extensions 2. It allows distribution of translations with Flask extensions
3. Much more configurable 3. It aims to be more configurable than Flask-Babel
If Flask-Admin can not import Flask-BabelEx, it disables localization support completely. Currently *Flask-BabelEx* is the only supported way of enabling localization support in Flask-Admin.
How to enable localization How to enable localization
-------------------------- --------------------------
1. Initialize Flask-BabelEx by creating instance of `Babel` class:: 1. Install Flask-BabelEx::
pip install flask-babelex
2. Initialize Flask-BabelEx by creating instance of `Babel` class::
from flask import app from flask import app
from flask.ext.babelex import Babel from flask.ext.babelex import Babel
...@@ -22,7 +26,7 @@ How to enable localization ...@@ -22,7 +26,7 @@ How to enable localization
app = Flask(__name__) app = Flask(__name__)
babel = Babel(app) babel = Babel(app)
2. Create locale selector function:: 3. Create a locale selector function::
@babel.localeselector @babel.localeselector
def get_locale(): def get_locale():
...@@ -30,7 +34,9 @@ How to enable localization ...@@ -30,7 +34,9 @@ How to enable localization
# user profile, cookie, session, etc. # user profile, cookie, session, etc.
return 'en' return 'en'
3. Initialize Flask-Admin as usual. 4. Initialize Flask-Admin as usual.
You can check `babel` example to see localization in action. To change locale, add *?en=<locale name>* to the URL. For example, URL You can check the `babel` example to see localization in action. When running this example, you can change the
can look like: `http://localhost:5000/admin/userview/?lang=fr <http://localhost:5000/admin/userview/?lang=fr>`_. locale simply by adding a query parameter, like *?en=<locale name>* to the URL. For example, a French version of
the application should be accessible at:
`http://localhost:5000/admin/userview/?lang=fr <http://localhost:5000/admin/userview/?lang=fr>`_.
...@@ -165,7 +165,6 @@ have access to the view in question:: ...@@ -165,7 +165,6 @@ have access to the view in question::
def is_accessible(self): def is_accessible(self):
return login.current_user.is_authenticated() return login.current_user.is_authenticated()
You can also implement policy-based security, conditionally allowing or disallowing access to parts of the You can also implement policy-based security, conditionally allowing or disallowing access to parts of the
administrative interface. If a user does not have access to a particular view, the menu item won't be visible. administrative interface. If a user does not have access to a particular view, the menu item won't be visible.
......
...@@ -4,38 +4,42 @@ Usage Tips ...@@ -4,38 +4,42 @@ Usage Tips
General tips General tips
------------ ------------
1. Whenever your administrative views share common functionality such as authentication, 1. A reasonably obvious, but very useful, pattern is to wrap any shared functionality that your different admin views
form validation, make use of read-only views and so on - create your own base class which might need into a base class that they can all inherit from (to help you keep things
inherits from proper Flask-Admin view class. `DRY <http://en.wikipedia.org/wiki/Don't_repeat_yourself>`_).
For example, if you need to check user permissions for every call, don't implement For example, rather than manually checking user permissions in each of your admin views, you can implement a
`is_accessible` in every administrative view. Create your own base class, implement base class such as ::
`is_accessible` there and use this class for all your views.
2. You can override used templates either by using `ModelView` properties (such as class MyView(BaseView):
`list_template`, `create_template`, `edit_template`) or def is_accessible(self):
putting customized version of the template into your `templates/admin/` directory return login.current_user.is_authenticated()
3. If you need to customize look and feel of model forms, there are two options: and every view that inherits from this, will have the permission checking done automatically. The important thing
- Override create/edit template to notice, is that your base class needs to inherit from a built-in Flask-Admin view.
- Use new :mod:`flask.ext.admin.form.rules` form rendering rules
4. Flask-Admin has that manage file/image uploads and store result in model field. You can 2. You can override a default template either by passing the path to your own template in to the relevant `ModelView`
find documentation here :mod:`flask.ext.admin.form.upload`. property (either `list_template`, `create_template` or `edit_template`) or by putting your own customized
version of a default template into your `templates/admin/` directory.
5. If you don't want to use Flask-Admin form scaffolding logic, you can override 3. To customize the overall look and feel of the default model forms, you have two options: Either, you could
:meth:`~flask.ext.admin.model.base.scaffold_form` and put your own form creation override the default create/edit templates. Or, alternatively, you could make use of the form rendering rules
logic there. For example, if you use `WTForms-Alchemy <https://github.com/kvesteri/wtforms-alchemy>`_, all you have to do (:mod:`flask.ext.admin.form.rules`) that were introduced in version 1.0.7.
is to put appropriate form generation code into your `ModelView` class into the
`scaffold_form` method. 4. To simplify the management of file uploads, Flask-Admin comes with a dedicated tool, for which you can find
documentation at: :mod:`flask.ext.admin.form.upload`.
5. If you don't want to the use the built-in Flask-Admin form scaffolding logic, you are free to roll your own
by simply overriding :meth:`~flask.ext.admin.model.base.scaffold_form`. For example, if you use
`WTForms-Alchemy <https://github.com/kvesteri/wtforms-alchemy>`_, you could put your form generation code
into a `scaffold_form` method in your `ModelView` class.
SQLAlchemy SQLAlchemy
---------- ----------
1. If `synonym_property` does not return SQLAlchemy field, Flask-Admin 1. If the `synonym_property` does not return a SQLAlchemy field, then Flask-Admin won't be able to figure out what to
won't be able to figure out what to do with it and won't generate form do with it, so it won't generate a form field. In this case, you would need to manually contribute your own field::
field. In this case, you need to manually contribute field::
class MyView(ModelView): class MyView(ModelView):
def scaffold_form(self): def scaffold_form(self):
...@@ -46,5 +50,5 @@ SQLAlchemy ...@@ -46,5 +50,5 @@ SQLAlchemy
MongoEngine MongoEngine
----------- -----------
1. Flask-Admin supports GridFS backed image and file uploads. Done through 1. Flask-Admin supports GridFS-backed image- and file uploads, done through WTForms fields. Documentation can be found
WTForms fields and documentation can be found here :mod:`flask.ext.admin.contrib.mongoengine.fields`. at :mod:`flask.ext.admin.contrib.mongoengine.fields`.
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