Mail sending

* adds mail sending logic for new notification setting
* adds new templates for user and team based sending
* enhances all email template layout
* adds translations
pull/195/head
mpeltriaux 2 years ago
parent 4138481a1b
commit ff26019b6e

@ -21,6 +21,7 @@ from compensation.models.compensation import AbstractCompensation, PikMixin
from compensation.utils.quality import EcoAccountQualityChecker
from konova.models import ShareableObjectMixin, RecordableObjectMixin, AbstractDocument, BaseResource, \
generate_document_file_upload_path
from konova.tasks import celery_send_mail_deduction_changed, celery_send_mail_deduction_changed_team
class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMixin, PikMixin):
@ -161,6 +162,25 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
"""
return reverse("compensation:acc:share", args=(self.id, self.access_token))
def send_notification_mail_on_deduction_change(self, data_change: dict):
""" Sends notification mails for changes on the deduction
Args:
data_change ():
Returns:
"""
# Send mail
shared_users = self.shared_users.values_list("id", flat=True)
for user_id in shared_users:
celery_send_mail_deduction_changed.delay(self.identifier, self.title, user_id, data_change)
# Send mail
shared_teams = self.shared_teams.values_list("id", flat=True)
for team_id in shared_teams:
celery_send_mail_deduction_changed_team.delay(self.identifier, self.title, team_id, data_change)
class EcoAccountDocument(AbstractDocument):
"""
@ -251,4 +271,4 @@ class EcoAccountDeduction(BaseResource):
if user is not None:
self.intervention.mark_as_edited(user, edit_comment=DEDUCTION_REMOVED)
self.account.mark_as_edited(user, edit_comment=DEDUCTION_REMOVED)
super().delete(*args, **kwargs)
super().delete(*args, **kwargs)

@ -508,28 +508,44 @@ class EditEcoAccountDeductionModalForm(NewDeductionModalForm):
deduction = self.deduction
form_account = self.cleaned_data.get("account", None)
form_intervention = self.cleaned_data.get("intervention", None)
current_account = deduction.account
current_intervention = deduction.intervention
old_account = deduction.account
old_intervention = deduction.intervention
old_surface = deduction.surface
# If account or intervention has been changed, we put that change in the logs just as if the deduction has
# been removed for this entry. Act as if the deduction is newly created for the new entries
if current_account != form_account:
current_account.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_REMOVED)
if old_account != form_account:
old_account.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_REMOVED)
form_account.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_ADDED)
else:
current_account.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_EDITED)
old_account.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_EDITED)
if current_intervention != form_intervention:
current_intervention.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_REMOVED)
if old_intervention != form_intervention:
old_intervention.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_REMOVED)
form_intervention.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_ADDED)
else:
current_intervention.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_EDITED)
old_intervention.mark_as_edited(self.user, self.request, edit_comment=DEDUCTION_EDITED)
deduction.account = form_account
deduction.intervention = self.cleaned_data.get("intervention", None)
deduction.surface = self.cleaned_data.get("surface", None)
deduction.save()
data_changes = {
"surface": {
"old": old_surface,
"new": deduction.surface,
},
"intervention": {
"old": old_intervention.identifier,
"new": deduction.intervention.identifier,
},
"account": {
"old": old_account.identifier,
"new": deduction.account.identifier,
}
}
old_account.send_notification_mail_on_deduction_change(data_changes)
return deduction

