approval.admin.monitored API documentation

# approval.admin.monitored

View Source

     1from django.contrib import messages
     2from django.contrib.admin import display
     3from django.contrib.admin.options import ModelAdmin
     4from django.core.exceptions import ImproperlyConfigured
     5from django.utils.translation import pgettext_lazy
     6
     7from ..models import MonitoredModel
     8
     9
    10class MonitoredAdmin(ModelAdmin):
    11    """
    12    ModelAdmin mixin for approval-controlled objects.
    13
    14    This class should not be registered into the admin.
    15    Instead, developers should create a `ModelAdmin` class derived from this
    16    class.
    17    """
    18
    19    def get_object(self, request, object_id, from_field: str = None) -> MonitoredModel:
    20        """Return the desired object, augmented with a request attribute."""
    21        obj: MonitoredModel = super().get_object(request, object_id)
    22        if isinstance(obj, MonitoredModel):
    23            # Only display approval warning if the object has not been approved.
    24            if getattr(obj, "approval", None) and obj.approval.approved is None:
    25                obj.approval._update_source(default=False, save=False)
    26                obj.request = request
    27                if obj.approval.approved is None:
    28                    self.message_user(
    29                        request,
    30                        pgettext_lazy(
    31                            "approval", "This form is showing changes currently pending."
    32                        ),
    33                        level=messages.WARNING,
    34                    )
    35            return obj
    36        else:
    37            raise ImproperlyConfigured(f"No approval model was declared for this model.")
    38
    39    @display(description=pgettext_lazy("approval", "status"), ordering="approval__approved")
    40    def get_approval_status(self, obj):
    41        if isinstance(obj, MonitoredModel) and hasattr(obj, "approval") and obj.approval:
    42            return obj.approval.get_approved_display()
    43        return pgettext_lazy("approval", "Unavailable")

class MonitoredAdmin(django.contrib.admin.options.ModelAdmin): View Source

    11class MonitoredAdmin(ModelAdmin):
    12    """
    13    ModelAdmin mixin for approval-controlled objects.
    14
    15    This class should not be registered into the admin.
    16    Instead, developers should create a `ModelAdmin` class derived from this
    17    class.
    18    """
    19
    20    def get_object(self, request, object_id, from_field: str = None) -> MonitoredModel:
    21        """Return the desired object, augmented with a request attribute."""
    22        obj: MonitoredModel = super().get_object(request, object_id)
    23        if isinstance(obj, MonitoredModel):
    24            # Only display approval warning if the object has not been approved.
    25            if getattr(obj, "approval", None) and obj.approval.approved is None:
    26                obj.approval._update_source(default=False, save=False)
    27                obj.request = request
    28                if obj.approval.approved is None:
    29                    self.message_user(
    30                        request,
    31                        pgettext_lazy(
    32                            "approval", "This form is showing changes currently pending."
    33                        ),
    34                        level=messages.WARNING,
    35                    )
    36            return obj
    37        else:
    38            raise ImproperlyConfigured(f"No approval model was declared for this model.")
    39
    40    @display(description=pgettext_lazy("approval", "status"), ordering="approval__approved")
    41    def get_approval_status(self, obj):
    42        if isinstance(obj, MonitoredModel) and hasattr(obj, "approval") and obj.approval:
    43            return obj.approval.get_approved_display()
    44        return pgettext_lazy("approval", "Unavailable")

ModelAdmin mixin for approval-controlled objects.

This class should not be registered into the admin. Instead, developers should create a `ModelAdmin` class derived from this class.

def get_object( self, request, object_id, from_field: str = None) -> approval.models.monitored.MonitoredModel: View Source

    20    def get_object(self, request, object_id, from_field: str = None) -> MonitoredModel:
    21        """Return the desired object, augmented with a request attribute."""
    22        obj: MonitoredModel = super().get_object(request, object_id)
    23        if isinstance(obj, MonitoredModel):
    24            # Only display approval warning if the object has not been approved.
    25            if getattr(obj, "approval", None) and obj.approval.approved is None:
    26                obj.approval._update_source(default=False, save=False)
    27                obj.request = request
    28                if obj.approval.approved is None:
    29                    self.message_user(
    30                        request,
    31                        pgettext_lazy(
    32                            "approval", "This form is showing changes currently pending."
    33                        ),
    34                        level=messages.WARNING,
    35                    )
    36            return obj
    37        else:
    38            raise ImproperlyConfigured(f"No approval model was declared for this model.")

