Refactor managemails to use signals
- use signals to trigger Celery tasks to create and delete mailboxes - add generic TestCaseWithCeleryTasks class to handle celery task tests in a uniform way
This commit is contained in:
parent
d6fc29a2b8
commit
610f8976fc
5 changed files with 114 additions and 37 deletions
62
gnuviechadmin/managemails/signals.py
Normal file
62
gnuviechadmin/managemails/signals.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
"""
|
||||
This module contains the signal handlers of the :py:mod:`managemails` app.
|
||||
|
||||
The module starts Celery_ tasks.
|
||||
|
||||
.. _Celery: https://www.celeryproject.org/
|
||||
|
||||
"""
|
||||
import logging
|
||||
|
||||
from django.db.models.signals import post_delete, post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
from fileservertasks.tasks import create_file_mailbox, delete_file_mailbox
|
||||
from managemails.models import Mailbox
|
||||
from taskresults.models import TaskResult
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@receiver(post_save, sender=Mailbox)
|
||||
def handle_mailbox_created(sender, instance, created, **kwargs):
|
||||
"""
|
||||
Handles post creation actions on :py:class:`Mailbox <managemails.models.Mailbox>` instances.
|
||||
|
||||
:param sender: sender of the signal
|
||||
:param instance: Mailbox instance
|
||||
:param created: whether the instance has just been created
|
||||
|
||||
This signal handler starts a Celery_ task.
|
||||
|
||||
"""
|
||||
if created:
|
||||
taskresult = TaskResult.objects.create_task_result(
|
||||
"handle_mailbox_created",
|
||||
create_file_mailbox.s(instance.osuser.username, instance.username),
|
||||
)
|
||||
_LOGGER.info(
|
||||
"Mailbox creation has been requested in task %s", taskresult.task_id
|
||||
)
|
||||
_LOGGER.debug("mailbox %s has been %s", instance, created and "created" or "updated")
|
||||
|
||||
|
||||
@receiver(post_delete, sender=Mailbox)
|
||||
def handle_mailbox_deleted(sender, instance, **kwargs):
|
||||
"""
|
||||
Handles cleanup actions to be done after deletion of a
|
||||
:py:class:`Mailbox <managemails.models.Mailbox>` instance.
|
||||
|
||||
:param sender: sender of the signal
|
||||
:param instance: Mailbox instance
|
||||
|
||||
This signal handler starts a Celery_ task.
|
||||
|
||||
"""
|
||||
taskresult = TaskResult.objects.create_task_result(
|
||||
"handle_mailbox_deleted",
|
||||
delete_file_mailbox.s(instance.osuser.username, instance.username)
|
||||
)
|
||||
_LOGGER.info(
|
||||
"Mailbox deletion has been requested in task %s", taskresult.task_id
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue