mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Add Notifications shortcut (#5104)
* add notify_users shortcut * make html template optional
This commit is contained in:
		@@ -262,7 +262,22 @@ def notify_responsible(instance, sender, content: NotificationBody = InvenTreeNo
 | 
			
		||||
        content (NotificationBody, optional): _description_. Defaults to InvenTreeNotificationBodies.NewOrder.
 | 
			
		||||
        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
 | 
			
		||||
    content_context = {
 | 
			
		||||
        'instance': str(instance),
 | 
			
		||||
@@ -278,16 +293,18 @@ def notify_responsible(instance, sender, content: NotificationBody = InvenTreeNo
 | 
			
		||||
        '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),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if content.template:
 | 
			
		||||
        context['template']['html'] = content.template.format(**content_context)
 | 
			
		||||
 | 
			
		||||
    # Create notification
 | 
			
		||||
    trigger_notification(
 | 
			
		||||
        instance,
 | 
			
		||||
        content.slug.format(**content_context),
 | 
			
		||||
            targets=[instance.responsible],
 | 
			
		||||
        targets=users,
 | 
			
		||||
        target_exclude=[exclude],
 | 
			
		||||
        context=context,
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
@@ -279,7 +279,7 @@ class NotificationBody:
 | 
			
		||||
    name: str
 | 
			
		||||
    slug: str
 | 
			
		||||
    message: str
 | 
			
		||||
    template: str
 | 
			
		||||
    template: str = None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class InvenTreeNotificationBodies:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user