2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 13:05:42 +00:00

Move email notification method into plugin

Fixes #2385
This commit is contained in:
Matthias
2022-04-02 02:46:41 +02:00
parent 73dd0ff8a8
commit 279be87448
3 changed files with 58 additions and 42 deletions

View File

@ -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'