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.

menu_title = 'CRUDitor'[source]

Title to use in templates / menu bar

index_url = '#'[source]

URL to use in the linked menu_title

logout_url = '#'[source]

URL to use when providing a logout link to the user.

change_password_url = None[source]

URL to the change password view, if available.

menu_template_name = 'cruditor/includes/menu.html'[source]

Template name which is included to render the menu.

extrahead_template_name = 'cruditor/includes/extrahead.html'[source]

Template used to include extra head stuff.

login_template_name = 'cruditor/login.html'[source]

Page template for the login view.

login_form_class[source]

Form class which is used in the login view.

alias of LoginForm

staff_required = True[source]

Decide if only staff users should be able to use the Cruditor views.

required_permission = None[source]

Which permission is required to access the view.

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 from get_title method.

get_title()[source]

Returns the title of the page. Uses view’s title property. If not set falls back to menu_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 a url 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 and url property/key. In addition, one can provide an alternative button_class which is used as a css class - pefixed with “btn-“. Default button_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 if staff_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.

ensure_required_permission()[source]

This method ensures that all required permissions (fetched by calling get_required_permission).

If permissions are not met, PermissionDenied is raised.

get_context_data(**kwargs)[source]

Adds the cruditor context variable to the template context. Uses data from get_cruditor_context method.

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).

formset_classes = None[source]
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.

get_success_message()[source]

Returns the success message to display when the form is valid.

form_valid(form, **formsets)[source]

Saves the data and provides a nice success message, then redirects to the get_success_url url.

form_invalid(form, **formsets)[source]

Re-render the page with the invalid form and/or formsets.