From 829c145749bd748eb5b2418d4681884d7786dce4 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Mon, 14 Nov 2022 07:19:04 +0100 Subject: [PATCH] Team mail fix * filters team members by notification settings before sending team mails --- konova/utils/mailer.py | 45 ++++++++++++++++++++++++++++-------------- user/models/team.py | 36 ++++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/konova/utils/mailer.py b/konova/utils/mailer.py index 8de91198..65bbbe61 100644 --- a/konova/utils/mailer.py +++ b/konova/utils/mailer.py @@ -92,11 +92,14 @@ class Mailer: msg ) - def send_mail_shared_access_given_team(self, obj_identifier, obj_title, team): + def send_mail_shared_access_given_team(self, obj_identifier, obj_title, team, users_to_notify): """ Send a mail if a team just got access to the object Args: obj_identifier (str): The object identifier + obj_title (str): Title of the main object + team (Team): Team to be notified + users_to_notify (QueryDict): Contains the team users which should be notified Returns: @@ -108,18 +111,21 @@ class Mailer: "EMAIL_REPLY_TO": EMAIL_REPLY_TO, } msg = render_to_string("email/sharing/shared_access_given_team.html", context) - user_mail_address = team.users.values_list("email", flat=True) + user_mail_address = users_to_notify.values_list("email", flat=True) self.send( user_mail_address, _("{} - Shared access given").format(obj_identifier), msg ) - def send_mail_shared_access_removed_team(self, obj_identifier, obj_title, team): + def send_mail_shared_access_removed_team(self, obj_identifier, obj_title, team, users_to_notify): """ Send a mail if a team just lost access to the object Args: obj_identifier (str): The object identifier + obj_title (str): Title of the main object + team (Team): Team to be notified + users_to_notify (QueryDict): Contains the team users which should be notified Returns: @@ -131,18 +137,21 @@ class Mailer: "EMAIL_REPLY_TO": EMAIL_REPLY_TO, } msg = render_to_string("email/sharing/shared_access_removed_team.html", context) - user_mail_address = team.users.values_list("email", flat=True) + user_mail_address = users_to_notify.values_list("email", flat=True) self.send( user_mail_address, _("{} - Shared access removed").format(obj_identifier), msg ) - def send_mail_shared_data_unrecorded_team(self, obj_identifier, obj_title, team): + def send_mail_shared_data_unrecorded_team(self, obj_identifier, obj_title, team, users_to_notify): """ Send a mail if data has just been unrecorded Args: obj_identifier (str): The object identifier + obj_title (str): Title of the main object + team (Team): Team to be notified + users_to_notify (QueryDict): Contains the team users which should be notified Returns: @@ -154,18 +163,21 @@ class Mailer: "EMAIL_REPLY_TO": EMAIL_REPLY_TO, } msg = render_to_string("email/recording/shared_data_unrecorded_team.html", context) - user_mail_address = team.users.values_list("email", flat=True) + user_mail_address = users_to_notify.values_list("email", flat=True) self.send( user_mail_address, _("{} - Shared data unrecorded").format(obj_identifier), msg ) - def send_mail_shared_data_recorded_team(self, obj_identifier, obj_title, team): + def send_mail_shared_data_recorded_team(self, obj_identifier, obj_title, team, users_to_notify): """ Send a mail if data has just been recorded Args: obj_identifier (str): The object identifier + obj_title (str): Title of the main object + team (Team): Team to be notified + users_to_notify (QueryDict): Contains the team users which should be notified Returns: @@ -177,18 +189,21 @@ class Mailer: "EMAIL_REPLY_TO": EMAIL_REPLY_TO, } msg = render_to_string("email/recording/shared_data_recorded_team.html", context) - user_mail_address = team.users.values_list("email", flat=True) + user_mail_address = users_to_notify.values_list("email", flat=True) self.send( user_mail_address, _("{} - Shared data recorded").format(obj_identifier), msg ) - def send_mail_shared_data_checked_team(self, obj_identifier, obj_title, team): + def send_mail_shared_data_checked_team(self, obj_identifier, obj_title, team, users_to_notify): """ Send a mail if data has just been checked Args: obj_identifier (str): The object identifier + obj_title (str): Title of the main object + team (Team): Team to be notified + users_to_notify (QueryDict): Contains the team users which should be notified Returns: @@ -200,14 +215,14 @@ class Mailer: "EMAIL_REPLY_TO": EMAIL_REPLY_TO, } msg = render_to_string("email/checking/shared_data_checked_team.html", context) - user_mail_address = team.users.values_list("email", flat=True) + user_mail_address = users_to_notify.values_list("email", flat=True) self.send( user_mail_address, _("{} - Shared data checked").format(obj_identifier), msg ) - def send_mail_deduction_changed_team(self, obj_identifier, obj_title, team, data_changes): + def send_mail_deduction_changed_team(self, obj_identifier, obj_title, team, data_changes, users_to_notify): """ Send a mail if deduction has been changed Args: @@ -215,7 +230,7 @@ class Mailer: 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 - + users_to_notify (QueryDict): Contains the team users which should be notified Returns: """ @@ -227,14 +242,14 @@ class Mailer: "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) + user_mail_address = users_to_notify.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): + def send_mail_shared_data_deleted_team(self, obj_identifier, obj_title, team, users_to_notify): """ Send a mail if data has just been deleted Args: @@ -250,7 +265,7 @@ class Mailer: "EMAIL_REPLY_TO": EMAIL_REPLY_TO, } msg = render_to_string("email/deleting/shared_data_deleted_team.html", context) - user_mail_address = team.users.values_list("email", flat=True) + user_mail_address = users_to_notify.values_list("email", flat=True) self.send( user_mail_address, _("{} - Shared data deleted").format(obj_identifier), diff --git a/user/models/team.py b/user/models/team.py index 7162e977..b4bc26c1 100644 --- a/user/models/team.py +++ b/user/models/team.py @@ -2,6 +2,7 @@ from django.db import models from konova.models import UuidModel, DeletableObjectMixin from konova.utils.mailer import Mailer +from user.enums import UserNotificationEnum from user.models import UserActionLogEntry @@ -41,7 +42,10 @@ class Team(UuidModel, DeletableObjectMixin): """ mailer = Mailer() - mailer.send_mail_shared_access_given_team(obj_identifier, obj_title, self) + users_to_notify = self.users.filter( + notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_ACCESS_GAINED.value] + ) + mailer.send_mail_shared_access_given_team(obj_identifier, obj_title, self, users_to_notify) def send_mail_shared_access_removed(self, obj_identifier, obj_title): """ Sends a mail to the team members in case of removed shared access @@ -54,7 +58,10 @@ class Team(UuidModel, DeletableObjectMixin): """ mailer = Mailer() - mailer.send_mail_shared_access_removed_team(obj_identifier, obj_title, self) + users_to_notify = self.users.filter( + notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_ACCESS_REMOVED.value] + ) + mailer.send_mail_shared_access_removed_team(obj_identifier, obj_title, self, users_to_notify) def send_mail_shared_data_unrecorded(self, obj_identifier, obj_title): """ Sends a mail to the team members in case of unrecorded data @@ -67,7 +74,10 @@ class Team(UuidModel, DeletableObjectMixin): """ mailer = Mailer() - mailer.send_mail_shared_data_unrecorded_team(obj_identifier, obj_title, self) + users_to_notify = self.users.filter( + notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_RECORDED.value] + ) + mailer.send_mail_shared_data_unrecorded_team(obj_identifier, obj_title, self, users_to_notify) def send_mail_shared_data_recorded(self, obj_identifier, obj_title): """ Sends a mail to the team members in case of unrecorded data @@ -80,7 +90,10 @@ class Team(UuidModel, DeletableObjectMixin): """ mailer = Mailer() - mailer.send_mail_shared_data_recorded_team(obj_identifier, obj_title, self) + users_to_notify = self.users.filter( + notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_RECORDED.value] + ) + mailer.send_mail_shared_data_recorded_team(obj_identifier, obj_title, self, users_to_notify) def send_mail_shared_data_checked(self, obj_identifier, obj_title): """ Sends a mail to the team members in case of checked data @@ -93,7 +106,10 @@ class Team(UuidModel, DeletableObjectMixin): """ mailer = Mailer() - mailer.send_mail_shared_data_checked_team(obj_identifier, obj_title, self) + users_to_notify = self.users.filter( + notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_CHECKED.value] + ) + mailer.send_mail_shared_data_checked_team(obj_identifier, obj_title, self, users_to_notify) 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 @@ -107,7 +123,10 @@ class Team(UuidModel, DeletableObjectMixin): """ mailer = Mailer() - mailer.send_mail_deduction_changed_team(obj_identifier, obj_title, self, data_changes) + users_to_notify = self.users.filter( + notifications__in=[UserNotificationEnum.NOTIFY_ON_DEDUCTION_CHANGES.value] + ) + mailer.send_mail_deduction_changed_team(obj_identifier, obj_title, self, data_changes, users_to_notify) def send_mail_shared_data_deleted(self, obj_identifier, obj_title): """ Sends a mail to the team members in case of deleted data @@ -120,7 +139,10 @@ class Team(UuidModel, DeletableObjectMixin): """ mailer = Mailer() - mailer.send_mail_shared_data_deleted_team(obj_identifier, obj_title, self) + users_to_notify = self.users.filter( + notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_DELETED.value] + ) + mailer.send_mail_shared_data_deleted_team(obj_identifier, obj_title, self, users_to_notify) def remove_user(self, user): """ Removes a user from the team