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

Merge branch 'master' of github.com:mrjoes/flask-admin

parents b7f53eb9 551aad3c
......@@ -8,15 +8,14 @@ Flask-Admin
Introduction
------------
This is library for building adminstrative interface on top of Flask framework.
This is a library for building an administrative interface on top of the Flask framework.
Flask-Admin comes with few batteries out of the box: model scaffolding for `SQLAlchemy <http://www.sqlalchemy.org/>`_,
`MongoEngine <http://mongoengine.org/>`_ and `Peewee <https://github.com/coleifer/peewee>`_ ORMs and
simple file management interface.
Flask-Admin comes with a few batteries included: model scaffolding for `SQLAlchemy <http://www.sqlalchemy.org/>`_,
`MongoEngine <http://mongoengine.org/>`_ and `Peewee <https://github.com/coleifer/peewee>`_ ORMs as well as a simple file management interface.
However, you're not limited by provided functionality - instead of providing simple scaffolding for the ORM
models, Flask-Admin provides tools that can be used to build adminstrative interface of any complexity,
using consistent look and feel.
You're not limited by the default functionality - instead of providing simple scaffolding for the ORM
models, Flask-Admin provides tools that can be used to construct administrative interfaces of any complexity,
using a consistent look and feel.
Documentation
-------------
......@@ -26,7 +25,7 @@ Flask-Admin is extensively documented, you can find `documentation here <http://
3rd Party Stuff
---------------
Flask-Admin is built with help of `Twitter Bootstrap <http://twitter.github.com/bootstrap/>`_ and `Select2 <https://github.com/ivaynberg/select2>`_.
Flask-Admin is built with the help of `Twitter Bootstrap <http://twitter.github.com/bootstrap/>`_ and `Select2 <https://github.com/ivaynberg/select2>`_.
Kudos
-----
......@@ -36,4 +35,4 @@ Some ideas were taken from the `Flask-Admin <https://github.com/wilsaj/flask-adm
Examples
--------
Library comes with few examples, you can find them in `examples` directory.
The library comes with a few examples, you can find them in the `examples` directory.
......@@ -29,10 +29,10 @@ class ActionsMixin(object):
Actions mixin.
In some cases, you might work with more than one "entity" (model, file, etc) in
your admin view and will want to perform actions on group of entities at once.
your admin view and will want to perform actions on a group of entities simultaneously.
In this case, you can add this functionality by doing this:
1. Add mixin to your administrative view class
1. Add this mixin to your administrative view class
2. Call `init_actions` in your class constructor
3. Expose actions view
4. Import `actions.html` library and add call library macros in your template
......@@ -76,7 +76,7 @@ class ActionsMixin(object):
def get_actions_list(self):
"""
Return list and a dictionary of allowed actions.
Return a list and a dictionary of allowed actions.
"""
actions = []
actions_confirmation = {}
......
......@@ -107,7 +107,7 @@ class BaseView(object):
arguments you want to pass to the template and call parent view.
These arguments are local for this request and will be discarded
in next request.
in the next request.
Any value passed through ``_template_args`` will override whatever
parent view function passed to the template.
......@@ -133,17 +133,17 @@ class BaseView(object):
Constructor.
:param name:
Name of this view. If not provided, will be defaulted to the class name.
Name of this view. If not provided, will default to the class name.
:param category:
View category. If not provided, will be shown as a top-level menu item. Otherwise, will
View category. If not provided, this view will be shown as a top-level menu item. Otherwise, it will
be in a submenu.
:param endpoint:
Base endpoint name for the view. For example, if there's view method called "index" and
endpoint was set to "myadmin", you can use `url_for('myadmin.index')` to get URL to the
view method. By default, equals to the class name in lower case.
Base endpoint name for the view. For example, if there's a view method called "index" and
endpoint is set to "myadmin", you can use `url_for('myadmin.index')` to get the URL to the
view method. Defaults to the class name in lower case.
:param url:
Base URL. If provided, affects how URLs are generated. For example, if url parameter
equals to "test", resulting URL will look like "/admin/test/". If not provided, will
Base URL. If provided, affects how URLs are generated. For example, if the url parameter
is "test", the resulting URL will look like "/admin/test/". If not provided, will
use endpoint as a base url. However, if URL starts with '/', absolute path is assumed
and '/admin/' prefix won't be applied.
"""
......@@ -230,7 +230,7 @@ class BaseView(object):
def _prettify_name(self, name):
"""
Prettify class name by splitting name by capital characters. So, 'MySuperClass' will look like 'My Super Class'
Prettify a class name by splitting the name on capitalized characters. So, 'MySuperClass' becomes 'My Super Class'
:param name:
String to prettify
......@@ -241,10 +241,10 @@ class BaseView(object):
"""
Override this method to add permission checks.
Flask-Admin does not make any assumptions about authentication system used in your application, so it is
up for you to implement it.
Flask-Admin does not make any assumptions about the authentication system used in your application, so it is
up to you to implement it.
By default, it will allow access for the everyone.
By default, it will allow access for everyone.
"""
return True
......@@ -252,7 +252,7 @@ class BaseView(object):
"""
This method will be executed before calling any view method.
By default, it will check if admin class is accessible and if it is not - will
By default, it will check if the admin class is accessible and if it is not it will
throw HTTP 404 error.
:param name:
......@@ -278,10 +278,10 @@ class AdminIndexView(BaseView):
admin = Admin(index_view=MyHomeView())
Default values for the index page are following:
Default values for the index page are:
* If name is not provided, 'Home' will be used.
* If endpoint is not provided, will use ``admin``
* If a name is not provided, 'Home' will be used.
* If an endpoint is not provided, will default to ``admin``
* Default URL route is ``/admin``.
* Automatically associates with static folder.
* Default template is ``admin/index.html``
......@@ -351,7 +351,7 @@ class MenuItem(object):
class Admin(object):
"""
Collection of the views. Also manages menu structure.
Collection of the admin views. Also manages menu structure.
"""
def __init__(self, app=None, name=None,
url=None, subdomain=None,
......@@ -364,19 +364,19 @@ class Admin(object):
:param app:
Flask application object
:param 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 the main menu and as a page title. Defaults to "Admin"
:param url:
Base URL
:param subdomain:
Subdomain to use
:param index_view:
Home page view to use. If not provided, will use `AdminIndexView`.
Home page view to use. Defaults to `AdminIndexView`.
:param translations_path:
Location of the translation message catalogs. By default will use translations
shipped with the Flask-Admin.
Location of the translation message catalogs. By default will use the translations
shipped with Flask-Admin.
:param endpoint:
Base endpoint name for index view. If you use multiple instances of `Admin` class with
one Flask application, you have to set unique endpoint name for each instance.
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.app = app
......@@ -407,7 +407,7 @@ class Admin(object):
def add_view(self, view):
"""
Add view to the collection.
Add a view to the collection.
:param view:
View to add.
......@@ -422,7 +422,7 @@ class Admin(object):
def locale_selector(self, f):
"""
Installs locale selector for current ``Admin`` instance.
Installs a locale selector for the current ``Admin`` instance.
Example::
......@@ -453,7 +453,7 @@ class Admin(object):
def _add_view_to_menu(self, view):
"""
Add view to the menu tree
Add a view to the menu tree
:param view:
View to add
......@@ -472,7 +472,7 @@ class Admin(object):
def init_app(self, app):
"""
Register all views with Flask application.
Register all views with the Flask application.
:param app:
Flask application instance
......@@ -506,6 +506,6 @@ class Admin(object):
def menu(self):
"""
Return menu hierarchy.
Return the menu hierarchy.
"""
return self._menu
......@@ -162,7 +162,7 @@ class FileAdmin(BaseView, ActionsMixin):
:param base_url:
Base URL for the files
:param name:
Name of this view. If not provided, will be defaulted to the class name.
Name of this view. If not provided, will default to the class name.
:param category:
View category
:param endpoint:
......@@ -171,7 +171,7 @@ class FileAdmin(BaseView, ActionsMixin):
URL for view
:param verify_path:
Verify if path exists. If set to `True` and path does not exist
will throw exception.
will raise an exception.
"""
self.base_path = base_path
self.base_url = base_url
......@@ -198,7 +198,7 @@ class FileAdmin(BaseView, ActionsMixin):
def is_accessible_path(self, path):
"""
Verify if path is accessible for current user.
Verify if the provided path is accessible for the current user.
Override to customize behavior.
......@@ -242,7 +242,7 @@ class FileAdmin(BaseView, ActionsMixin):
def is_file_editable(self, filename):
"""
Verify if file can be edited.
Determine if the file can be edited.
Override to customize behavior.
......@@ -261,7 +261,7 @@ class FileAdmin(BaseView, ActionsMixin):
def is_in_folder(self, base_path, directory):
"""
Verify if `directory` is in `base_path` folder
Verify that `directory` is in `base_path` folder
:param base_path:
Base directory path
......@@ -319,9 +319,9 @@ class FileAdmin(BaseView, ActionsMixin):
"""
Verify and normalize path.
If path is not relative to the base directory, will throw 404 exception.
If the path is not relative to the base directory, will raise a 404 exception.
If path does not exist, will also throw 404 exception.
If the path does not exist, this will also raise a 404 exception.
"""
base_path = self.get_base_path()
......@@ -353,7 +353,7 @@ class FileAdmin(BaseView, ActionsMixin):
Index view method
:param path:
Optional directory path. If not provided, will use base directory
Optional directory path. If not provided, will use the base directory
"""
# Get path and verify if it is valid
base_path, directory, path = self._normalize_path(path)
......@@ -406,7 +406,7 @@ class FileAdmin(BaseView, ActionsMixin):
Upload view method
:param path:
Optional directory path. If not provided, will use base directory
Optional directory path. If not provided, will use the base directory
"""
# Get path and verify if it is valid
base_path, directory, path = self._normalize_path(path)
......@@ -439,7 +439,7 @@ class FileAdmin(BaseView, ActionsMixin):
Directory creation view method
:param path:
Optional directory path. If not provided, will use base directory
Optional directory path. If not provided, will use the base directory
"""
# Get path and verify if it is valid
base_path, directory, path = self._normalize_path(path)
......
......@@ -75,7 +75,7 @@ class ModelView(BaseModelView):
"""
Field to filter converter.
Override this attribute to use non-default converter.
Override this attribute to use a non-default converter.
"""
column_type_formatters = DEFAULT_FORMATTERS
......@@ -123,7 +123,7 @@ class ModelView(BaseModelView):
def get_pk_value(self, model):
"""
Return primary key value from the model instance
Return the primary key value from the model instance
:param model:
Model instance
......@@ -154,7 +154,7 @@ class ModelView(BaseModelView):
def scaffold_sortable_columns(self):
"""
Return sortable columns dictionary (name, field)
Return a dictionary of sortable columns (name, field)
"""
columns = {}
......@@ -222,7 +222,7 @@ class ModelView(BaseModelView):
def is_valid_filter(self, filter):
"""
Validate if it is valid MongoEngine filter
Validate if the provided filter is a valid MongoEngine filter
:param filter:
Filter object
......@@ -254,7 +254,7 @@ class ModelView(BaseModelView):
:param search:
Search criteria
:param filters:
List of applied fiters
List of applied filters
:param execute:
Run query immediately or not
"""
......@@ -307,7 +307,7 @@ class ModelView(BaseModelView):
def get_one(self, id):
"""
Return single model instance by ID
Return a single model instance by its ID
:param id:
Model ID
......
......@@ -3,7 +3,7 @@ from peewee import PrimaryKeyField
def get_primary_key(model):
for n, f in model._meta.get_sorted_fields():
if type(f) == PrimaryKeyField:
if type(f) == PrimaryKeyField or f.primary_key:
return n
......
......@@ -18,7 +18,7 @@ from .typefmt import DEFAULT_FORMATTERS
class ModelView(BaseModelView):
"""
SQLALchemy model view
SQLAlchemy model view
Usage sample::
......@@ -90,7 +90,7 @@ class ModelView(BaseModelView):
class MyModelView(ModelView):
column_searchable_list = (User.name, User.email)
Following search rules apply:
The following search rules apply:
- If you enter *ZZZ* in the UI search field, it will generate *ILIKE '%ZZZ%'*
statement against searchable columns.
......@@ -103,8 +103,8 @@ class ModelView(BaseModelView):
- If you prefix your search term with ^, it will find all rows
that start with ^. So, if you entered *^ZZZ*, *ILIKE 'ZZZ%'* will be used.
- If you prefix your search term with =, it will do exact match.
For example, if you entered *=ZZZ*, *ILIKE 'ZZZ'* statement will be used.
- If you prefix your search term with =, it will perform an exact match.
For example, if you entered *=ZZZ*, the statement *ILIKE 'ZZZ'* will be used.
"""
column_filters = None
......@@ -163,11 +163,11 @@ class ModelView(BaseModelView):
"""
If set to `False` and user deletes more than one model using built in action,
all models will be read from the database and then deleted one by one
giving SQLAlchemy chance to manually cleanup any dependencies (many-to-many
giving SQLAlchemy a chance to manually cleanup any dependencies (many-to-many
relationships, etc).
If set to `True`, will run `DELETE` statement which is somewhat faster,
but might leave corrupted data if you forget to configure `DELETE
If set to `True`, will run a `DELETE` statement which is somewhat faster,
but may leave corrupted data if you forget to configure `DELETE
CASCADE` for your model.
"""
......@@ -195,9 +195,9 @@ class ModelView(BaseModelView):
class MyModelView(ModelView):
inline_models = (MyInlineModelForm(MyInlineModel),)
You can customize generated field name by:
You can customize the generated field name by:
1. Using `form_name` property as option:
1. Using the `form_name` property as a key to the options dictionary:
class MyModelView(ModelView):
inline_models = ((Post, dict(form_label='Hello')))
......@@ -226,15 +226,15 @@ class ModelView(BaseModelView):
:param model:
Model class
:param session:
SQLALchemy session
SQLAlchemy session
:param name:
View name. If not set, will default to model name
View name. If not set, defaults to the model name
:param category:
Category name
:param endpoint:
Endpoint name. If not set, will default to model name
Endpoint name. If not set, defaults to the model name
:param url:
Base URL. If not set, will default to '/admin/' + endpoint
Base URL. If not set, defaults to '/admin/' + endpoint
"""
self.session = session
......@@ -270,19 +270,19 @@ class ModelView(BaseModelView):
# Scaffolding
def scaffold_pk(self):
"""
Return primary key name from a model
Return the primary key name from a model
"""
return tools.get_primary_key(self.model)
def get_pk_value(self, model):
"""
Return PK value from a model object.
Return the PK value from a model object.
"""
return getattr(model, self._primary_key)
def scaffold_list_columns(self):
"""
Return list of columns from the model.
Return a list of columns from the model.
"""
columns = []
......@@ -307,7 +307,7 @@ class ModelView(BaseModelView):
def scaffold_sortable_columns(self):
"""
Return dictionary of sortable columns.
Return a dictionary of sortable columns.
Key is column name, value is sort column/field.
"""
columns = dict()
......@@ -321,7 +321,7 @@ class ModelView(BaseModelView):
column = p.columns[0]
# Can't sort by on primary and foreign keys by default
# Can't sort on primary or foreign keys by default
if column.foreign_keys:
continue
......@@ -382,7 +382,7 @@ class ModelView(BaseModelView):
def is_text_column_type(self, name):
"""
Verify if column type is text-based.
Verify if the provided column type is text-based.
:returns:
``True`` for ``String``, ``Unicode``, ``Text``, ``UnicodeText``
......@@ -478,7 +478,7 @@ class ModelView(BaseModelView):
def is_valid_filter(self, filter):
"""
Verify that provided filter object is derived from the
Verify that the provided filter object is derived from the
SQLAlchemy-compatible filter class.
:param filter:
......@@ -521,7 +521,7 @@ class ModelView(BaseModelView):
def scaffold_auto_joins(self):
"""
Return list of joined tables by going through the
Return a list of joined tables by going through the
displayed columns.
"""
if not self.column_auto_select_related:
......@@ -668,7 +668,7 @@ class ModelView(BaseModelView):
def get_one(self, id):
"""
Return one model by its id.
Return a single model by its id.
:param id:
Model id
......
......@@ -14,17 +14,17 @@ class BaseModelView(BaseView, ActionsMixin):
"""
Base model view.
View does not make any assumptions on how models are stored or managed, but expects following:
This view does not make any assumptions on how models are stored or managed, but expects the following:
1. Model is an object
2. Model contains properties
3. Each model contains attribute which uniquely identifies it (i.e. primary key for database model)
4. You can get list of sorted models with pagination applied from a data source
1. The provided model is an object
2. The model contains properties
3. Each model contains an attribute which uniquely identifies it (i.e. a primary key for a database model)
4. It is possible to retrieve a list of sorted models with pagination applied from a data source
5. You can get one model by its identifier from the data source
Essentially, if you want to support new data store, all you have to do:
Essentially, if you want to support a new data store, all you have to do is:
1. Derive from `BaseModelView` class
1. Derive from the `BaseModelView` class
2. Implement various data-related methods (`get_list`, `get_one`, `create_model`, etc)
3. Implement automatic form generation from the model representation (`scaffold_form`)
"""
......@@ -81,7 +81,7 @@ class BaseModelView(BaseView, ActionsMixin):
class MyModelView(BaseModelView):
column_formatters = dict(price=lambda c, m, p: m.price*2)
Callback function has following prototype::
The Callback function has the prototype::
def formatter(context, model, name):
# context is instance of jinja2.runtime.Context
......@@ -92,19 +92,19 @@ class BaseModelView(BaseView, ActionsMixin):
column_type_formatters = ObsoleteAttr('column_type_formatters', 'list_type_formatters', None)
"""
Dictionary of value type formatters to be used in list view.
Dictionary of value type formatters to be used in the list view.
By default, two types are formatted:
1. ``None`` will be displayed as empty string
2. ``bool`` will be displayed as check if it is ``True``
1. ``None`` will be displayed as an empty string
2. ``bool`` will be displayed as a checkmark if it is ``True``
If you don't like default behavior and don't want any type formatters
applied, just override this property with empty dictionary::
If you don't like the default behavior and don't want any type formatters
applied, just override this property with an empty dictionary::
class MyModelView(BaseModelView):
column_type_formatters = dict()
If you want to display `NULL` instead of empty string, you can do
If you want to display `NULL` instead of an empty string, you can do
something like this::
from flask.ext.admin import typefmt
......@@ -155,13 +155,13 @@ class BaseModelView(BaseView, ActionsMixin):
column_sortable_list = ('name', 'last_name')
If you want to explicitly specify field/column to be used while
sorting, you can use tuple::
sorting, you can use a tuple::
class MyModelView(BaseModelView):
column_sortable_list = ('name', ('user', 'user.username'))
When using SQLAlchemy models, model attributes can be used instead
of the string::
of strings::
class MyModelView(BaseModelView):
column_sortable_list = ('name', ('user', User.username))
......@@ -171,9 +171,9 @@ class BaseModelView(BaseView, ActionsMixin):
'searchable_columns',
None)
"""
Collection of the searchable columns. It is assumed that only
text-only fields are searchable, but it is up for a model
implementation to make decision.
A collection of the searchable columns. It is assumed that only
text-only fields are searchable, but it is up to the model
implementation to decide.
Example::
......@@ -197,7 +197,7 @@ class BaseModelView(BaseView, ActionsMixin):
'list_display_pk',
False)
"""
Controls if primary key should be displayed in list view.
Controls if the primary key should be displayed in the list view.
"""
form = None
......@@ -274,7 +274,7 @@ class BaseModelView(BaseView, ActionsMixin):
# Various settings
page_size = 20
"""
Default page size.
Default page size for pagination.
"""
def __init__(self, model,
......@@ -285,11 +285,11 @@ class BaseModelView(BaseView, ActionsMixin):
:param model:
Model class
:param name:
View name. If not provided, will use model class name
View name. If not provided, will use the model class name
:param category:
View category
:param endpoint:
Base endpoint. If not provided, will use model name + 'view'.
Base endpoint. If not provided, will use the model name + 'view'.
For example if model name was 'User', endpoint will be
'userview'
:param url:
......@@ -383,7 +383,7 @@ class BaseModelView(BaseView, ActionsMixin):
def get_column_name(self, field):
"""
Return human-readable column name.
Return a human-readable column name.
:param field:
Model field name.
......@@ -395,9 +395,9 @@ class BaseModelView(BaseView, ActionsMixin):
def get_list_columns(self):
"""
Returns list of the model field names. If `column_list` was
Returns a list of the model field names. If `column_list` was
set, returns it. Otherwise calls `scaffold_list_columns`
to generate list from the model.
to generate the list from the model.
"""
columns = self.column_list
......@@ -415,14 +415,14 @@ class BaseModelView(BaseView, ActionsMixin):
Returns dictionary of sortable columns. Must be implemented in
the child class.
Expected return format is dictionary, where key is field name and
value is property name.
Expected return format is a dictionary, where keys are field names and
values are property names.
"""
raise NotImplemented('Please implement scaffold_sortable_columns method')
def get_sortable_columns(self):
"""
Returns dictionary of the sortable columns. Key is a model
Returns a dictionary of the sortable columns. Key is a model
field name and value is sort column (for example - attribute).
If `column_sortable_list` is set, will use it. Otherwise, will call
......@@ -459,10 +459,10 @@ class BaseModelView(BaseView, ActionsMixin):
def is_valid_filter(self, filter):
"""
Verify that provided filter object is valid.
Verify that the provided filter object is valid.
Override in model backend implementation to verify if
provided filter type is allowed.
the provided filter type is allowed.
:param filter:
Filter object to verify.
......@@ -471,7 +471,7 @@ class BaseModelView(BaseView, ActionsMixin):
def get_filters(self):
"""
Return list of filter objects.
Return a list of filter objects.
If your model backend implementation does not support filters,
override this method and return `None`.
......@@ -568,10 +568,9 @@ class BaseModelView(BaseView, ActionsMixin):
# Database-related API
def get_list(self, page, sort_field, sort_desc, search, filters):
"""
Return list of models from the data source with applied pagination
and sorting.
Must be implemented in child class.
Return a paginated and sorted list of models from the data source.
Must be implemented in the child class.
:param page:
Page number, 0 based. Can be set to None if it is first page.
......@@ -601,7 +600,7 @@ class BaseModelView(BaseView, ActionsMixin):
# Model handlers
def on_model_change(self, form, model):
"""
Allow to do some actions after a model was created or updated.
Perform some actions after a model is created or updated.
Called from create_model and update_model in the same transaction
(if it has any meaning for a store backend).
......@@ -612,7 +611,7 @@ class BaseModelView(BaseView, ActionsMixin):
def on_model_delete(self, model):
"""
Allow to do some actions before a model will be deleted.
Perform some actions before a model is deleted.
Called from delete_model in the same transaction
(if it has any meaning for a store backend).
......@@ -753,7 +752,7 @@ class BaseModelView(BaseView, ActionsMixin):
Override this method to allow or disallow actions based
on some condition.
Default implementation only checks if particular action
The default implementation only checks if the particular action
is not in `action_disallowed_list`.
"""
return name not in self.action_disallowed_list
......@@ -761,7 +760,7 @@ class BaseModelView(BaseView, ActionsMixin):
@contextfunction
def get_list_value(self, context, model, name):
"""
Returns value to be displayed in list view
Returns the value to be displayed in the list view
:param context:
:py:class:`jinja2.runtime.Context`
......
......@@ -30,7 +30,7 @@ def import_attribute(name):
:param name:
String reference.
Throws ImportError or AttributeError if module or attribute do not exist.
Raises ImportError or AttributeError if module or attribute do not exist.
Example::
......@@ -80,7 +80,7 @@ def rec_getattr(obj, attr, default=None):
def get_dict_attr(obj, attr, default=None):
"""
Get attibute of the object without triggering its __getattr__.
Get attribute of the object without triggering its __getattr__.
:param obj:
Object
......@@ -98,7 +98,7 @@ def get_dict_attr(obj, attr, default=None):
def get_property(obj, name, old_name, default=None):
"""
Check if old property name exists and if it is - show warning message
Check if old property name exists and if it does - show warning message
and return value.
Otherwise, return new property value
......
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