Return the desired object, augmented with a request attribute.

@display(description=pgettext_lazy('approval', 'status'), ordering='approval__approved')
def get_approval_status(self, obj): View Source

    40    @display(description=pgettext_lazy("approval", "status"), ordering="approval__approved")
    41    def get_approval_status(self, obj):
    42        if isinstance(obj, MonitoredModel) and hasattr(obj, "approval") and obj.approval:
    43            return obj.approval.get_approved_display()
    44        return pgettext_lazy("approval", "Unavailable")

media View Source

    193    def _media(self):
    194        # Get the media property of the superclass, if it exists
    195        sup_cls = super(cls, self)
    196        try:
    197            base = sup_cls.media
    198        except AttributeError:
    199            base = Media()
    200
    201        # Get the media definition for this class
    202        definition = getattr(cls, "Media", None)
    203        if definition:
    204            extend = getattr(definition, "extend", True)
    205            if extend:
    206                if extend is True:
    207                    m = base
    208                else:
    209                    m = Media()
    210                    for medium in extend:
    211                        m += base[medium]
    212                return m + Media(definition)
    213            return Media(definition)
    214        return base

##### Inherited Members

django.contrib.admin.options.ModelAdmin
  ModelAdmin
  list_display
  list_display_links
  list_filter
  list_select_related
  list_per_page
  list_max_show_all
  list_editable
  search_fields
  search_help_text
  date_hierarchy
  save_as
  save_as_continue
  save_on_top
  paginator
  preserve_filters
  inlines
  add_form_template
  change_form_template
  change_list_template
  delete_confirmation_template
  delete_selected_confirmation_template
  object_history_template
  popup_response_template
  actions
  action_form
  actions_on_top
  actions_on_bottom
  actions_selection_counter
  checks_class
  model
  opts
  admin_site
  get_inline_instances
  get_urls
  urls
  get_model_perms
  get_form
  get_changelist
  get_changelist_instance
  get_changelist_form
  get_changelist_formset
  get_formsets_with_inlines
  get_paginator
  log_addition
  log_change
  log_deletion
  action_checkbox
  get_actions
  get_action_choices
  get_action
  get_list_display
  get_list_display_links
  get_list_filter
  get_list_select_related
  get_search_fields
  get_search_results
  get_preserved_filters
  construct_change_message
  message_user
  save_form
  save_model
  delete_model
  delete_queryset
  save_formset
  save_related
  render_change_form
  response_add
  response_change
  response_post_save_add
  response_post_save_change
  response_action
  response_delete
  render_delete_form
  get_inline_formsets
  get_changeform_initial_data
  changeform_view
  add_view
  change_view
  changelist_view
  get_deleted_objects
  delete_view
  history_view
  get_formset_kwargs
django.contrib.admin.options.BaseModelAdmin
  autocomplete_fields
  raw_id_fields
  fields
  exclude
  fieldsets
  form
  filter_vertical
  filter_horizontal
  radio_fields
  prepopulated_fields
  formfield_overrides
  readonly_fields
  ordering
  sortable_by
  view_on_site
  show_full_result_count
  check
  formfield_for_dbfield
  formfield_for_choice_field
  get_field_queryset
  formfield_for_foreignkey
  formfield_for_manytomany
  get_autocomplete_fields
  get_view_on_site_url
  get_empty_value_display
  get_exclude
  get_fields
  get_fieldsets
  get_inlines
  get_ordering
  get_readonly_fields
  get_prepopulated_fields
  get_queryset
  get_sortable_by
  lookup_allowed
  to_field_allowed
  has_add_permission
  has_change_permission
  has_delete_permission
  has_view_permission
  has_view_or_change_permission
  has_module_permission