@ -106,3 +106,17 @@ def celery_send_mail_shared_data_checked_team(obj_identifier, obj_title=None, te
from user.models import Team
team = Team.objects.get(id=team_id)
team.send_mail_shared_data_checked(obj_identifier, obj_title)
@shared_task
def celery_send_mail_deduction_changed_team(obj_identifier, obj_title=None, team_id=None, data_changes=None):
from user.models import Team
team = Team.objects.get(id=team_id)
team.send_mail_deduction_changed(obj_identifier, obj_title, data_changes)
@shared_task
def celery_send_mail_deduction_changed(obj_identifier, obj_title=None, user_id=None, data_changes=None):
from user.models import User
user = User.objects.get(id=user_id)
user.send_mail_deduction_changed(obj_identifier, obj_title, data_changes)

@ -207,6 +207,33 @@ class Mailer:
msg
)
def send_mail_deduction_changed_team(self, obj_identifier, obj_title, team, data_changes):
""" Send a mail if deduction has been changed
Args:
obj_identifier (str): Identifier of the main object
obj_title (str): Title of the main object
team (Team): Team to be notified
data_changes (dict): Contains the old|new changes of the deduction changes
Returns:
"""
context = {
"team": team,
"obj_identifier": obj_identifier,
"obj_title": obj_title,
"EMAIL_REPLY_TO": EMAIL_REPLY_TO,
"data_changes": data_changes,
}
msg = render_to_string("email/other/deduction_changed_team.html", context)
user_mail_address = team.users.values_list("email", flat=True)
self.send(
user_mail_address,
_("{} - Deduction changed").format(obj_identifier),
msg
)
def send_mail_shared_data_deleted_team(self, obj_identifier, obj_title, team):
""" Send a mail if data has just been deleted
@ -322,6 +349,34 @@ class Mailer:
msg
)
def send_mail_deduction_changed(self, obj_identifier, obj_title, user, data_changes):
""" Send a mail if deduction has been changed
Args:
obj_identifier (str): Identifier of the main object
obj_title (str): Title of the main object
user (User): User to be notified
data_changes (dict): Contains the old|new changes of the deduction changes
Returns:
"""
context = {
"user": user,
"obj_identifier": obj_identifier,
"obj_title": obj_title,
"EMAIL_REPLY_TO": EMAIL_REPLY_TO,
"data_changes": data_changes,
}
msg = render_to_string("email/other/deduction_changed.html", context)
user_mail_address = [user.email]
self.send(
user_mail_address,
_("{} - Deduction changed").format(obj_identifier),
msg
)
def send_mail_verify_api_token(self, user):
""" Send a mail if a user creates a new token

Binary file not shown.

