This commit is contained in:
Matthias 2021-12-04 03:43:14 +01:00
parent 057c880377
commit fc1fa775c3
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076

View File

@ -21,7 +21,7 @@ class NotificationMethod:
CONTEXT_BUILTIN = ['name', 'message', ] CONTEXT_BUILTIN = ['name', 'message', ]
CONTEXT_EXTRA = [] CONTEXT_EXTRA = []
def __init__(self, obj, entry_name, receivers, context) -> None: def __init__(self, obj, entry_name, targets, context) -> None:
# Check if a sending fnc is defined # Check if a sending fnc is defined
if (not hasattr(self, 'send')) and (not hasattr(self, 'send_bulk')): if (not hasattr(self, 'send')) and (not hasattr(self, 'send_bulk')):
raise NotImplementedError('A NotificationMethod must either define a `send` or a `send_bulk` method') raise NotImplementedError('A NotificationMethod must either define a `send` or a `send_bulk` method')
@ -33,7 +33,7 @@ class NotificationMethod:
# Define arguments # Define arguments
self.obj = obj self.obj = obj
self.entry_name = entry_name self.entry_name = entry_name
self.receivers = receivers self.targets = targets
self.context = self.check_context(context) self.context = self.check_context(context)
# Gather recipients # Gather recipients
@ -109,7 +109,7 @@ class EmailNotification(BulkNotificationMethod):
def get_recipients(self): def get_recipients(self):
return EmailAddress.objects.filter( return EmailAddress.objects.filter(
user__in=self.receivers, user__in=self.targets,
) )
def send_bulk(self): def send_bulk(self):
@ -124,7 +124,7 @@ class UIMessageNotification(SingleNotificationMethod):
METHOD_NAME = 'ui_message' METHOD_NAME = 'ui_message'
def get_recipients(self): def get_recipients(self):
return self.receivers return self.targets
def send(self, receiver): def send(self, receiver):
NotificationMessage.objects.create( NotificationMessage.objects.create(
@ -139,7 +139,7 @@ class UIMessageNotification(SingleNotificationMethod):
# endregion # endregion
def trigger_notifaction(obj, entry_name=None, obj_ref='pk', receivers=None, receiver_fnc=None, receiver_args=[], receiver_kwargs={}, notification_context={}): def trigger_notifaction(obj, entry_name=None, obj_ref='pk', targets=None, receiver_fnc=None, receiver_args=[], receiver_kwargs={}, notification_context={}):
""" """
Send out an notification Send out an notification
""" """
@ -166,11 +166,11 @@ def trigger_notifaction(obj, entry_name=None, obj_ref='pk', receivers=None, rece
return return
logger.info(f"Gathering users for notification '{entry_name}'") logger.info(f"Gathering users for notification '{entry_name}'")
# Collect possible receivers # Collect possible targets
if not receivers: if not targets:
receivers = receiver_fnc(*receiver_args, **receiver_kwargs) targets = receiver_fnc(*receiver_args, **receiver_kwargs)
if receivers: if targets:
logger.info(f"Sending notification '{entry_name}' for '{str(obj)}'") logger.info(f"Sending notification '{entry_name}' for '{str(obj)}'")
# Collect possible methods # Collect possible methods
@ -183,7 +183,7 @@ def trigger_notifaction(obj, entry_name=None, obj_ref='pk', receivers=None, rece
for method in [a for a in delivery_methods if a not in [SingleNotificationMethod, BulkNotificationMethod]]: for method in [a for a in delivery_methods if a not in [SingleNotificationMethod, BulkNotificationMethod]]:
logger.info(f"Triggering method '{method.METHOD_NAME}'") logger.info(f"Triggering method '{method.METHOD_NAME}'")
try: try:
deliver_notification(method, obj, entry_name, receivers, notification_context) deliver_notification(method, obj, entry_name, targets, notification_context)
except NotImplementedError as error: except NotImplementedError as error:
print('NotImplementedError') print('NotImplementedError')
raise error raise error
@ -197,9 +197,9 @@ def trigger_notifaction(obj, entry_name=None, obj_ref='pk', receivers=None, rece
logger.info(f"No possible users for notification '{entry_name}'") logger.info(f"No possible users for notification '{entry_name}'")
def deliver_notification(cls: NotificationMethod, obj, entry_name: str, receivers, notification_context: dict): def deliver_notification(cls: NotificationMethod, obj, entry_name: str, targets, notification_context: dict):
# Init delivery method # Init delivery method
method = cls(obj, entry_name, receivers, notification_context) method = cls(obj, entry_name, targets, notification_context)
if method.recipients and len(method.recipients) > 0: if method.recipients and len(method.recipients) > 0:
# Log start # Log start