approval.listeners.monitored API documentation

# approval.listeners.monitored

View Source

     1import logging
     2from typing import Type
     3
     4from django.db import models
     5from django.utils.translation import pgettext_lazy
     6
     7from ..models.monitored import MonitoredModel
     8from django.db.models.signals import post_save, pre_save
     9from django.dispatch.dispatcher import receiver
    10
    11logger = logging.getLogger("approval")
    12
    13
    14@receiver(pre_save)
    15def before_save(sender: Type[models.Model], instance: MonitoredModel, **kwargs):
    16    """
    17    Manage data in the approvable item before it is saved.
    18
    19    For already created objects, update the sandbox with current object status,
    20    and then revert the changes in the object before saving. Then try automatic
    21    validation on the object.
    22
    23    Args:
    24        sender: Class of the instance to process.
    25        instance: Instance to process.
    26    """
    27    if issubclass(sender, MonitoredModel) and not getattr(instance, "_ignore_approval", False):
    28        # Create approval instance if not present
    29        if not getattr(instance, "approval", None):
    30            for obj in instance._meta.related_objects:
    31                if getattr(obj.related_model, "_is_sandbox", False):
    32                    approval: models.Model = obj.related_model(source=instance)
    33                    approval.save()
    34                    break
    35        if instance.pk is not None and getattr(instance, "approval", None):  # Sandbox updated instances only
    36            logger.debug(pgettext_lazy("approval", "Pre-save signal handled on updated {cls}").format(cls=sender))
    37            users = instance._get_authors()
    38            instance.approval._update_sandbox()
    39            instance._revert()
    40            instance.approval._auto_process_approval(authors=users, update=True)
    41
    42
    43@receiver(post_save)
    44def after_save(sender: Type[models.Model], instance: models.Model, **kwargs):
    45    """
    46    Manage data in the approvable item after it has been saved for the first time
    47
    48    For new objects, copy the status to the sandbox, and then
    49    set some fields in the original object to reflect approval defaults
    50    (generally, it means setting content to invisible or unpublished)
    51
    52    :param sender: Generally, the class of the saved object
    53    :param instance: Instance of the saved object
    54    :param raw: --
    55    :param created: Is the instance new in the database ?
    56    """
    57    if issubclass(sender, MonitoredModel) and not getattr(instance, "_ignore_approval", False):
    58        if kwargs.get("created", False) and getattr(instance, "approval", None):
    59            logger.debug(pgettext_lazy("approval", "Post-save signal handled on new {cls}").format(cls=sender))
    60            users = instance._get_authors()
    61            instance.approval._update_sandbox()
    62            instance.approval._update_source(default=True, save=True)
    63            instance.approval._auto_process_approval(authors=users, update=False)

logger = <Logger approval (DEBUG)>
@receiver(pre_save)
def before_save( sender: Type[django.db.models.base.Model], instance: approval.models.monitored.MonitoredModel, **kwargs): View Source

    15@receiver(pre_save)
    16def before_save(sender: Type[models.Model], instance: MonitoredModel, **kwargs):
    17    """
    18    Manage data in the approvable item before it is saved.
    19
    20    For already created objects, update the sandbox with current object status,
    21    and then revert the changes in the object before saving. Then try automatic
    22    validation on the object.
    23
    24    Args:
    25        sender: Class of the instance to process.
    26        instance: Instance to process.
    27    """
    28    if issubclass(sender, MonitoredModel) and not getattr(instance, "_ignore_approval", False):
    29        # Create approval instance if not present
    30        if not getattr(instance, "approval", None):
    31            for obj in instance._meta.related_objects:
    32                if getattr(obj.related_model, "_is_sandbox", False):
    33                    approval: models.Model = obj.related_model(source=instance)
    34                    approval.save()
    35                    break
    36        if instance.pk is not None and getattr(instance, "approval", None):  # Sandbox updated instances only
    37            logger.debug(pgettext_lazy("approval", "Pre-save signal handled on updated {cls}").format(cls=sender))
    38            users = instance._get_authors()
    39            instance.approval._update_sandbox()
    40            instance._revert()
    41            instance.approval._auto_process_approval(authors=users, update=True)

Manage data in the approvable item before it is saved.

For already created objects, update the sandbox with current object status, and then revert the changes in the object before saving. Then try automatic validation on the object.

Args: sender: Class of the instance to process. instance: Instance to process.

@receiver(post_save)
def after_save( sender: Type[django.db.models.base.Model], instance: django.db.models.base.Model, **kwargs): View Source

    44@receiver(post_save)
    45def after_save(sender: Type[models.Model], instance: models.Model, **kwargs):
    46    """
    47    Manage data in the approvable item after it has been saved for the first time
    48
    49    For new objects, copy the status to the sandbox, and then
    50    set some fields in the original object to reflect approval defaults
    51    (generally, it means setting content to invisible or unpublished)
    52
    53    :param sender: Generally, the class of the saved object
    54    :param instance: Instance of the saved object
    55    :param raw: --
    56    :param created: Is the instance new in the database ?
    57    """
    58    if issubclass(sender, MonitoredModel) and not getattr(instance, "_ignore_approval", False):
    59        if kwargs.get("created", False) and getattr(instance, "approval", None):
    60            logger.debug(pgettext_lazy("approval", "Post-save signal handled on new {cls}").format(cls=sender))
    61            users = instance._get_authors()
    62            instance.approval._update_sandbox()
    63            instance.approval._update_source(default=True, save=True)
    64            instance.approval._auto_process_approval(authors=users, update=False)

Manage data in the approvable item after it has been saved for the first time

For new objects, copy the status to the sandbox, and then set some fields in the original object to reflect approval defaults (generally, it means setting content to invisible or unpublished)

###### Parameters

- sender: Generally, the class of the saved object
- instance: Instance of the saved object
- raw: --
- created: Is the instance new in the database ?