mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 12:35:46 +00:00
feat: Add email notification when a part is changed (#9275)
* Add function to star / unstar a part * Also use with category * Email notification when a part is changed Fixes #7834 * enable disabling of recent checks * Add error handler * remove unneeded function
This commit is contained in:
@ -366,6 +366,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs):
|
||||
target_exclude = kwargs.get('target_exclude')
|
||||
context = kwargs.get('context', {})
|
||||
delivery_methods = kwargs.get('delivery_methods')
|
||||
check_recent = kwargs.get('check_recent', True)
|
||||
|
||||
# Check if data is importing currently
|
||||
if isImportingData() or isRebuildingData():
|
||||
@ -391,7 +392,9 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs):
|
||||
# Check if we have notified recently...
|
||||
delta = timedelta(days=1)
|
||||
|
||||
if common.models.NotificationEntry.check_recent(category, obj_ref_value, delta):
|
||||
if check_recent and common.models.NotificationEntry.check_recent(
|
||||
category, obj_ref_value, delta
|
||||
):
|
||||
logger.info(
|
||||
"Notification '%s' has recently been sent for '%s' - SKIPPING",
|
||||
category,
|
||||
|
@ -0,0 +1,69 @@
|
||||
"""Core set of Notifications as a Plugin."""
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
import structlog
|
||||
|
||||
import common.models
|
||||
import common.notifications
|
||||
import InvenTree.helpers
|
||||
import InvenTree.helpers_email
|
||||
import InvenTree.helpers_model
|
||||
import InvenTree.tasks
|
||||
from part.models import Part
|
||||
from plugin import InvenTreePlugin
|
||||
from plugin.mixins import EventMixin, SettingsMixin
|
||||
|
||||
logger = structlog.get_logger('inventree')
|
||||
|
||||
|
||||
class PartNotificationsPlugin(SettingsMixin, EventMixin, InvenTreePlugin):
|
||||
"""Core notification methods for InvenTree."""
|
||||
|
||||
NAME = 'PartNotificationsPlugin'
|
||||
TITLE = _('Part Notifications')
|
||||
AUTHOR = _('InvenTree contributors')
|
||||
DESCRIPTION = _('Notify users about part changes')
|
||||
VERSION = '1.0.0'
|
||||
|
||||
SETTINGS = {
|
||||
'ENABLE_PART_NOTIFICATIONS': {
|
||||
'name': _('Send notifications'),
|
||||
'description': _('Send notifications for part changes to subscribed users'),
|
||||
'default': False,
|
||||
'validator': bool,
|
||||
}
|
||||
}
|
||||
|
||||
def wants_process_event(self, event):
|
||||
"""Return whether given event should be processed or not."""
|
||||
return event.startswith('part_part.')
|
||||
|
||||
def process_event(self, event, *args, **kwargs):
|
||||
"""Custom event processing."""
|
||||
if not self.get_setting('ENABLE_PART_NOTIFICATIONS'):
|
||||
return
|
||||
part = Part.objects.get(pk=kwargs['id'])
|
||||
part_action = event.split('.')[-1]
|
||||
|
||||
name = _('Changed part notification')
|
||||
common.notifications.trigger_notification(
|
||||
part,
|
||||
'part.notification',
|
||||
target_fnc=part.get_subscribers,
|
||||
check_recent=False,
|
||||
context={
|
||||
'part': part,
|
||||
'name': name,
|
||||
'message': _(
|
||||
f'The part `{part.name}` has been triggered with a `{part_action}` event'
|
||||
),
|
||||
'link': InvenTree.helpers_model.construct_absolute_url(
|
||||
part.get_absolute_url()
|
||||
),
|
||||
'template': {
|
||||
'html': 'email/part_event_notification.html',
|
||||
'subject': name,
|
||||
},
|
||||
},
|
||||
)
|
@ -0,0 +1,33 @@
|
||||
{% extends "email/email.html" %}
|
||||
|
||||
{% load i18n %}
|
||||
{% load inventree_extras %}
|
||||
|
||||
{% block title %}
|
||||
{{ message }}
|
||||
{% if link %}
|
||||
<p>{% trans "Click on the following link to view this part" %}: <a href="{{ link }}">{{ link }}</a></p>
|
||||
{% endif %}
|
||||
{% endblock title %}
|
||||
|
||||
{% block body %}
|
||||
<tr style="height: 3rem; border-bottom: 1px solid">
|
||||
<th>{% trans "Part" %}</th>
|
||||
<th>{% trans "Part Category" %}</th>
|
||||
<th>{% trans "Total Stock" %}</th>
|
||||
<th>{% trans "Available" %}</th>
|
||||
<th>{% trans "Minimum Quantity" %}</th>
|
||||
</tr>
|
||||
|
||||
<tr style="height: 3rem">
|
||||
<td style="text-align: center;">{{ part.full_name }}</td>
|
||||
<td style="text-align: center;">{{ part.category_path }}</td>
|
||||
<td style="text-align: center;">{% decimal part.total_stock %}</td>
|
||||
<td style="text-align: center;">{% decimal part.available_stock %}</td>
|
||||
<td style="text-align: center;">{% decimal part.minimum_stock %}</td>
|
||||
</tr>
|
||||
{% endblock body %}
|
||||
|
||||
{% block footer_prefix %}
|
||||
<p><em>{% blocktrans with part=part.name %}You are receiving this email because you are subscribed to notifications for this part or a category that it is part of {% endblocktrans %}.</em></p>
|
||||
{% endblock footer_prefix %}
|
Reference in New Issue
Block a user