Base views & mix-ins

Base Dashboard classes

class megforms.views.base.OTPRequiredConditionalMixin

Copy of OTPRequiredMixin with added condition View mixin which verifies that the user logged in using OTP.

Note

This mixin should be the left-most base class.

raise_anonymous = False

Whether to raise PermissionDenied if the user isn’t logged in.

login_url = None

If raise_anonymous is set to False, this defines where the user will be redirected to. Defaults to two_factor:login.

redirect_field_name = 'next'

URL query name to use for providing the destination URL.

raise_unverified = False

Whether to raise PermissionDenied if the user isn’t verified.

verification_url = None

If raise_unverified is set to False, this defines where the user will be redirected to. If set to None, an explanation will be shown to the user on why access was denied.

get_login_url()

Returns login url to redirect to.

get_verification_url()

Returns verification url to redirect to.

require_otp(request)

Whether OTP should be required for this page load

class megforms.views.base.AuditorInstitutionMixin

Provides auditor and institution properties and context variables

auditor

Auditor for currently logged in user. If user is not an auditor, raises PermissionDenied

institution

Gets current institution Institution is taken from slug in the url if present, or default auditor’s institution is returned if institution slug is not in the url :return: Institution instance

forms

Gets qs of forms available to the current user

institution_forms

Audit forms available to the user in current institution

property audit_forms: AuditFormQueryset

audit forms available in the context of current user and institution

class megforms.views.base.MaintenanceModeMixin

A mixin for Create/Update views to disable it while current institution is in maintenance mode. To be used with AuditorInstitutionMixin

class megforms.views.base.NavbarBranding

Base for a mixin class that allows overriding navbar color and logo

class megforms.views.base.FormTypeMixin
form_types

Form types available in current view

class megforms.views.base.PermittedWardsMixin

Provides a method which exposes a queryset of wards implementing advanced ringfencing.

permitted_wards

Based on whether the current audit form uses advanced ward ringfencing, determines what wards the user should be allowed to see for this view.

class megforms.views.base.AuditFormPermissionsMixin

Provides audit form mixin with a has_permission method that takes into account user’s permissions for current form.

Attributes: enforce_level_check (bool): If True, will trigger a check to match the users level with the forms level

has_perm(*permissions: str) bool

check if user has specific permissions in this form as opposed to only permission defined in view class.

has_global_perm

Checks if user has the required permission on the global level.

level_edit_access() bool

Wrapper to call the utility check_edit_access function.

has_permission() bool

Overrides method from PermissionRequiredMixin so that views implementing PermissionRequiredMixin can take advantage of form-level permissions If enforce_level_check is True, enforce the level check logic.

class megforms.views.base.AuditFormMixin

Mixin to add get_current_form() method to a view The view will accept the following kwargs: * current_form_id - pk of the current form, otherwise defaults to the first form available to the user Provides the following context variables: * forms - forms available to the user in current institution * current_form - currently selected form

require_otp(request)

Whether OTP should be required for this page load

forms
current_form: AuditForm

Audit form currently selected for this view If audit form id was not passed into the view with current_form_id arg, first available form is returned

Returns:

the form instance

Raises:

AuditForm.DoesNotExist – If the form does not exist or is not published

class megforms.views.base.FilterFormMixin

Mixin which adds support for filter form

normalize_query_dict(value: QueryDict | dict, ignored_keys: set[str] | None = None) dict

Normalize query dict from either Saved Bookmark json or from request data and return a valid dictionary.

Parameters:
  • value – the QueryDict or dict to normalize

  • ignored_keys – keys to not include in result dictionary

Returns:

load_stored_filters() dict[str, Any]

Loads filter dict from cache

Returns:

dictionary containing filter fields mapped to their selected value, or null. If there are no filters, and empty dictionary is returned

Raises:

KeyError – if filter with that key does not exist in cache

request_get_data

Reads GET parameters from the request and returns a dictionary containing the parameters suitable to be passed to the filter form. Returned values are not ring-fenced as the ringfencing will be applied in the form itself.

Returns:

form data with the values passed via get

get_forms_for_filter() AuditForm | AuditFormQueryset

Gets forms visible in the filter form. This can be a wider range of forms if current institution is group-level

