From 420a9522c58bc6e8732a57dbd41a5dc45f101a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Wed, 26 Mar 2014 17:26:40 +0100 Subject: [PATCH] Refactor: create a notification service to use it outside of the ViewSet classes --- taiga/base/notifications/api.py | 29 ++++++++++++---------------- taiga/base/notifications/services.py | 19 ++++++++++++++++++ 2 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 taiga/base/notifications/services.py diff --git a/taiga/base/notifications/api.py b/taiga/base/notifications/api.py index 33147889..a717241b 100644 --- a/taiga/base/notifications/api.py +++ b/taiga/base/notifications/api.py @@ -1,22 +1,13 @@ # -*- coding: utf-8 -*- -from djmail import template_mail +from . import services class NotificationSenderMixin(object): create_notification_template = None update_notification_template = None destroy_notification_template = None - - def _send_notification_email(self, template_method, users=None, context=None): - mails = template_mail.MagicMailBuilder() - for user in users: - email = getattr(mails, template_method)(user, context) - email.send() - - def post_save(self, obj, created=False): - super().post_save(obj, created) - self._post_save_notification_sender(obj, created) + notification_service = services.NotificationService() def _post_save_notification_sender(self, obj, created=False): users = obj.get_watchers_to_notify(self.request.user) @@ -24,22 +15,26 @@ class NotificationSenderMixin(object): context = {'changer': self.request.user, "comment": comment, 'object': obj} if created: - self._send_notification_email(self.create_notification_template, - users=users, context=context) + self.notification_service.send_notification_email(self.create_notification_template, + users=users, context=context) else: changed_fields = obj.get_changed_fields_list(self.request.DATA) if changed_fields: context["changed_fields"] = changed_fields - self._send_notification_email(self.update_notification_template, - users=users, context=context) + self.notification_service.send_notification_email(self.update_notification_template, + users=users, context=context) + + def post_save(self, obj, created=False): + super().post_save(obj, created) + self._post_save_notification_sender(obj, created) def destroy(self, request, *args, **kwargs): obj = self.get_object() users = obj.get_watchers_to_notify(self.request.user) context = {'changer': self.request.user, 'object': obj} - self._send_notification_email(self.destroy_notification_template, - users=users, context=context) + self.notification_service.send_notification_email(self.destroy_notification_template, + users=users, context=context) return super().destroy(request, *args, **kwargs) diff --git a/taiga/base/notifications/services.py b/taiga/base/notifications/services.py new file mode 100644 index 00000000..dfd59674 --- /dev/null +++ b/taiga/base/notifications/services.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +from djmail import template_mail + +import collections + + +class NotificationService(object): + def send_notification_email(self, template_method, users=None, context=None): + if not users: + return + + if not isinstance(users, collections.Iterable): + users = (users,) + + mails = template_mail.MagicMailBuilder() + for user in users: + email = getattr(mails, template_method)(user, context) + email.send()