From 279be874485ae6379b89c6c01fb15d4b8f15c716 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 2 Apr 2022 02:46:41 +0200 Subject: [PATCH] Move email notification method into plugin Fixes #2385 --- InvenTree/common/notifications.py | 42 -------------- .../builtin/integration/core_notifications.py | 55 +++++++++++++++++++ InvenTree/plugin/mixins/__init__.py | 3 + 3 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 InvenTree/plugin/builtin/integration/core_notifications.py diff --git a/InvenTree/common/notifications.py b/InvenTree/common/notifications.py index fe737fc919..6e87797eb8 100644 --- a/InvenTree/common/notifications.py +++ b/InvenTree/common/notifications.py @@ -1,16 +1,9 @@ import logging from datetime import timedelta -from django.template.loader import render_to_string - -from allauth.account.models import EmailAddress - from InvenTree.helpers import inheritors from InvenTree.ready import isImportingData from common.models import NotificationEntry, NotificationMessage -from common.models import InvenTreeUserSetting - -import InvenTree.tasks logger = logging.getLogger('inventree') @@ -101,41 +94,6 @@ class BulkNotificationMethod(NotificationMethod): raise NotImplementedError('The `send` method must be overriden!') -class EmailNotification(BulkNotificationMethod): - METHOD_NAME = 'mail' - CONTEXT_EXTRA = [ - ('template', ), - ('template', 'html', ), - ('template', 'subject', ), - ] - - def get_targets(self): - """ - Return a list of target email addresses, - only for users which allow email notifications - """ - - allowed_users = [] - - for user in self.targets: - allows_emails = InvenTreeUserSetting.get_setting('NOTIFICATION_SEND_EMAILS', user=user) - - if allows_emails: - allowed_users.append(user) - - return EmailAddress.objects.filter( - user__in=allowed_users, - ) - - def send_bulk(self): - html_message = render_to_string(self.context['template']['html'], self.context) - targets = self.get_targets().values_list('email', flat=True) - - InvenTree.tasks.send_email(self.context['template']['subject'], '', targets, html_message=html_message) - - return True - - class UIMessageNotification(SingleNotificationMethod): METHOD_NAME = 'ui_message' diff --git a/InvenTree/plugin/builtin/integration/core_notifications.py b/InvenTree/plugin/builtin/integration/core_notifications.py new file mode 100644 index 0000000000..18c62cfdf0 --- /dev/null +++ b/InvenTree/plugin/builtin/integration/core_notifications.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +"""Core set of Notifications as a Plugin""" +from django.template.loader import render_to_string +from django.utils.translation import ugettext_lazy as _ + +from allauth.account.models import EmailAddress + +from plugin import IntegrationPluginBase +from plugin.mixins import BulkNotificationMethod +from common.models import InvenTreeUserSetting +import InvenTree.tasks + + +class CoreNotificationsPlugin(IntegrationPluginBase): + """ + Core notification methods for InvenTree + """ + + PLUGIN_NAME = "CoreNotificationsPlugin" + AUTHOR = _('InvenTree contributors') + DESCRIPTION = _('Integrated outgoing notificaton methods') + + class EmailNotification(BulkNotificationMethod): + METHOD_NAME = 'mail' + CONTEXT_EXTRA = [ + ('template', ), + ('template', 'html', ), + ('template', 'subject', ), + ] + + def get_targets(self): + """ + Return a list of target email addresses, + only for users which allow email notifications + """ + + allowed_users = [] + + for user in self.targets: + allows_emails = InvenTreeUserSetting.get_setting('NOTIFICATION_SEND_EMAILS', user=user) + + if allows_emails: + allowed_users.append(user) + + return EmailAddress.objects.filter( + user__in=allowed_users, + ) + + def send_bulk(self): + html_message = render_to_string(self.context['template']['html'], self.context) + targets = self.targets.values_list('email', flat=True) + + InvenTree.tasks.send_email(self.context['template']['subject'], '', targets, html_message=html_message) + + return True diff --git a/InvenTree/plugin/mixins/__init__.py b/InvenTree/plugin/mixins/__init__.py index 86e5e92f37..900289ae37 100644 --- a/InvenTree/plugin/mixins/__init__.py +++ b/InvenTree/plugin/mixins/__init__.py @@ -3,6 +3,7 @@ Utility class to enable simpler imports """ from ..builtin.integration.mixins import APICallMixin, AppMixin, LabelPrintingMixin, SettingsMixin, EventMixin, ScheduleMixin, UrlsMixin, NavigationMixin +from common.notifications import SingleNotificationMethod, BulkNotificationMethod from ..builtin.action.mixins import ActionMixin from ..builtin.barcode.mixins import BarcodeMixin @@ -18,4 +19,6 @@ __all__ = [ 'UrlsMixin', 'ActionMixin', 'BarcodeMixin', + 'SingleNotificationMethod', + 'BulkNotificationMethod', ]