2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 19:46:46 +00:00

Add Notifications shortcut (#5104)

* add notify_users shortcut

* make html template optional
This commit is contained in:
Matthias Mair 2023-06-26 01:43:07 +02:00 committed by GitHub
parent 0458b5c53a
commit a83a71b3a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 28 deletions

View File

@ -262,7 +262,22 @@ def notify_responsible(instance, sender, content: NotificationBody = InvenTreeNo
content (NotificationBody, optional): _description_. Defaults to InvenTreeNotificationBodies.NewOrder. content (NotificationBody, optional): _description_. Defaults to InvenTreeNotificationBodies.NewOrder.
exclude (User, optional): User instance that should be excluded. Defaults to None. exclude (User, optional): User instance that should be excluded. Defaults to None.
""" """
if instance.responsible is not None: notify_users([instance.responsible], instance, sender, content=content, exclude=exclude)
def notify_users(users, instance, sender, content: NotificationBody = InvenTreeNotificationBodies.NewOrder, exclude=None):
"""Notify all passed users or groups.
Parses the supplied content with the provided instance and sender and sends a notification to all users,
excluding the optional excluded list.
Args:
users: List of users or groups to notify
instance: The newly created instance
sender: Sender model reference
content (NotificationBody, optional): _description_. Defaults to InvenTreeNotificationBodies.NewOrder.
exclude (User, optional): User instance that should be excluded. Defaults to None.
"""
# Setup context for notification parsing # Setup context for notification parsing
content_context = { content_context = {
'instance': str(instance), 'instance': str(instance),
@ -278,16 +293,18 @@ def notify_responsible(instance, sender, content: NotificationBody = InvenTreeNo
'message': content.message.format(**content_context), 'message': content.message.format(**content_context),
'link': InvenTree.helpers_model.construct_absolute_url(instance.get_absolute_url()), 'link': InvenTree.helpers_model.construct_absolute_url(instance.get_absolute_url()),
'template': { 'template': {
'html': content.template.format(**content_context),
'subject': content.name.format(**content_context), 'subject': content.name.format(**content_context),
} }
} }
if content.template:
context['template']['html'] = content.template.format(**content_context)
# Create notification # Create notification
trigger_notification( trigger_notification(
instance, instance,
content.slug.format(**content_context), content.slug.format(**content_context),
targets=[instance.responsible], targets=users,
target_exclude=[exclude], target_exclude=[exclude],
context=context, context=context,
) )

View File

@ -279,7 +279,7 @@ class NotificationBody:
name: str name: str
slug: str slug: str
message: str message: str
template: str template: str = None
class InvenTreeNotificationBodies: class InvenTreeNotificationBodies: