From 886e4554bc1fa5232c2530b85cb5bcfa8ab24286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Tue, 29 Oct 2013 17:27:51 +0100 Subject: [PATCH] Now only sent emails when: - any notifiable_fields is update - an object is create - an object is delete --- greenmine/base/notifications/api.py | 11 +++++++---- greenmine/base/notifications/models.py | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/greenmine/base/notifications/api.py b/greenmine/base/notifications/api.py index 3fbfed6a..7248cd5f 100644 --- a/greenmine/base/notifications/api.py +++ b/greenmine/base/notifications/api.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*-i +# -*- coding: utf-8 -*- from djmail import template_mail @@ -15,14 +15,17 @@ class NotificationSenderMixin(object): email.send() def post_save(self, obj, created=False): + super().post_save(obj, created) + users = obj.get_watchers_to_notify(self.request.user) context = {'changer': self.request.user, 'object': obj} + changed_fields_dict = obj.get_changed_fields_dict(self.request.DATA) if created: self._send_notification_email(self.create_notification_template, users=users, context=context) - else: - context["changed_fields_dict"] = obj.get_changed_fields_dict(self.request.DATA) + elif changed_fields_dict: + context["changed_fields_dict"] = changed_fields_dict self._send_notification_email(self.update_notification_template, users=users, context=context) @@ -34,4 +37,4 @@ class NotificationSenderMixin(object): self._send_notification_email(self.destroy_notification_template, users=users, context=context) - return super(NotificationSenderMixin, self).destroy(request, *args, **kwargs) + return super().destroy(request, *args, **kwargs) diff --git a/greenmine/base/notifications/models.py b/greenmine/base/notifications/models.py index 28d871c5..42bf49c1 100644 --- a/greenmine/base/notifications/models.py +++ b/greenmine/base/notifications/models.py @@ -67,13 +67,13 @@ class WatchedMixin(models.Model): def get_changed_fields_dict(self, data_dict): if self.notifiable_fields: - changed_data = {k: v for k, v in data_dict.items() + changed_data = {k:v for k, v in data_dict.items() if k in self.notifiable_fields} else: changed_data = data_dict field_dict = {} - for field_name, data_value in data_dict.items(): + for field_name, data_value in changed_data.items(): field_dict.update(self._get_changed_field(field_name, data_value)) return field_dict