@ -26,7 +26,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-08-10 08:01+0200\n"
"POT-Creation-Date: 2022-08-10 08:37+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -241,6 +241,7 @@ msgstr ""
#: ema/templates/ema/detail/includes/states-after.html:36
#: ema/templates/ema/detail/includes/states-before.html:36
#: intervention/forms/modalForms.py:364
#: templates/email/other/deduction_changed.html:29
msgid "Surface"
msgstr "Fläche"
@ -307,6 +308,7 @@ msgstr "Typ"
#: intervention/forms/modalForms.py:382 intervention/tables.py:87
#: intervention/templates/intervention/detail/view.html:19
#: konova/templates/konova/includes/quickstart/interventions.html:4
#: templates/email/other/deduction_changed.html:24
#: templates/navbars/navbar.html:22
msgid "Intervention"
msgstr "Eingriff"
@ -696,14 +698,14 @@ msgstr ""
msgid "Pieces"
msgstr "Stück"
#: compensation/models/eco_account.py:55
#: compensation/models/eco_account.py:56
msgid ""
"Deductable surface can not be larger than existing surfaces in after states"
msgstr ""
"Die abbuchbare Fläche darf die Gesamtfläche der Zielzustände nicht "
"überschreiten"
#: compensation/models/eco_account.py:62
#: compensation/models/eco_account.py:63
msgid ""
"Deductable surface can not be smaller than the sum of already existing "
"deductions. Please contact the responsible users for the deductions!"
@ -1915,23 +1917,27 @@ msgstr "{} - Zugriff entzogen"
msgid "{} - Shared access given"
msgstr "{} - Zugriff freigegeben"
#: konova/utils/mailer.py:160 konova/utils/mailer.py:275
#: konova/utils/mailer.py:160 konova/utils/mailer.py:302
msgid "{} - Shared data unrecorded"
msgstr "{} - Freigegebene Daten entzeichnet"
#: konova/utils/mailer.py:183 konova/utils/mailer.py:252
#: konova/utils/mailer.py:183 konova/utils/mailer.py:279
msgid "{} - Shared data recorded"
msgstr "{} - Freigegebene Daten verzeichnet"
#: konova/utils/mailer.py:206 konova/utils/mailer.py:321
#: konova/utils/mailer.py:206 konova/utils/mailer.py:348
msgid "{} - Shared data checked"
msgstr "{} - Freigegebene Daten geprüft"
#: konova/utils/mailer.py:229 konova/utils/mailer.py:298
#: konova/utils/mailer.py:233 konova/utils/mailer.py:372
msgid "{} - Deduction changed"
msgstr "{} - Abbuchung geändert"
#: konova/utils/mailer.py:256 konova/utils/mailer.py:325
msgid "{} - Shared data deleted"
msgstr "{} - Freigegebene Daten gelöscht"
#: konova/utils/mailer.py:342 templates/email/api/verify_token.html:4
#: konova/utils/mailer.py:393 templates/email/api/verify_token.html:4
msgid "Request for new API token"
msgstr "Anfrage für neuen API Token"
@ -2235,6 +2241,7 @@ msgstr ""
#: templates/email/checking/shared_data_checked_team.html:19
#: templates/email/deleting/shared_data_deleted.html:19
#: templates/email/deleting/shared_data_deleted_team.html:19
#: templates/email/other/deduction_changed.html:38
#: templates/email/recording/shared_data_recorded.html:19
#: templates/email/recording/shared_data_recorded_team.html:19
#: templates/email/recording/shared_data_unrecorded.html:19
@ -2253,6 +2260,7 @@ msgstr "Freigegebene Daten geprüft"
#: templates/email/checking/shared_data_checked.html:8
#: templates/email/deleting/shared_data_deleted.html:8
#: templates/email/other/deduction_changed.html:8
#: templates/email/recording/shared_data_recorded.html:8
#: templates/email/recording/shared_data_unrecorded.html:8
#: templates/email/sharing/shared_access_given.html:8
@ -2295,6 +2303,7 @@ msgstr "der folgende Datensatz wurde soeben gelöscht "
#: templates/email/deleting/shared_data_deleted.html:16
#: templates/email/deleting/shared_data_deleted_team.html:16
#: templates/email/other/deduction_changed.html:35
msgid ""
"If this should not have been happened, please contact us. See the signature "
"for details."
@ -2302,6 +2311,31 @@ msgstr ""
"Falls das nicht hätte passieren dürfen, kontaktieren Sie uns bitte. In der E-"
"mail Signatur finden Sie weitere Kontaktinformationen."
#: templates/email/other/deduction_changed.html:4
msgid "Deduction changed"
msgstr "Abbuchung geändert"
#: templates/email/other/deduction_changed.html:10
msgid "a deduction of this eco account has changed:"
msgstr "eine Abbuchung des Ökokontos hat sich geändert:"
#: templates/email/other/deduction_changed.html:14
msgid "Attribute"
msgstr "Attribute"
#: templates/email/other/deduction_changed.html:15
msgid "Old"
msgstr "Alt"
#: templates/email/other/deduction_changed.html:16
#: templates/generic_index.html:43 user/templates/user/team/index.html:22
msgid "New"
msgstr "Neu"
#: templates/email/other/deduction_changed.html:19
msgid "EcoAccount"
msgstr "Ökokonto"
#: templates/email/recording/shared_data_recorded.html:4
#: templates/email/recording/shared_data_recorded_team.html:4
msgid "Shared data recorded"
@ -2499,10 +2533,6 @@ msgstr "* sind Pflichtfelder."
msgid "New entry"
msgstr "Neuer Eintrag"
#: templates/generic_index.html:43 user/templates/user/team/index.html:22
msgid "New"
msgstr "Neu"
#: templates/generic_index.html:58
msgid "Search for keywords"
msgstr "Nach Schlagwörtern suchen"

