mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 11:36:44 +00:00
Add Notifications shortcut (#5104)
* add notify_users shortcut * make html template optional
This commit is contained in:
parent
0458b5c53a
commit
a83a71b3a3
@ -262,32 +262,49 @@ 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)
|
||||||
# Setup context for notification parsing
|
|
||||||
content_context = {
|
|
||||||
'instance': str(instance),
|
|
||||||
'verbose_name': sender._meta.verbose_name,
|
|
||||||
'app_label': sender._meta.app_label,
|
|
||||||
'model_name': sender._meta.model_name,
|
|
||||||
}
|
|
||||||
|
|
||||||
# Setup notification context
|
|
||||||
context = {
|
|
||||||
'instance': instance,
|
|
||||||
'name': content.name.format(**content_context),
|
|
||||||
'message': content.message.format(**content_context),
|
|
||||||
'link': InvenTree.helpers_model.construct_absolute_url(instance.get_absolute_url()),
|
|
||||||
'template': {
|
|
||||||
'html': content.template.format(**content_context),
|
|
||||||
'subject': content.name.format(**content_context),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Create notification
|
def notify_users(users, instance, sender, content: NotificationBody = InvenTreeNotificationBodies.NewOrder, exclude=None):
|
||||||
trigger_notification(
|
"""Notify all passed users or groups.
|
||||||
instance,
|
|
||||||
content.slug.format(**content_context),
|
Parses the supplied content with the provided instance and sender and sends a notification to all users,
|
||||||
targets=[instance.responsible],
|
excluding the optional excluded list.
|
||||||
target_exclude=[exclude],
|
|
||||||
context=context,
|
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
|
||||||
|
content_context = {
|
||||||
|
'instance': str(instance),
|
||||||
|
'verbose_name': sender._meta.verbose_name,
|
||||||
|
'app_label': sender._meta.app_label,
|
||||||
|
'model_name': sender._meta.model_name,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Setup notification context
|
||||||
|
context = {
|
||||||
|
'instance': instance,
|
||||||
|
'name': content.name.format(**content_context),
|
||||||
|
'message': content.message.format(**content_context),
|
||||||
|
'link': InvenTree.helpers_model.construct_absolute_url(instance.get_absolute_url()),
|
||||||
|
'template': {
|
||||||
|
'subject': content.name.format(**content_context),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if content.template:
|
||||||
|
context['template']['html'] = content.template.format(**content_context)
|
||||||
|
|
||||||
|
# Create notification
|
||||||
|
trigger_notification(
|
||||||
|
instance,
|
||||||
|
content.slug.format(**content_context),
|
||||||
|
targets=users,
|
||||||
|
target_exclude=[exclude],
|
||||||
|
context=context,
|
||||||
|
)
|
||||||
|
@ -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:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user