欢迎来到 Flask_Dashed 的文档!

介绍

Flask-Dashed 提供构建简单以及具有扩展性的管理界面的工具。

在线演示: http://flask-dashed.jeanphi.fr/ (需要 Github 账号)。

列表视图:

_images/screen.png

表单视图:

_images/screen-edit.png

安装

pip install Flask-Dashed

最小的使用

代码:

from flask import Flask
from flask_dashed.admin import Admin

app = Flask(__name__)
admin = Admin(app)

if __name__ == '__main__':
    app.run()

示例应用程序: http://github.com/jeanphix/flask-dashed-demo

安全地处理

保护(“武装”)所有模块端:

from flask import session

book_module = admin.register_module(BookModule, '/books', 'books',
    'book management')

@book_module.secure(http_code=401)
def login_required():
    return "user" in session

保护(“武装”)特定的模块端:

@book_module.secure_endpoint('edit', http_code=403)
def check_edit_credential(view):
    # I'm now signed in, may I modify the ressource?
    return session.user.can_edit_book(view.object)

模块组织

由于管理节点( node )注册到一个“树”,因此很容易地管理它们。:

library = admin.register_node('/library', 'library', my library)
book_module = admin.register_module(BookModule, '/books', 'books',
    'book management', parent=library)

为了满足你的需求,导航和面包屑会自动地建立。子模块的安全将会继承自父模块。

SQLALchemy 扩展

Code:

from flask_dashed.ext.sqlalchemy import ModelAdminModule

class BookModule(ModelAdminModule):
    model = Book
    db_session = db.session

book_module = admin.register_module(BookModule, '/books', 'books',
    'book management')

Api

Admin 对象

class admin.Admin(app, url_prefix='/admin', title='flask-dashed', main_dashboard=None, endpoint='admin')

Class that provides a way to add admin interface to Flask applications.

Parameters:

  • app – The Flask application
  • url_prefix – The url prefix
  • main_dashboard – The main dashboard object
  • endpoint – The endpoint

add_path_security(path, function, http_code=403)

Registers security function for given path.

Parameters:

  • path – The endpoint to secure
  • function – The security function
  • http_code – The response http code

check_path_security(path)

Checks security for specific and path.

Parameters: path – The path to check

register_module(module_class, url_prefix, endpoint, short_title, title=None, parent=None)

Registers new module to current admin.

register_node(url_prefix, endpoint, short_title, title=None, parent=None, node_class=<class 'admin.AdminNode'>)

Registers admin node.

Parameters:

  • url_prefix – The url prefix
  • endpoint – The endpoint
  • short_title – The short title
  • title – The long title
  • parent – The parent node path
  • node_class – The class for node objects

Admin 模块

admin.recursive_getattr(obj, attr)

Returns object related attributes, as it’s a template filter None is return when attribute doesn’t exists.

eg:

a = object()
a.b = object()
a.b.c = 1
recursive_getattr(a, 'b.c') => 1
recursive_getattr(a, 'b.d') => None

class admin.AdminNode(admin, url_prefix, endpoint, short_title, title=None, parent=None)

An AdminNode just act as navigation container, it doesn’t provide any rules.

Parameters:

  • admin – The parent admin object
  • url_prefix – The url prefix
  • enpoint – The endpoint
  • short_title – The short module title use on navigation & breadcrumbs
  • title – The long title
  • parent – The parent node

parents

Returns all parent hierarchy as list. Usefull for breadcrumbs.

secure(http_code=403)

Gives a way to secure specific url path.

Parameters: http_code – The response http code when False

url_path

Returns the url path relative to admin one.

class admin.AdminModule(*args, **kwargs)

Class that provides a way to create simple admin module.

Parameters:

  • admin – The parent admin object
  • url_prefix – The url prefix
  • enpoint – The endpoint
  • short_title – the short module title use on navigation & breadcrumbs
  • title – The long title
  • parent – The parent node

add_url_rule(rule, endpoint, view_func, **options)

Adds a routing rule to the application from relative endpoint. view_class is copied as we need to dynamically apply decorators.

Parameters:

  • rule – The rule
  • endpoint – The endpoint
  • view_func – The view

secure_endpoint(endpoint, http_code=403)

Gives a way to secure specific url path.

Parameters:

  • endpoint – The endpoint to protect
  • http_code – The response http code when False

url

Returns first registered (main) rule as url.

SQLAlchemy 扩展

class ext.sqlalchemy.ModelAdminModule(*args, **kwargs)

SQLAlchemy model admin module builder.

count_list(search=None)

Counts filtered list.

Parameters: search – The string for quick search

create_object()

New object instance new object.

delete_object(object)

Deletes object.

Parameters: object – The object to delete

edit_query_factory

Returns query for object edition.

form_view

alias of ObjectFormView

get_actions_for_object(object)

“Returns actions for object as and tuple list.

Parameters: object – The object

get_object(pk)

Gets back object by primary key.

Parameters: pk – The object primary key

get_object_list(search=None, order_by_name=None, order_by_direction=None, offset=None, limit=None)

Returns ordered, filtered and limited query.

Parameters:

  • search – The string for search filter
  • order_by_name – The field name to order by
  • order_by_direction – The field direction
  • offset – The offset position
  • limit – The limit

list_query_factory

Returns non filtered list query.

save_object(obj)

Saves object.

Parameters: object – The object to save