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            if hasattr(obj, "approval") and obj.approval:
    24                obj.approval._update_source(default=False, save=False)
    25                obj.request = request
    26                self.message_user(
    27                    request,
    28                    pgettext_lazy("approval", "This form is showing changes currently pending."),
    29                    level=messages.WARNING,
    30                )
    31            else:
    32                raise ImproperlyConfigured(f"No approval model was declared for this model.")
    33        return obj
    34
    35    @display(description=pgettext_lazy("approval", "status"), ordering="approval__approved")
    36    def get_approval_status(self, obj):
    37        if isinstance(obj, MonitoredModel) and hasattr(obj, "approval") and obj.approval:
    38            return obj.approval.get_approved_display()
    39        return "N/A"

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            if hasattr(obj, "approval") and obj.approval:
    25                obj.approval._update_source(default=False, save=False)
    26                obj.request = request
    27                self.message_user(
    28                    request,
    29                    pgettext_lazy("approval", "This form is showing changes currently pending."),
    30                    level=messages.WARNING,
    31                )
    32            else:
    33                raise ImproperlyConfigured(f"No approval model was declared for this model.")
    34        return obj
    35
    36    @display(description=pgettext_lazy("approval", "status"), ordering="approval__approved")
    37    def get_approval_status(self, obj):
    38        if isinstance(obj, MonitoredModel) and hasattr(obj, "approval") and obj.approval:
    39            return obj.approval.get_approved_display()
    40        return "N/A"

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            if hasattr(obj, "approval") and obj.approval:
    25                obj.approval._update_source(default=False, save=False)
    26                obj.request = request
    27                self.message_user(
    28                    request,
    29                    pgettext_lazy("approval", "This form is showing changes currently pending."),
    30                    level=messages.WARNING,
    31                )
    32            else:
    33                raise ImproperlyConfigured(f"No approval model was declared for this model.")
    34        return obj

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

    36    @display(description=pgettext_lazy("approval", "status"), ordering="approval__approved")
    37    def get_approval_status(self, obj):
    38        if isinstance(obj, MonitoredModel) and hasattr(obj, "approval") and obj.approval:
    39            return obj.approval.get_approved_display()
    40        return "N/A"

media

##### 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