property child_form_models: set[typing.Union[ForwardRef('CustomSubform'), typing.Type[ForwardRef('BaseSubObservation')]]]

Set of models (or custom subform instances) selected in the subform dropdown

filter_objects(qs: ObservationQueryset) ObservationQueryset

Applies filtering selected by the user to the queryset

filters_key

Hash of cached filters. May be null if no filters are set, or filter hash cached entry has expired - the view should fall back to default filters.

get_overview_filter_params()

GET parameters to retain selected search items

save_filter_state(request)

Stores filter state in the session and returns session key

get_filter_redirect_url(request: HttpRequest, filter_state_key: str) str

Builds url to reload this page using new filter state

property child_forms

Subform names

subforms

Filtered subobservations (or all)

Returns:

mixed list of hardcoded subforms (model classes) and custom subforms (Subform instances)

property date_range: Tuple[datetime, datetime] | None

Tuple of (start_date, end_date) values

class megforms.views.base.ToolbarMixin

Include dashboard/toolbar.html template to render toolbar

check_current_user_toolbar_item_permissions(item: BaseToolbarItem, *, form: AuditForm | None = None) bool

Determine whether the current user can see a toolbar item. Checks both global permissions and form-level permissions when a form is provided.

Parameters:
  • item – item to check which permissions we need.

  • form – form to verify the permissions against.

get_toolbar_items() Sequence[BaseToolbarItem]

Return list of toolbar items for this dashboard in form of a list of tuples (label, url, icon_type).

class megforms.views.base.AuditReportToolbarMixin
get_toolbar_items() list[megforms.toolbar.BaseToolbarItem]

Return list of toolbar items for this dashboard in form of a list of tuples (label, url, icon_type).

class megforms.views.base.ReversionMixin

View mixin that logs historical changes using reversion library. This mixin logs changes made during POST request by default.

reversion_enabled(request)

Whether reversion should be used in this request

class megforms.views.base.ContainerClassesMixin

View mixin allowing to inject additional css classes to the wrapping div. The div normally has only one class: non-responsive-container-fixed but additional classes can be specified in container_classes by using this mixin. By default container_classes is an empty tuple

class megforms.views.base.SettingsTabMixin

A mixin for dashboard settings tabs to allow current tab to be highlighted with permissions and without

Authentication

class accounts.views.mixin.NextUrlViewMixin

Mixin for view classes that provides “next” parameter in url. The value is exposed as next property in the view.

For example: /login?next=/accounts/profile

The “next” url typically stores the url where teh view should redirect to after user performs an action (i.e. form submission).

NEXT_ARG_NAME = 'next'

name of the GET argument that stores the value of the next url

next

Value of the next GET parameter. The property gets the value and validates its safety before returning.

Returns:

str url, or None if not valid url was passed

Analytics

class analytics.views.mixin.AnalyticsViewLogMixin

Logs object view for analytics

eGuides

class eguides.views.mixins.ChangeList(list_display: tuple[str, ...], list_display_links: tuple[str, ...], model: Eguide | Section | Information | Disclaimer | EGuideVersion | EguideUrlSlug, model_admin: View)

Replica object of django.contrib.admin.views.main.ChangeList. Allows eguides cms list views to extend django admin functionality for rendering headers and table row content.

class eguides.views.mixins.EguideCMSBaseMixin

Mixin used in every eguides cms view

get_empty_value_display() str

Required by items_for_result

class eguides.views.mixins.EguidesCMSMenusMixin

Mixin to add nav menu items

class eguides.views.mixins.EguideContextMixin

Mixin to add the current eGuide to the view.

class eguides.views.mixins.EguideObjectBaseMixin

For use in all crud and list views where the model has an eguide.

class eguides.views.mixins.ObjectCrudButtonsMixin

Mixin to allow for CRUD buttons to be added dynamically to context.

class eguides.views.mixins.FieldsetsCMSAdminMixin

A Mixin to add fieldsets to the template context. To be used with any view that specifies a fields attribute. Inheriting classes don’t need to specify the fields attribute, but must specify a fieldsets attribute.

class eguides.views.mixins.ObjectCrudBreadcrumbMixin

Adds breadcrumbs specific to CRUD views.