From 9e5ca78d788be3d54e3797e1668c5fb29ec5e56f Mon Sep 17 00:00:00 2001 From: "Parth J. Kharwar" Date: Fri, 26 Feb 2021 13:03:42 +0530 Subject: [PATCH] feat: enables, disables and deletes notification settings when a user is modified [CU-j8wutt] (#9) --- frappe/core/doctype/user/user.py | 11 ++++++++++- .../notification_settings/notification_settings.py | 3 +++ .../doctype/energy_point_log/energy_point_log.py | 5 ++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/frappe/core/doctype/user/user.py b/frappe/core/doctype/user/user.py index 3f19a6ef7b..1e3a25678f 100644 --- a/frappe/core/doctype/user/user.py +++ b/frappe/core/doctype/user/user.py @@ -8,7 +8,7 @@ from frappe.utils import cint, flt, has_gravatar, escape_html, format_datetime, from frappe import throw, msgprint, _ from frappe.utils.password import update_password as _update_password, check_password from frappe.desk.notifications import clear_notifications -from frappe.desk.doctype.notification_settings.notification_settings import create_notification_settings +from frappe.desk.doctype.notification_settings.notification_settings import create_notification_settings, enable_disable_notifications from frappe.utils.user import get_system_managers from bs4 import BeautifulSoup import frappe.permissions @@ -141,11 +141,17 @@ class User(Document): if not cint(self.enabled): self.a_system_manager_should_exist() + # disable notifications if the user has been disabled + enable_disable_notifications(self.name, enabled=False) # clear sessions if disabled if not cint(self.enabled) and getattr(frappe.local, "login_manager", None): frappe.local.login_manager.logout(user=self.name) + # enable notifications if the user has been enabled + if cint(self.enabled): + enable_disable_notifications(self.name, enabled=True) + def add_system_manager_role(self): # if adding system manager, do nothing if not cint(self.enabled) or ("System Manager" in [user_role.role for user_role in @@ -358,6 +364,9 @@ class User(Document): set `user`=null where `user`=%s""", (self.name)) + # delete notification settings + frappe.delete_doc("Notification Settings", self.name, ignore_permissions=True) + def before_rename(self, old_name, new_name, merge=False): self.check_demo() diff --git a/frappe/desk/doctype/notification_settings/notification_settings.py b/frappe/desk/doctype/notification_settings/notification_settings.py index 34726bdf8a..4b32252ac8 100644 --- a/frappe/desk/doctype/notification_settings/notification_settings.py +++ b/frappe/desk/doctype/notification_settings/notification_settings.py @@ -43,6 +43,9 @@ def create_notification_settings(user): _doc.name = user _doc.insert(ignore_permissions=True) +def enable_disable_notifications(user, enabled): + frappe.set_value("Notification Settings", user, 'enabled', enabled) + @frappe.whitelist() def get_subscribed_documents(): diff --git a/frappe/social/doctype/energy_point_log/energy_point_log.py b/frappe/social/doctype/energy_point_log/energy_point_log.py index 21a0e24598..e9425cec86 100644 --- a/frappe/social/doctype/energy_point_log/energy_point_log.py +++ b/frappe/social/doctype/energy_point_log/energy_point_log.py @@ -319,7 +319,10 @@ def send_summary(timespan): from_date = getdate(from_date) to_date = getdate() - all_users = [user.email for user in get_enabled_system_users()] + + # select only those users that have energy point email notifications enabled + all_users = [user.email for user in get_enabled_system_users() if + is_email_notifications_enabled_for_type(user.name, 'Energy Point')] frappe.sendmail( subject = '{} energy points summary'.format(timespan),