diff --git a/compensation/models/eco_account.py b/compensation/models/eco_account.py index 3d48b691..42a202f7 100644 --- a/compensation/models/eco_account.py +++ b/compensation/models/eco_account.py @@ -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) \ No newline at end of file + super().delete(*args, **kwargs) diff --git a/intervention/forms/modalForms.py b/intervention/forms/modalForms.py index 07911be9..b6445a55 100644 --- a/intervention/forms/modalForms.py +++ b/intervention/forms/modalForms.py @@ -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 diff --git a/konova/tasks.py b/konova/tasks.py index 798effb4..65801219 100644 --- a/konova/tasks.py +++ b/konova/tasks.py @@ -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) diff --git a/konova/utils/mailer.py b/konova/utils/mailer.py index dd8eef1f..92bd2b60 100644 --- a/konova/utils/mailer.py +++ b/konova/utils/mailer.py @@ -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 diff --git a/locale/de/LC_MESSAGES/django.mo b/locale/de/LC_MESSAGES/django.mo index d1cbdfba..6ee76a29 100644 Binary files a/locale/de/LC_MESSAGES/django.mo and b/locale/de/LC_MESSAGES/django.mo differ diff --git a/locale/de/LC_MESSAGES/django.po b/locale/de/LC_MESSAGES/django.po index 893d4384..b4b18615 100644 --- a/locale/de/LC_MESSAGES/django.po +++ b/locale/de/LC_MESSAGES/django.po @@ -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 \n" "Language-Team: LANGUAGE \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" diff --git a/templates/email/api/verify_token.html b/templates/email/api/verify_token.html index bd12a0fe..355647b2 100644 --- a/templates/email/api/verify_token.html +++ b/templates/email/api/verify_token.html @@ -6,6 +6,7 @@
{% trans 'Hello support' %},
+
{% trans 'you need to verify the API token for user' %}:

diff --git a/templates/email/checking/shared_data_checked.html b/templates/email/checking/shared_data_checked.html index 0b67ecc7..133dae2e 100644 --- a/templates/email/checking/shared_data_checked.html +++ b/templates/email/checking/shared_data_checked.html @@ -7,6 +7,7 @@
{% trans 'Hello ' %} {{user.username}},
+
{% trans 'the following dataset has just been checked' %}
{{obj_identifier}} diff --git a/templates/email/checking/shared_data_checked_team.html b/templates/email/checking/shared_data_checked_team.html index ee813811..6a7a4500 100644 --- a/templates/email/checking/shared_data_checked_team.html +++ b/templates/email/checking/shared_data_checked_team.html @@ -7,6 +7,7 @@
{% trans 'Hello team' %} {{team.name}},
+
{% trans 'the following dataset has just been checked' %}
{{obj_identifier}} diff --git a/templates/email/deleting/shared_data_deleted.html b/templates/email/deleting/shared_data_deleted.html index 272b0fde..7857444c 100644 --- a/templates/email/deleting/shared_data_deleted.html +++ b/templates/email/deleting/shared_data_deleted.html @@ -7,6 +7,7 @@
{% trans 'Hello ' %} {{user.username}},
+
{% trans 'the following dataset has just been deleted' %}
{{obj_identifier}} diff --git a/templates/email/deleting/shared_data_deleted_team.html b/templates/email/deleting/shared_data_deleted_team.html index cedb2a45..0ffa8bbf 100644 --- a/templates/email/deleting/shared_data_deleted_team.html +++ b/templates/email/deleting/shared_data_deleted_team.html @@ -7,6 +7,7 @@
{% trans 'Hello team' %} {{team.name}},
+
{% trans 'the following dataset has just been deleted' %}
{{obj_identifier}} diff --git a/templates/email/other/deduction_changed.html b/templates/email/other/deduction_changed.html new file mode 100644 index 00000000..8129f6b2 --- /dev/null +++ b/templates/email/other/deduction_changed.html @@ -0,0 +1,50 @@ +{% load i18n %} + +
+

{% translate 'Deduction changed' %}

+

{{obj_identifier}}

+
+
+ {% translate 'Hello ' %} {{user.username}}, +
+
+ {% translate 'a deduction of this eco account has changed:' %} +
+
+ + + + + + + + + + + + + + + + + + + + + +
{% translate 'Attribute' %}{% translate 'Old' %}{% translate 'New' %}
{% translate 'EcoAccount' %}{{data_changes.account.old}}{{data_changes.account.new}}
{% translate 'Intervention' %}{{data_changes.intervention.old}}{{data_changes.intervention.new}}
{% translate 'Surface' %}{{data_changes.surface.old}} m²{{data_changes.surface.new}} m²
+
+
+ {% translate 'If this should not have been happened, please contact us. See the signature for details.' %} +
+
+ {% translate 'Best regards' %} +
+ KSP +
+
+
+ {% include 'email/signature.html' %} +
+
+ diff --git a/templates/email/other/deduction_changed_team.html b/templates/email/other/deduction_changed_team.html new file mode 100644 index 00000000..babf36c0 --- /dev/null +++ b/templates/email/other/deduction_changed_team.html @@ -0,0 +1,50 @@ +{% load i18n %} + +
+