@ -6,6 +6,7 @@
<article>
{% trans 'Hello support' %},
<br>
<br>
{% trans 'you need to verify the API token for user' %}:
<br>
<br>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello ' %} {{user.username}},
<br>
<br>
{% trans 'the following dataset has just been checked' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello team' %} {{team.name}},
<br>
<br>
{% trans 'the following dataset has just been checked' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello ' %} {{user.username}},
<br>
<br>
{% trans 'the following dataset has just been deleted' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello team' %} {{team.name}},
<br>
<br>
{% trans 'the following dataset has just been deleted' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -0,0 +1,50 @@
{% load i18n %}
<div>
<h2>{% translate 'Deduction changed' %}</h2>
<h4>{{obj_identifier}}</h4>
<hr>
<article>
{% translate 'Hello ' %} {{user.username}},
<br>
<br>
{% translate 'a deduction of this eco account has changed:' %}
<br>
<br>
<table>
<tr>
<th scope="col">{% translate 'Attribute' %}</th>
<th scope="col">{% translate 'Old' %}</th>
<th scope="col">{% translate 'New' %}</th>
</tr>
<tr>
<td>{% translate 'EcoAccount' %}</td>
<td>{{data_changes.account.old}}</td>
<td>{{data_changes.account.new}}</td>
</tr>
<tr>
<td>{% translate 'Intervention' %}</td>
<td>{{data_changes.intervention.old}}</td>
<td>{{data_changes.intervention.new}}</td>
</tr>
<tr>
<td>{% translate 'Surface' %}</td>
<td>{{data_changes.surface.old}} m²</td>
<td>{{data_changes.surface.new}} m²</td>
</tr>
</table>
<br>
<br>
{% translate 'If this should not have been happened, please contact us. See the signature for details.' %}
<br>
<br>
{% translate 'Best regards' %}
<br>
KSP
<br>
<br>
<br>
{% include 'email/signature.html' %}
</article>
</div>

@ -0,0 +1,50 @@
{% load i18n %}
<div>
<h2>{% translate 'Deduction changed' %}</h2>
<h4>{{obj_identifier}}</h4>
<hr>
<article>
{% trans 'Hello team' %} {{team.name}},
<br>
<br>
{% translate 'a deduction of this eco account has changed:' %}
<br>
<br>
<table>
<tr>
<th scope="col">{% translate 'Attribute' %}</th>
<th scope="col">{% translate 'Old' %}</th>
<th scope="col">{% translate 'New' %}</th>
</tr>
<tr>
<td>{% translate 'EcoAccount' %}</td>
<td>{{data_changes.account.old}}</td>
<td>{{data_changes.account.new}}</td>
</tr>
<tr>
<td>{% translate 'Intervention' %}</td>
<td>{{data_changes.intervention.old}}</td>
<td>{{data_changes.intervention.new}}</td>
</tr>
<tr>
<td>{% translate 'Surface' %}</td>
<td>{{data_changes.surface.old}} m²</td>
<td>{{data_changes.surface.new}} m²</td>
</tr>
</table>
<br>
<br>
{% translate 'If this should not have been happened, please contact us. See the signature for details.' %}
<br>
<br>
{% translate 'Best regards' %}
<br>
KSP
<br>
<br>
<br>
{% include 'email/signature.html' %}
</article>
</div>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello ' %} {{user.username}},
<br>
<br>
{% trans 'the following dataset has just been recorded' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello team' %} {{team.name}},
<br>
<br>
{% trans 'the following dataset has just been recorded' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello ' %} {{user.username}},
<br>
<br>
{% trans 'the following dataset has just been unrecorded' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello team' %} {{team.name}},
<br>
<br>
{% trans 'the following dataset has just been unrecorded' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello ' %} {{user.username}},
<br>
<br>
{% trans 'the following dataset has just been shared with you' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello team' %} {{team.name}},
<br>
<br>
{% trans 'the following dataset has just been shared with your team' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello ' %} {{user.username}},
<br>
<br>
{% trans 'your shared access, including editing, has been revoked for the dataset ' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -7,6 +7,7 @@
<article>
{% trans 'Hello team' %} {{team.name}},
<br>
<br>
{% trans 'your teams shared access, including editing, has been revoked for the dataset ' %}
<br>
<strong>{{obj_identifier}}</strong>

@ -95,6 +95,20 @@ class Team(UuidModel, DeletableObjectMixin):
mailer = Mailer()
mailer.send_mail_shared_data_checked_team(obj_identifier, obj_title, self)
def send_mail_deduction_changed(self, obj_identifier, obj_title, data_changes):
""" Sends a mail to the team members in case of changed deduction values
Args:
obj_identifier (str): Identifier of the main object
obj_title (str): Title of the main object
data_changes (dict): Contains the old|new changes of the deduction changes
Returns:
"""
mailer = Mailer()
mailer.send_mail_deduction_changed_team(obj_identifier, obj_title, self, data_changes)
def send_mail_shared_data_deleted(self, obj_identifier, obj_title):
""" Sends a mail to the team members in case of deleted data

@ -145,6 +145,22 @@ class User(AbstractUser):
mailer = Mailer()
mailer.send_mail_shared_data_checked(obj_identifier, obj_title, self)
def send_mail_deduction_changed(self, obj_identifier, obj_title, data_changes):
""" Sends a mail to the user in case of a changed deduction
Args:
obj_identifier (str): Identifier of the main object
obj_title (str): Title of the main object
data_changes (dict): Contains the old|new changes of the deduction changes
Returns:
"""
notification_set = self.is_notification_setting_set(UserNotificationEnum.NOTIFY_ON_DEDUCTION_CHANGES)
if notification_set:
mailer = Mailer()
mailer.send_mail_deduction_changed(obj_identifier, obj_title, self, data_changes)
def get_API_token(self):
""" Getter for an API token

Loading…
Cancel
Save