approval.models.monitored API documentation

# approval.models.monitored

View Source

     1from typing import List
     2
     3from django.core.exceptions import ObjectDoesNotExist
     4from django.db import models
     5import logging
     6
     7from django.utils.translation import pgettext_lazy
     8
     9
    10logger = logging.getLogger("approval")
    11
    12
    13class MonitoredModel(models.Model):
    14    """
    15    Moderated table mixin.
    16
    17    If you want to mark a model to make it subject to a moderation stage,
    18    you must make it inherit from ``MonitoredModel``. No need to make it
    19    also inherit from ``django.db.models.Model``, since it's already
    20    a parent class of the mixin.
    21
    22    .. code-block:: python
    23        :linenos:
    24
    25        from approval.models import MonitoredModel
    26        from django.db import models
    27
    28        class Post(MonitoredModel):  # Inherit from MonitoredModel
    29            # Add your own fields. Having a visibility field is a good idea.
    30            user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="entries")
    31            is_visible = models.BooleanField(default=True, verbose_name="visible")
    32            content = models.TextField(blank=False, verbose_name="content")
    33
    34    Inheriting from ``MonitoredModel`` allows provided ``pre-save`` and ``post-save`` signal
    35    hooks to automatically manage your model.
    36    """
    37
    38    class Meta:
    39        abstract = True
    40
    41    def _get_authors(self) -> List[models.Model]:
    42        """Get the authors of the current instance."""
    43        # Use user from request as author only if allowed
    44        if getattr(self, "approval", None):
    45            if self.approval.auto_approve_by_request:
    46                if getattr(self, "request") and getattr(self.request, "user", None):
    47                    logger.debug(pgettext_lazy("approval", f"Using request user as author of {self}."))
    48                    return [self.request.user]
    49            return self.approval._get_authors()
    50        return []
    51
    52    def _revert(self) -> bool:
    53        """
    54        Revert the instance to its last saved state.
    55
    56        This method deletes unsaved changes on source model instance,
    57        by reloading what's stored in the database.
    58
    59        Returns:
    60            `True` if revert was possible, `False` otherwise.
    61        """
    62        try:
    63            self.refresh_from_db()
    64            logger.debug(pgettext_lazy("approval", "Monitored {obj} was reverted to last DB state.").format(obj=self))
    65            return True
    66        except ObjectDoesNotExist:
    67            return False

logger = <Logger approval (DEBUG)>
class MonitoredModel(django.db.models.base.Model): View Source

    14class MonitoredModel(models.Model):
    15    """
    16    Moderated table mixin.
    17
    18    If you want to mark a model to make it subject to a moderation stage,
    19    you must make it inherit from ``MonitoredModel``. No need to make it
    20    also inherit from ``django.db.models.Model``, since it's already
    21    a parent class of the mixin.
    22
    23    .. code-block:: python
    24        :linenos:
    25
    26        from approval.models import MonitoredModel
    27        from django.db import models
    28
    29        class Post(MonitoredModel):  # Inherit from MonitoredModel
    30            # Add your own fields. Having a visibility field is a good idea.
    31            user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="entries")
    32            is_visible = models.BooleanField(default=True, verbose_name="visible")
    33            content = models.TextField(blank=False, verbose_name="content")
    34
    35    Inheriting from ``MonitoredModel`` allows provided ``pre-save`` and ``post-save`` signal
    36    hooks to automatically manage your model.
    37    """
    38
    39    class Meta:
    40        abstract = True
    41
    42    def _get_authors(self) -> List[models.Model]:
    43        """Get the authors of the current instance."""
    44        # Use user from request as author only if allowed
    45        if getattr(self, "approval", None):
    46            if self.approval.auto_approve_by_request:
    47                if getattr(self, "request") and getattr(self.request, "user", None):
    48                    logger.debug(pgettext_lazy("approval", f"Using request user as author of {self}."))
    49                    return [self.request.user]
    50            return self.approval._get_authors()
    51        return []
    52
    53    def _revert(self) -> bool:
    54        """
    55        Revert the instance to its last saved state.
    56
    57        This method deletes unsaved changes on source model instance,
    58        by reloading what's stored in the database.
    59
    60        Returns:
    61            `True` if revert was possible, `False` otherwise.
    62        """
    63        try:
    64            self.refresh_from_db()
    65            logger.debug(pgettext_lazy("approval", "Monitored {obj} was reverted to last DB state.").format(obj=self))
    66            return True
    67        except ObjectDoesNotExist:
    68            return False

Moderated table mixin.

If you want to mark a model to make it subject to a moderation stage, you must make it inherit from `MonitoredModel`. No need to make it also inherit from `django.db.models.Model`, since it's already a parent class of the mixin.

    :linenos:

    from approval.models import MonitoredModel
    from django.db import models

    class Post(MonitoredModel):  # Inherit from MonitoredModel
        # Add your own fields. Having a visibility field is a good idea.
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="entries")
        is_visible = models.BooleanField(default=True, verbose_name="visible")
        content = models.TextField(blank=False, verbose_name="content")

Inheriting from `MonitoredModel` allows provided `pre-save` and `post-save` signal hooks to automatically manage your model.

##### Inherited Members

django.db.models.base.Model
  Model
  from_db
  pk
  get_deferred_fields
  refresh_from_db
  arefresh_from_db
  serializable_value
  save
  asave
  save_base
  delete
  adelete
  prepare_database_save
  clean
  validate_unique
  date_error_message
  unique_error_message
  get_constraints
  validate_constraints
  full_clean
  clean_fields
  check

class MonitoredModel.Meta: View Source

    39    class Meta:
    40        abstract = True

abstract = False