{% translate 'Deduction changed' %}

+

{{obj_identifier}}

+
+
+ {% trans 'Hello team' %} {{team.name}}, +
+
+ {% translate 'a deduction of this eco account has changed:' %} +
+
+ + + + + + + + + + + + + + + + + + + + + +
{% translate 'Attribute' %}{% translate 'Old' %}{% translate 'New' %}
{% translate 'EcoAccount' %}{{data_changes.account.old}}{{data_changes.account.new}}
{% translate 'Intervention' %}{{data_changes.intervention.old}}{{data_changes.intervention.new}}
{% translate 'Surface' %}{{data_changes.surface.old}} m²{{data_changes.surface.new}} m²
+
+
+ {% translate 'If this should not have been happened, please contact us. See the signature for details.' %} +
+
+ {% translate 'Best regards' %} +
+ KSP +
+
+
+ {% include 'email/signature.html' %} +
+
+ diff --git a/templates/email/recording/shared_data_recorded.html b/templates/email/recording/shared_data_recorded.html index 6805c928..ccad11e0 100644 --- a/templates/email/recording/shared_data_recorded.html +++ b/templates/email/recording/shared_data_recorded.html @@ -7,6 +7,7 @@
{% trans 'Hello ' %} {{user.username}},
+
{% trans 'the following dataset has just been recorded' %}
{{obj_identifier}} diff --git a/templates/email/recording/shared_data_recorded_team.html b/templates/email/recording/shared_data_recorded_team.html index 12efa8f6..0734bc62 100644 --- a/templates/email/recording/shared_data_recorded_team.html +++ b/templates/email/recording/shared_data_recorded_team.html @@ -7,6 +7,7 @@
{% trans 'Hello team' %} {{team.name}},
+
{% trans 'the following dataset has just been recorded' %}
{{obj_identifier}} diff --git a/templates/email/recording/shared_data_unrecorded.html b/templates/email/recording/shared_data_unrecorded.html index 1e0310ae..3406b750 100644 --- a/templates/email/recording/shared_data_unrecorded.html +++ b/templates/email/recording/shared_data_unrecorded.html @@ -7,6 +7,7 @@
{% trans 'Hello ' %} {{user.username}},
+
{% trans 'the following dataset has just been unrecorded' %}
{{obj_identifier}} diff --git a/templates/email/recording/shared_data_unrecorded_team.html b/templates/email/recording/shared_data_unrecorded_team.html index 64141555..5c0f8560 100644 --- a/templates/email/recording/shared_data_unrecorded_team.html +++ b/templates/email/recording/shared_data_unrecorded_team.html @@ -7,6 +7,7 @@
{% trans 'Hello team' %} {{team.name}},
+
{% trans 'the following dataset has just been unrecorded' %}
{{obj_identifier}} diff --git a/templates/email/sharing/shared_access_given.html b/templates/email/sharing/shared_access_given.html index 140e7a88..6ab759b3 100644 --- a/templates/email/sharing/shared_access_given.html +++ b/templates/email/sharing/shared_access_given.html @@ -7,6 +7,7 @@
{% trans 'Hello ' %} {{user.username}},
+
{% trans 'the following dataset has just been shared with you' %}
{{obj_identifier}} diff --git a/templates/email/sharing/shared_access_given_team.html b/templates/email/sharing/shared_access_given_team.html index 990ba2de..cdf3bb1c 100644 --- a/templates/email/sharing/shared_access_given_team.html +++ b/templates/email/sharing/shared_access_given_team.html @@ -7,6 +7,7 @@
{% trans 'Hello team' %} {{team.name}},
+
{% trans 'the following dataset has just been shared with your team' %}
{{obj_identifier}} diff --git a/templates/email/sharing/shared_access_removed.html b/templates/email/sharing/shared_access_removed.html index d1cbc5bf..b3121117 100644 --- a/templates/email/sharing/shared_access_removed.html +++ b/templates/email/sharing/shared_access_removed.html @@ -7,6 +7,7 @@
{% trans 'Hello ' %} {{user.username}},
+
{% trans 'your shared access, including editing, has been revoked for the dataset ' %}
{{obj_identifier}} diff --git a/templates/email/sharing/shared_access_removed_team.html b/templates/email/sharing/shared_access_removed_team.html index 5472ef73..992f00f8 100644 --- a/templates/email/sharing/shared_access_removed_team.html +++ b/templates/email/sharing/shared_access_removed_team.html @@ -7,6 +7,7 @@
{% trans 'Hello team' %} {{team.name}},
+
{% trans 'your teams shared access, including editing, has been revoked for the dataset ' %}
{{obj_identifier}} diff --git a/user/models/team.py b/user/models/team.py index 5e728e71..7162e977 100644 --- a/user/models/team.py +++ b/user/models/team.py @@ -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 diff --git a/user/models/user.py b/user/models/user.py index b40a3b1b..20d3d50b 100644 --- a/user/models/user.py +++ b/user/models/user.py @@ -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