approval.listeners.monitored

 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        if instance.pk is not None:  # Sandbox updated instances only
29            logger.debug(pgettext_lazy("approval", "Pre-save signal handled on updated {cls}").format(cls=sender))
30            users = instance._get_authors()
31            instance.approval._update_sandbox()
32            instance._revert()
33            instance.approval._auto_process_approval(authors=users, update=True)
34
35
36@receiver(post_save)
37def after_save(sender: Type[models.Model], instance: models.Model, **kwargs):
38    """
39    Manage data in the approvable item after it has been saved for the first time
40
41    For new objects, copy the status to the sandbox, and then
42    set some fields in the original object to reflect approval defaults
43    (generally, it means setting content to invisible or unpublished)
44
45    :param sender: Generally, the class of the saved object
46    :param instance: Instance of the saved object
47    :param raw: --
48    :param created: Is the instance new in the database ?
49    """
50    if issubclass(sender, MonitoredModel) and not getattr(instance, "_ignore_approval", False):
51        if kwargs.get("created", False):
52            logger.debug(pgettext_lazy("approval", "Post-save signal handled on new {cls}").format(cls=sender))
53            users = instance._get_authors()
54            instance.approval._update_sandbox()
55            instance.approval._update_source(default=True, save=True)
56            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):
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        if instance.pk is not None:  # Sandbox updated instances only
30            logger.debug(pgettext_lazy("approval", "Pre-save signal handled on updated {cls}").format(cls=sender))
31            users = instance._get_authors()
32            instance.approval._update_sandbox()
33            instance._revert()
34            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):
37@receiver(post_save)
38def after_save(sender: Type[models.Model], instance: models.Model, **kwargs):
39    """
40    Manage data in the approvable item after it has been saved for the first time
41
42    For new objects, copy the status to the sandbox, and then
43    set some fields in the original object to reflect approval defaults
44    (generally, it means setting content to invisible or unpublished)
45
46    :param sender: Generally, the class of the saved object
47    :param instance: Instance of the saved object
48    :param raw: --
49    :param created: Is the instance new in the database ?
50    """
51    if issubclass(sender, MonitoredModel) and not getattr(instance, "_ignore_approval", False):
52        if kwargs.get("created", False):
53            logger.debug(pgettext_lazy("approval", "Post-save signal handled on new {cls}").format(cls=sender))
54            users = instance._get_authors()
55            instance.approval._update_sandbox()
56            instance.approval._update_source(default=True, save=True)
57            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 ?