Invenio-Admin is an optional component of Invenio, responsible for registering
and customizing the administration panel for model views and user-defined
admin pages. The module uses standard Flask-Admin features and assumes very
little about other components installed within a given Invenio instance.

We use the view_class_factory parameter above to disable the
authentication to the admin panel, in order to simplify this tutorial.
Do not use this for production systems, as you will grant access to the
admin panel to anonymous users!

In full application with an authentication system in place, it is
sufficient to instantiate the extension like:

In real-world scenarios you will most likley want to add an admin view for
your custom models from within the Invenio module or an Invenio overlay
application. Instead of registering it directly on the application as in the
example above, you can use entry points to register those automatically.

Let us start with defining the admin.py file inside your module or overlay,
which will contain all admin-related classes and functions.
For example, assuming a Invenio-Diner module, the file could reside in:

invenio-diner/invenio_diner/admin.py.

In this example we will define two model views for two database models and one
separate base view for statistics page. The content of the file is as follows:

# invenio-diner/invenio_diner/admin.pyfromflask_admin.baseimportBaseView,exposefromflask_admin.contrib.sqlaimportModelViewfrominvenio_dbimportdbfrom.modelsimportSnack,BreakfastclassSnackModelView(ModelView):can_create=Truecan_edit=Truecan_view_details=Truecolumn_list=('id','name','price',)classBreakfastModelView(ModelView):can_create=Falsecan_edit=Falsecan_view_details=Truecolumn_searchable_list=('id','toast','eggs','bacon')classDinerStats(BaseView):@expose('/')defindex(self):return"Welcome to the Invenio-Diner statistics page!"@expose('/sales/')defsales(self):return"You have served 0 meals!"snack_adminview={'view_class':Snack,'args':[SnackModelView,db.session],'kwargs':{'category':'Diner'},}breakfast_adminview={'view_class':Breakfast,'args':[BreakfastModelView,db.session],'kwargs':{'category':'Diner'},}stats_adminview={'view_class':DinerStats,'kwargs':{'name':'Invenio Diner Stats'},}__all__=('snack_adminview','breakfast_adminview','stats_adminview',)

Note

You have to define a dictionary for each BaseView and Model-ModelView pairs
(see stats_adminview, snack_adminview and breakfast_adminview
above) in order to have the admin views automatically registered via
entry points (see next section).

The args and kwargs keys in the dictionaries are passed to the
constructor of the view class once it is intialized.

The default way of adding admin views to the admin panel is though
setuptools’ entry point discovery. To do that, a newly created module has to
register an entry point under the group invenio_admin.views inside its
setup.py as follows:

By default Invenio-Admin protects the admin views from unauthenticated users
with Flask-Login and restricts the access on a per-permission basis using
Flask-Security. In order to login to a Invenio-Admin panel the user
needs to be authenticated using Flask-Login and have a Flask-Security
identity which provides the ActionNeed('admin-access').

Note

If you want to use a custom permission rule, you can easily specify
your own permission factory in the configuration variable
invenio_admin.config.ADMIN_PERMISSION_FACTORY.

For more information, see the default factory:
invenio_admin.permissions.admin_permission_factory()
and how the the view is using it:
invenio_admin.views.protected_adminview_factory()

At core, Invenio-Admin uses Flask-Admin for rendering the admin panel
and all of its views. All of the features for defining the ModelViews
and BaseViews can be found in the official Flask-Admin documentation.
Nonetheless, we will mention some of the ones that were already made easy to
use directly in Invenio-Admin.

Non-basic data types can be made easier to search for and filter using type
filters. This way, fields of certain type that is not searchable by default
can be extended with that functionality. For example see a built-in UUID
filter invenio_admin.filters.UUIDEqualFilter. You can enable the
custom fields filters, by setting a variable filter_converter on the
ModelView class. See an example of a custom filter converter in
invenio_admin.filters.FilterConverter.

Assuming that the id field in Snack model from the example above is a
UUID-type field, you could enable the UUID filtering on this model as follows:

Styling of the administration interface can be changed via the configuration
variable ADMIN_BASE_TEMPLATE.

If Invenio-Theme is installed,
ADMIN_BASE_TEMPLATE is automatically set to use the
AdminLTE theme
which provides an extra configuration variable ADMIN_UI_SKIN which controls
the AdminLTE skin (e.g. skin-blue or skin-black). See AdminLTE
documentation for details on supported skins.

If Invenio-Theme is not installed the default Flask-Admin templates will be
used (based on Bootstrap).