From dbf32f31388cdbbce3a4e51a7b3ab51ab94b67d0 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Wed, 12 Jan 2022 14:27:39 +0100 Subject: [PATCH] # 63 Mail background sending * moves mail sending to background using celery --- konova/models/object.py | 6 ++++-- konova/tasks.py | 8 +++++++- konova/utils/mailer.py | 6 +++--- templates/email/sharing/shared_access_removed.html | 4 ++-- user/models/user.py | 4 ++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/konova/models/object.py b/konova/models/object.py index 23a21651..6451fb83 100644 --- a/konova/models/object.py +++ b/konova/models/object.py @@ -10,6 +10,8 @@ import uuid from abc import abstractmethod from django.contrib import messages + +from konova.tasks import celery_send_mail_shared_access_removed from user.models import User from django.core.exceptions import ObjectDoesNotExist from django.http import HttpRequest @@ -413,9 +415,9 @@ class ShareableObjectMixin(models.Model): ) removed_users = self.users.all().exclude( id__in=accessing_users - ) + ).values("id") for user in removed_users: - user.send_mail_shared_access_removed(self) + celery_send_mail_shared_access_removed.delay(self.identifier, user["id"]) self.share_with_list(users) diff --git a/konova/tasks.py b/konova/tasks.py index 2db4b19f..eb4e91d5 100644 --- a/konova/tasks.py +++ b/konova/tasks.py @@ -3,11 +3,11 @@ from time import sleep from celery import shared_task from django.core.exceptions import ObjectDoesNotExist -from konova.models import Geometry @shared_task def celery_update_parcels(geometry_id: str, recheck: bool = True): + from konova.models import Geometry try: geom = Geometry.objects.get(id=geometry_id) geom.parcels.clear() @@ -17,3 +17,9 @@ def celery_update_parcels(geometry_id: str, recheck: bool = True): sleep(5) celery_update_parcels(geometry_id, False) + +@shared_task +def celery_send_mail_shared_access_removed(obj_identifier, user_id): + from user.models import User + user = User.objects.get(id=user_id) + user.send_mail_shared_access_removed(obj_identifier) \ No newline at end of file diff --git a/konova/utils/mailer.py b/konova/utils/mailer.py index ab2ddcaf..f3f2efb0 100644 --- a/konova/utils/mailer.py +++ b/konova/utils/mailer.py @@ -49,7 +49,7 @@ class Mailer: auth_password=self.auth_password ) - def send_mail_shared_access_removed(self, obj, user): + def send_mail_shared_access_removed(self, obj_identifier, user): """ Send a mail if user has no access to the object anymore Args: @@ -60,14 +60,14 @@ class Mailer: """ context = { "user": user, - "obj": obj, + "obj_identifier": obj_identifier, "EMAIL_REPLY_TO": EMAIL_REPLY_TO, } msg = render_to_string("email/sharing/shared_access_removed.html", context) user_mail_address = [user.email] self.send( user_mail_address, - _("{} - Shared access removed").format(obj.identifier), + _("{} - Shared access removed").format(obj_identifier), msg ) diff --git a/templates/email/sharing/shared_access_removed.html b/templates/email/sharing/shared_access_removed.html index 529252c3..be6a4d6c 100644 --- a/templates/email/sharing/shared_access_removed.html +++ b/templates/email/sharing/shared_access_removed.html @@ -2,14 +2,14 @@

{% trans 'Shared access removed' %}

-

{{obj.identifier}}

+

{{obj_identifier}}


{% trans 'Hello ' %} {{user.username}},
{% trans 'your shared access, including editing, has been revoked for the dataset ' %}
- '{{obj.identifier}}' + '{{obj_identifier}}'
{% trans 'However, you are still able to view the dataset content.' %} {% trans 'Please use the provided search filter on the dataset`s overview pages to find them.' %} diff --git a/user/models/user.py b/user/models/user.py index 63f71e35..c55896ee 100644 --- a/user/models/user.py +++ b/user/models/user.py @@ -15,7 +15,7 @@ from konova.utils.mailer import Mailer class User(AbstractUser): notifications = models.ManyToManyField("user.UserNotification", related_name="+", blank=True) - def send_mail_shared_access_removed(self, obj): + def send_mail_shared_access_removed(self, obj_identifier): """ Sends a mail to the user in case of removed shared access Args: @@ -25,4 +25,4 @@ class User(AbstractUser): """ mailer = Mailer() - mailer.send_mail_shared_access_removed(obj, self) + mailer.send_mail_shared_access_removed(obj_identifier, self)