Mixins¶
- class cruditor.mixins.CruditorMixin[source]¶
Bases:
object
Base mixin for all Cruditor views. Provides common functionality for all views.
It is a good idea to have a own “base” mixin to configure the common options like
menu_title
, the urls and templates.Usually you might have
required_permission
configured per-view.Title to use in templates / menu bar
Template name which is included to render the menu.
- extrahead_template_name = 'cruditor/includes/extrahead.html'[source]¶
Template used to include extra head stuff.
- model_verbose_name = None[source]¶
If not provided, Cruditor tries to look up the verbose name from
model.Meta
- dispatch(request, *args, **kwargs)[source]¶
Ensure the user is logged in (by calling ensure_logged_in` method). If the user is logged in, permissions are checked by calling
ensure_required_permission
.
- get_cruditor_context(alternative_title=None, login_context=False)[source]¶
Provides some context for all Cruditor templates to render menu, header, breadcrumb and title buttons.
The method takes an optional argument
alternative_title
to override the default title fromget_title
method.
- get_title()[source]¶
Returns the title of the page. Uses view’s
title
property. If not set falls back tomenu_title
.
- get_breadcrumb_title()[source]¶
By default, the breadcrumb title is the same as the page title. Calls
get_title
if not overwritten.
- get_breadcrumb()[source]¶
This method is expected to return a list of breadcrumb elements as a list.
Every breadcrumb element is a dict or object with at least a
title
property/key. If aurl
key/property is provided, the item is linked.
- get_titlebuttons()[source]¶
This method is expected to return None or a list of buttons to display in the title row of the page.
Every button element is a dict or object with at least a
label
andurl
property/key. In addition, one can provide an alternativebutton_class
which is used as a css class - pefixed with “btn-“. Defaultbutton_class
is “light”.
- get_model_verbose_name()[source]¶
Returns the verbose name of the handled object/item.
If
model_verbose_name
is set, the value is used. If not, Cruditor tries to get the verbose name from the model property (via Meta class). If no name is available at all, “Item” is returned.
- ensure_logged_in(request, *args, **kwargs)[source]¶
This method checks if the request user is logged in and has the right flags set (e.g.
is_staff
ifstaff_required
is set in view).If user is logged in,
True
is returned. If not,handle_not_logged_in
is called.
- handle_not_logged_in(request, *args, **kwargs)[source]¶
This method is responsible to handle not logged-in users. By default, renders the Django login view using a Cruditor optimized template using the
login_form_class
as Form.
- get_required_permission()[source]¶
Returns the required Django permissions required to access the view.
You might override the method to apply more complex rules on what permissions are required.
- class cruditor.mixins.FormViewMixin[source]¶
Bases:
object
Mixin to add formset support to Django FormViews. To use formsets, you have to provide a set of formsets as a dict (or OrderedDict if you have more than one formset - just to have a defined ordering).
- get_formset_classes()[source]¶
This method returns the formset classes to render in the form view. By default, returns the
formset_classes
property.
- get_formset_kwargs(formset_class)[source]¶
This method returns additional kwargs to initialize a formset. The formset_class is provided to ensure the method can return proper kwargs.
- get(request, *args, **kwargs)[source]¶
Extended get-method to render to form and all formsets properly initialized.
- post(request, *args, **kwargs)[source]¶
Extended version of the FormView.post method which validates the form and all configured formsets. If everything is valid,
form_valid
is called. If something is not valid,form_invalid
is called.Both the form instance and all formset instances are provided to the called method. The form is passed as the first argument, the formsets are passed as keyword arguments using the formset key from
formset_classes
.
- save_form(form, **formsets)[source]¶
This method is called from
form_valid
to actual save the data from the form and all formsets. All saving is done by default.