# 63 Mail background sending

* moves mail sending to background using celery
This commit is contained in:
mpeltriaux 2022-01-12 14:27:39 +01:00
parent 9c4bab3800
commit dbf32f3138
5 changed files with 18 additions and 10 deletions

View File

@ -10,6 +10,8 @@ import uuid
from abc import abstractmethod from abc import abstractmethod
from django.contrib import messages from django.contrib import messages
from konova.tasks import celery_send_mail_shared_access_removed
from user.models import User from user.models import User
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpRequest from django.http import HttpRequest
@ -413,9 +415,9 @@ class ShareableObjectMixin(models.Model):
) )
removed_users = self.users.all().exclude( removed_users = self.users.all().exclude(
id__in=accessing_users id__in=accessing_users
) ).values("id")
for user in removed_users: 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) self.share_with_list(users)

View File

@ -3,11 +3,11 @@ from time import sleep
from celery import shared_task from celery import shared_task
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from konova.models import Geometry
@shared_task @shared_task
def celery_update_parcels(geometry_id: str, recheck: bool = True): def celery_update_parcels(geometry_id: str, recheck: bool = True):
from konova.models import Geometry
try: try:
geom = Geometry.objects.get(id=geometry_id) geom = Geometry.objects.get(id=geometry_id)
geom.parcels.clear() geom.parcels.clear()
@ -17,3 +17,9 @@ def celery_update_parcels(geometry_id: str, recheck: bool = True):
sleep(5) sleep(5)
celery_update_parcels(geometry_id, False) 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)

View File

@ -49,7 +49,7 @@ class Mailer:
auth_password=self.auth_password 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 """ Send a mail if user has no access to the object anymore
Args: Args:
@ -60,14 +60,14 @@ class Mailer:
""" """
context = { context = {
"user": user, "user": user,
"obj": obj, "obj_identifier": obj_identifier,
"EMAIL_REPLY_TO": EMAIL_REPLY_TO, "EMAIL_REPLY_TO": EMAIL_REPLY_TO,
} }
msg = render_to_string("email/sharing/shared_access_removed.html", context) msg = render_to_string("email/sharing/shared_access_removed.html", context)
user_mail_address = [user.email] user_mail_address = [user.email]
self.send( self.send(
user_mail_address, user_mail_address,
_("{} - Shared access removed").format(obj.identifier), _("{} - Shared access removed").format(obj_identifier),
msg msg
) )

View File

@ -2,14 +2,14 @@
<div> <div>
<h2>{% trans 'Shared access removed' %}</h2> <h2>{% trans 'Shared access removed' %}</h2>
<h4>{{obj.identifier}}</h4> <h4>{{obj_identifier}}</h4>
<hr> <hr>
<article> <article>
{% trans 'Hello ' %} {{user.username}}, {% trans 'Hello ' %} {{user.username}},
<br> <br>
{% trans 'your shared access, including editing, has been revoked for the dataset ' %} {% trans 'your shared access, including editing, has been revoked for the dataset ' %}
<br> <br>
<strong>'{{obj.identifier}}'</strong> <strong>'{{obj_identifier}}'</strong>
<br> <br>
{% trans 'However, you are still able to view the dataset content.' %} {% 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.' %} {% trans 'Please use the provided search filter on the dataset`s overview pages to find them.' %}

View File

@ -15,7 +15,7 @@ from konova.utils.mailer import Mailer
class User(AbstractUser): class User(AbstractUser):
notifications = models.ManyToManyField("user.UserNotification", related_name="+", blank=True) 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 """ Sends a mail to the user in case of removed shared access
Args: Args:
@ -25,4 +25,4 @@ class User(AbstractUser):
""" """
mailer = Mailer() mailer = Mailer()
mailer.send_mail_shared_access_removed(obj, self) mailer.send_mail_shared_access_removed(obj_identifier, self)