# 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 3531997080
commit 82778d1fec
5 changed files with 18 additions and 10 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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
)

View File

@ -2,14 +2,14 @@
<div>
<h2>{% trans 'Shared access removed' %}</h2>
<h4>{{obj.identifier}}</h4>
<h4>{{obj_identifier}}</h4>
<hr>
<article>
{% trans 'Hello ' %} {{user.username}},
<br>
{% trans 'your shared access, including editing, has been revoked for the dataset ' %}
<br>
<strong>'{{obj.identifier}}'</strong>
<strong>'{{obj_identifier}}'</strong>
<br>
{% 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.' %}

View File

@ -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)