From a447e22108d8934a6b9f0834ff54cbea8a6836c0 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 4 Nov 2021 01:18:00 +1100 Subject: [PATCH] Prevent low-stock notifications from overwhelming users - Limit to once per day, per part --- InvenTree/part/tasks.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/InvenTree/part/tasks.py b/InvenTree/part/tasks.py index 779027a96d..f4f1459214 100644 --- a/InvenTree/part/tasks.py +++ b/InvenTree/part/tasks.py @@ -2,13 +2,14 @@ from __future__ import unicode_literals import logging +from datetime import timedelta from django.utils.translation import ugettext_lazy as _ from django.template.loader import render_to_string from allauth.account.models import EmailAddress -from common.models import InvenTree +from common.models import NotificationEntry import InvenTree.helpers import InvenTree.tasks @@ -23,6 +24,13 @@ def notify_low_stock(part: part.models.Part): Notify users who have starred a part when its stock quantity falls below the minimum threshold """ + # Check if we have notified recently... + delta = timedelta(days=1) + + if NotificationEntry.check_recent('part.notify_low_stock', part.pk, delta): + logger.info(f"Low stock notification has recently been sent for '{part.full_name}' - SKIPPING") + return + logger.info(f"Sending low stock notification email for {part.full_name}") # Get a list of users who are subcribed to this part @@ -48,6 +56,8 @@ def notify_low_stock(part: part.models.Part): InvenTree.tasks.send_email(subject, '', recipients, html_message=html_message) + NotificationEntry.notify('part.notify_low_stock', part.pk) + def notify_low_stock_if_required(part: part.models.Part): """