From d8796f95356730784b31ddd078c618780e3c3918 Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Fri, 29 Oct 2021 16:03:41 +0530 Subject: [PATCH 01/19] Notify users who have starred a part when that part's stock quantity falls below the minimum quanitity/threshold through email. --- InvenTree/InvenTree/tasks.py | 36 +++++++++++++++++-- InvenTree/stock/models.py | 14 +++++++- .../stock/low_stock_notification.html | 27 ++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 InvenTree/stock/templates/stock/low_stock_notification.html diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index aa17ef8603..9987a2593d 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -11,6 +11,7 @@ from django.utils import timezone from django.core.exceptions import AppRegistryNotReady from django.db.utils import OperationalError, ProgrammingError +from django.template.loader import render_to_string logger = logging.getLogger("inventree") @@ -52,7 +53,7 @@ def schedule_task(taskname, **kwargs): pass -def offload_task(taskname, force_sync=False, *args, **kwargs): +def offload_task(taskname, *args, force_sync=False, **kwargs): """ Create an AsyncTask if workers are running. This is different to a 'scheduled' task, @@ -290,7 +291,7 @@ def update_exchange_rates(): Rate.objects.filter(backend="InvenTreeExchange").exclude(currency__in=currency_codes()).delete() -def send_email(subject, body, recipients, from_email=None): +def send_email(subject, body, recipients, from_email=None, html_message=None): """ Send an email with the specified subject and body, to the specified recipients list. @@ -306,4 +307,35 @@ def send_email(subject, body, recipients, from_email=None): from_email, recipients, fail_silently=False, + html_message=html_message ) + + +def notify_low_stock(stock_item): + """ + Notify users who have starred a part when its stock quantity falls below the minimum threshold + """ + + from allauth.account.models import EmailAddress + starred_users = EmailAddress.objects.filter(user__starred_parts__part=stock_item.part) + + if len(starred_users) > 0: + logger.info(f"Notify users regarding low stock of {stock_item.part.name}") + body = f'Hi, {stock_item.part.name} is low on stock. Kindly do the needful.' + context = { + 'part_name': stock_item.part.name, + # Part url can be used to open the page of part in application from the email. + # It can be facilitated when the application base url is accessible programmatically. + # 'part_url': f'{application_base_url}/part/{stock_item.part.id}', + + 'message': body, + + # quantity is in decimal field datatype. Since the same datatype is used in models, + # it is not converted to number/integer, + 'part_quantity': stock_item.quantity, + 'minimum_quantity': stock_item.part.minimum_stock + } + subject = f'Attention! {stock_item.part.name} is low on stock' + html_message = render_to_string('stock/low_stock_notification.html', context) + recipients = starred_users.values_list('email', flat=True) + send_email(subject, body, recipients, html_message=html_message) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 1372e63406..ff8b91b105 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -17,7 +17,7 @@ from django.db.models import Sum, Q from django.db.models.functions import Coalesce from django.core.validators import MinValueValidator from django.contrib.auth.models import User -from django.db.models.signals import pre_delete +from django.db.models.signals import pre_delete, post_save from django.dispatch import receiver from markdownx.models import MarkdownxField @@ -36,6 +36,7 @@ import label.models from InvenTree.status_codes import StockStatus, StockHistoryCode from InvenTree.models import InvenTreeTree, InvenTreeAttachment from InvenTree.fields import InvenTreeModelMoneyField, InvenTreeURLField +from InvenTree import tasks as inventree_tasks from users.models import Owner @@ -1651,6 +1652,17 @@ def before_delete_stock_item(sender, instance, using, **kwargs): child.save() +@receiver(post_save, sender=StockItem) +def after_save_stock_item(sender, instance: StockItem, **kwargs): + """ + Check if the stock quantity has fallen below the minimum threshold of part. If yes, notify the users who have + starred the part + """ + + if instance.quantity <= instance.part.minimum_stock: + inventree_tasks.notify_low_stock(instance) + + class StockItemAttachment(InvenTreeAttachment): """ Model for storing file attachments against a StockItem object. diff --git a/InvenTree/stock/templates/stock/low_stock_notification.html b/InvenTree/stock/templates/stock/low_stock_notification.html new file mode 100644 index 0000000000..fa3799f6dd --- /dev/null +++ b/InvenTree/stock/templates/stock/low_stock_notification.html @@ -0,0 +1,27 @@ +

{{ message }}

+ + + + + + + + + + + + + + + + + + + + + + + +
Part low on stock
Part NameAvailable QuantityMinimum Quantity
{{ part_name }}{{ part_quantity }}{{ minimum_quantity }}
You are receiving this mail because you have starred the part {{ part_name }} in + Inventree application
+ From 83309fd054f0aa9cfab16e57a23a7eeecb4468e8 Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Sat, 30 Oct 2021 08:16:42 +0530 Subject: [PATCH 02/19] Fixed the order of fixtures installation for testing --- InvenTree/InvenTree/test_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/test_api.py b/InvenTree/InvenTree/test_api.py index dfe94c034e..6ace21b576 100644 --- a/InvenTree/InvenTree/test_api.py +++ b/InvenTree/InvenTree/test_api.py @@ -102,9 +102,9 @@ class APITests(InvenTreeAPITestCase): fixtures = [ 'location', - 'stock', - 'part', 'category', + 'part', + 'stock' ] token = None From e0cd02ee60a5ea42cd2735d4700a8ab34d9af950 Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Sat, 30 Oct 2021 08:30:39 +0530 Subject: [PATCH 03/19] added dispatch_uid to post_save signal of StockItem --- InvenTree/stock/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index ff8b91b105..b4746e0879 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -1652,7 +1652,7 @@ def before_delete_stock_item(sender, instance, using, **kwargs): child.save() -@receiver(post_save, sender=StockItem) +@receiver(post_save, sender=StockItem, dispatch_uid='stock_item_post_save_log') def after_save_stock_item(sender, instance: StockItem, **kwargs): """ Check if the stock quantity has fallen below the minimum threshold of part. If yes, notify the users who have From 6ec2801fcea15d0784e0ad57f6000e8b568aa05b Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Sat, 30 Oct 2021 20:32:10 +0530 Subject: [PATCH 04/19] Facilitated translation for low stock notification subject moved the message/content of low stock notification to html template Facilitated translation in low stock notification html template file --- InvenTree/InvenTree/tasks.py | 8 +++----- .../stock/templates/stock/low_stock_notification.html | 9 ++++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index 9987a2593d..da1d29d76a 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -12,6 +12,7 @@ from django.utils import timezone from django.core.exceptions import AppRegistryNotReady from django.db.utils import OperationalError, ProgrammingError from django.template.loader import render_to_string +from django.utils.translation import gettext_lazy as _ logger = logging.getLogger("inventree") @@ -321,21 +322,18 @@ def notify_low_stock(stock_item): if len(starred_users) > 0: logger.info(f"Notify users regarding low stock of {stock_item.part.name}") - body = f'Hi, {stock_item.part.name} is low on stock. Kindly do the needful.' context = { 'part_name': stock_item.part.name, # Part url can be used to open the page of part in application from the email. # It can be facilitated when the application base url is accessible programmatically. # 'part_url': f'{application_base_url}/part/{stock_item.part.id}', - 'message': body, - # quantity is in decimal field datatype. Since the same datatype is used in models, # it is not converted to number/integer, 'part_quantity': stock_item.quantity, 'minimum_quantity': stock_item.part.minimum_stock } - subject = f'Attention! {stock_item.part.name} is low on stock' + subject = _(f'Attention! {stock_item.part.name} is low on stock') html_message = render_to_string('stock/low_stock_notification.html', context) recipients = starred_users.values_list('email', flat=True) - send_email(subject, body, recipients, html_message=html_message) + send_email(subject, '', recipients, html_message=html_message) diff --git a/InvenTree/stock/templates/stock/low_stock_notification.html b/InvenTree/stock/templates/stock/low_stock_notification.html index fa3799f6dd..04ada64e18 100644 --- a/InvenTree/stock/templates/stock/low_stock_notification.html +++ b/InvenTree/stock/templates/stock/low_stock_notification.html @@ -1,4 +1,6 @@ -

{{ message }}

+{% load i18n %} + +

{% trans "Hi, " %} {{ part_name }} {% trans "is low on stock. Kindly do the needful." %}

@@ -19,8 +21,9 @@ - +
You are receiving this mail because you have starred the part {{ part_name }} in - Inventree application{% trans "You are receiving this mail because you have starred the part " %} {{ part_name }} + {% trans "Inventree application" %} +
From fca15a0439969181cfbafc3c93799f7e329df4ad Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Sun, 31 Oct 2021 11:21:06 +0530 Subject: [PATCH 05/19] added arbitrary args and arbitrary keyword args while executing a function synchronously from offload_task() in inventree.tasks --- InvenTree/InvenTree/tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index da1d29d76a..e623d7a98c 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -110,7 +110,7 @@ def offload_task(taskname, *args, force_sync=False, **kwargs): return # Workers are not running: run it as synchronous task - _func() + _func(*args, **kwargs) def heartbeat(): From 40da41959bffc677d940c1cd77245a7ddee3af9b Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Sun, 31 Oct 2021 11:26:41 +0530 Subject: [PATCH 06/19] Created part.tasks file and moved notify_low_stock function to the same from InvenTree.tasks. The argument type is changed from StockItem to Part Added trans to headers of table in email template of low_stock_notification.html added is_part_low_on_stock() function to the part model to check if the part's stock has fallen below the minimum quantity used offload_task function to run the low stock notification function asynchronously --- InvenTree/InvenTree/tasks.py | 27 ------------- InvenTree/part/models.py | 3 ++ InvenTree/part/tasks.py | 39 +++++++++++++++++++ InvenTree/stock/models.py | 7 +++- .../stock/low_stock_notification.html | 8 ++-- 5 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 InvenTree/part/tasks.py diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index e623d7a98c..4fa7409326 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -310,30 +310,3 @@ def send_email(subject, body, recipients, from_email=None, html_message=None): fail_silently=False, html_message=html_message ) - - -def notify_low_stock(stock_item): - """ - Notify users who have starred a part when its stock quantity falls below the minimum threshold - """ - - from allauth.account.models import EmailAddress - starred_users = EmailAddress.objects.filter(user__starred_parts__part=stock_item.part) - - if len(starred_users) > 0: - logger.info(f"Notify users regarding low stock of {stock_item.part.name}") - context = { - 'part_name': stock_item.part.name, - # Part url can be used to open the page of part in application from the email. - # It can be facilitated when the application base url is accessible programmatically. - # 'part_url': f'{application_base_url}/part/{stock_item.part.id}', - - # quantity is in decimal field datatype. Since the same datatype is used in models, - # it is not converted to number/integer, - 'part_quantity': stock_item.quantity, - 'minimum_quantity': stock_item.part.minimum_stock - } - subject = _(f'Attention! {stock_item.part.name} is low on stock') - html_message = render_to_string('stock/low_stock_notification.html', context) - recipients = starred_users.values_list('email', flat=True) - send_email(subject, '', recipients, html_message=html_message) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 5cd9fa3180..050b46058a 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1988,6 +1988,9 @@ class Part(MPTTModel): def related_count(self): return len(self.get_related_parts()) + def is_part_low_on_stock(self): + return self.total_stock <= self.minimum_stock + def attach_file(instance, filename): """ Function for storing a file for a PartAttachment diff --git a/InvenTree/part/tasks.py b/InvenTree/part/tasks.py new file mode 100644 index 0000000000..667e70f1a9 --- /dev/null +++ b/InvenTree/part/tasks.py @@ -0,0 +1,39 @@ +# Author: Roche Christopher +# Created at 10:26 AM on 31/10/21 + +import logging + +from django.utils.translation import ugettext_lazy as _ +from django.template.loader import render_to_string + +from InvenTree import tasks as inventree_tasks +from part.models import Part + +logger = logging.getLogger("inventree") + + +def notify_low_stock(part: Part): + """ + Notify users who have starred a part when its stock quantity falls below the minimum threshold + """ + + from allauth.account.models import EmailAddress + starred_users_email = EmailAddress.objects.filter(user__starred_parts__part=part) + + if len(starred_users_email) > 0: + logger.info(f"Notify users regarding low stock of {part.name}") + context = { + 'part_name': part.name, + # Part url can be used to open the page of part in application from the email. + # It can be facilitated when the application base url is accessible programmatically. + # 'part_url': f'{application_base_url}/part/{stock_item.part.id}', + + # quantity is in decimal field datatype. Since the same datatype is used in models, + # it is not converted to number/integer, + 'part_quantity': part.total_stock, + 'minimum_quantity': part.minimum_stock + } + subject = _(f'Attention! {part.name} is low on stock') + html_message = render_to_string('stock/low_stock_notification.html', context) + recipients = starred_users_email.values_list('email', flat=True) + inventree_tasks.send_email(subject, '', recipients, html_message=html_message) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index b4746e0879..69b061d25a 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -1659,8 +1659,11 @@ def after_save_stock_item(sender, instance: StockItem, **kwargs): starred the part """ - if instance.quantity <= instance.part.minimum_stock: - inventree_tasks.notify_low_stock(instance) + if instance.part.is_part_low_on_stock(): + inventree_tasks.offload_task( + 'part.tasks.notify_low_stock', + instance.part + ) class StockItemAttachment(InvenTreeAttachment): diff --git a/InvenTree/stock/templates/stock/low_stock_notification.html b/InvenTree/stock/templates/stock/low_stock_notification.html index 04ada64e18..3126cd11c1 100644 --- a/InvenTree/stock/templates/stock/low_stock_notification.html +++ b/InvenTree/stock/templates/stock/low_stock_notification.html @@ -5,13 +5,13 @@ - + - - - + + + From 60c2aab06d395bac99e5dcd6ed6d034c55298d29 Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Sun, 31 Oct 2021 11:30:14 +0530 Subject: [PATCH 07/19] remove unused imports --- InvenTree/InvenTree/tasks.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index 4fa7409326..801c75aa26 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -11,8 +11,6 @@ from django.utils import timezone from django.core.exceptions import AppRegistryNotReady from django.db.utils import OperationalError, ProgrammingError -from django.template.loader import render_to_string -from django.utils.translation import gettext_lazy as _ logger = logging.getLogger("inventree") From 76c1e936db78424e0d6953c4062eb32863e302c6 Mon Sep 17 00:00:00 2001 From: rocheparadox Date: Mon, 1 Nov 2021 08:25:59 +0530 Subject: [PATCH 08/19] Added post_delete hook to StockItem moved the business logic of 'deciding if a low stock notification has to be sent' to part.tasks --- InvenTree/part/tasks.py | 13 +++++++++++++ InvenTree/stock/models.py | 22 +++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/InvenTree/part/tasks.py b/InvenTree/part/tasks.py index 667e70f1a9..9fc05ec3f1 100644 --- a/InvenTree/part/tasks.py +++ b/InvenTree/part/tasks.py @@ -37,3 +37,16 @@ def notify_low_stock(part: Part): html_message = render_to_string('stock/low_stock_notification.html', context) recipients = starred_users_email.values_list('email', flat=True) inventree_tasks.send_email(subject, '', recipients, html_message=html_message) + + +def notify_low_stock_if_required(part: Part): + """ + Check if the stock quantity has fallen below the minimum threshold of part. If yes, notify the users who have + starred the part + """ + + if part.is_part_low_on_stock(): + inventree_tasks.offload_task( + 'part.tasks.notify_low_stock', + part + ) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 69b061d25a..657469a744 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -17,7 +17,7 @@ from django.db.models import Sum, Q from django.db.models.functions import Coalesce from django.core.validators import MinValueValidator from django.contrib.auth.models import User -from django.db.models.signals import pre_delete, post_save +from django.db.models.signals import pre_delete, post_save, post_delete from django.dispatch import receiver from markdownx.models import MarkdownxField @@ -36,12 +36,12 @@ import label.models from InvenTree.status_codes import StockStatus, StockHistoryCode from InvenTree.models import InvenTreeTree, InvenTreeAttachment from InvenTree.fields import InvenTreeModelMoneyField, InvenTreeURLField -from InvenTree import tasks as inventree_tasks from users.models import Owner from company import models as CompanyModels from part import models as PartModels +from part import tasks as part_tasks class StockLocation(InvenTreeTree): @@ -1652,18 +1652,22 @@ def before_delete_stock_item(sender, instance, using, **kwargs): child.save() +@receiver(post_delete, sender=StockItem, dispatch_uid='stock_item_post_delete_log') +def after_delete_stock_item(sender, instance: StockItem, **kwargs): + """ + Function to be executed after a StockItem object is deleted + """ + + part_tasks.notify_low_stock_if_required(instance.part) + + @receiver(post_save, sender=StockItem, dispatch_uid='stock_item_post_save_log') def after_save_stock_item(sender, instance: StockItem, **kwargs): """ - Check if the stock quantity has fallen below the minimum threshold of part. If yes, notify the users who have - starred the part + Hook function to be executed after StockItem object is saved/updated """ - if instance.part.is_part_low_on_stock(): - inventree_tasks.offload_task( - 'part.tasks.notify_low_stock', - instance.part - ) + part_tasks.notify_low_stock_if_required(instance.part) class StockItemAttachment(InvenTreeAttachment): From dabaa9aea5d42a749b2c5e8a28f4a2af0e8c5a9f Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 1 Nov 2021 23:40:21 +1100 Subject: [PATCH 09/19] Adds a function to construct an "absolute" URL Useful for providing an external link (e.g. in an email) --- InvenTree/InvenTree/helpers.py | 27 +++++++++++++++++++++++++ InvenTree/report/templatetags/report.py | 14 ++++--------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 319b88cb09..4b0f31a702 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -69,6 +69,33 @@ def getStaticUrl(filename): return os.path.join(STATIC_URL, str(filename)) +def construct_absolute_url(url): + """ + Construct (or attempt to construct) an absolute URL from a relative URL. + + This is useful when (for example) sending an email to a user with a link + to something in the InvenTree web framework. + + This requires the BASE_URL configuration option to be set! + """ + + base = str(InvenTreeSetting.get_setting('INVENTREE_BASE_URL')) + + url = str(url) + + if not base: + return url + + # Strip trailing slash from base url + if base.endswith('/'): + base = base[:-1] + + if url.startswith('/'): + url = url[1:] + + url = f"{base}/{url}" + + def getBlankImage(): """ Return the qualified path for the 'blank image' placeholder. diff --git a/InvenTree/report/templatetags/report.py b/InvenTree/report/templatetags/report.py index 45d11c1d2d..9593db1885 100644 --- a/InvenTree/report/templatetags/report.py +++ b/InvenTree/report/templatetags/report.py @@ -14,6 +14,8 @@ from stock.models import StockItem from common.models import InvenTreeSetting +import InvenTree.helpers + register = template.Library() @@ -119,18 +121,10 @@ def internal_link(link, text): text = str(text) - base_url = InvenTreeSetting.get_setting('INVENTREE_BASE_URL') + url = InvenTree.helpers.construct_absolute_url(link) # If the base URL is not set, just return the text - if not base_url: + if not url: return text - if not base_url.endswith('/'): - base_url += '/' - - if base_url.endswith('/') and link.startswith('/'): - link = link[1:] - - url = f"{base_url}{link}" - return mark_safe(f'{text}') From 6f9ac4a8504988a7803645060e09b4efcdda383c Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 00:40:25 +1100 Subject: [PATCH 10/19] - Fixes for construct_absolute_url function - Refactor notification email generation - Update template file - Add separate templates folder for email --- InvenTree/InvenTree/helpers.py | 6 ++- InvenTree/part/tasks.py | 44 +++++++++++-------- .../part/templatetags/inventree_extras.py | 6 +++ .../stock/low_stock_notification.html | 30 ------------- .../email/low_stock_notification.html | 35 +++++++++++++++ 5 files changed, 70 insertions(+), 51 deletions(-) delete mode 100644 InvenTree/stock/templates/stock/low_stock_notification.html create mode 100644 InvenTree/templates/email/low_stock_notification.html diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 4b0f31a702..fd86306627 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -69,7 +69,7 @@ def getStaticUrl(filename): return os.path.join(STATIC_URL, str(filename)) -def construct_absolute_url(url): +def construct_absolute_url(*arg): """ Construct (or attempt to construct) an absolute URL from a relative URL. @@ -81,7 +81,7 @@ def construct_absolute_url(url): base = str(InvenTreeSetting.get_setting('INVENTREE_BASE_URL')) - url = str(url) + url = '/'.join(arg) if not base: return url @@ -95,6 +95,8 @@ def construct_absolute_url(url): url = f"{base}/{url}" + return url + def getBlankImage(): """ diff --git a/InvenTree/part/tasks.py b/InvenTree/part/tasks.py index 9fc05ec3f1..72d996e772 100644 --- a/InvenTree/part/tasks.py +++ b/InvenTree/part/tasks.py @@ -1,12 +1,18 @@ -# Author: Roche Christopher -# Created at 10:26 AM on 31/10/21 +# -*- coding: utf-8 -*- +from __future__ import unicode_literals import logging from django.utils.translation import ugettext_lazy as _ from django.template.loader import render_to_string -from InvenTree import tasks as inventree_tasks +from allauth.account.models import EmailAddress + +from common.models import InvenTree + +import InvenTree.helpers +import InvenTree.tasks + from part.models import Part logger = logging.getLogger("inventree") @@ -17,36 +23,36 @@ def notify_low_stock(part: Part): Notify users who have starred a part when its stock quantity falls below the minimum threshold """ - from allauth.account.models import EmailAddress + logger.info(f"Sending low stock notification email for {part.full_name}") + starred_users_email = EmailAddress.objects.filter(user__starred_parts__part=part) + # TODO: In the future, include the part image in the email template + if len(starred_users_email) > 0: logger.info(f"Notify users regarding low stock of {part.name}") context = { - 'part_name': part.name, - # Part url can be used to open the page of part in application from the email. - # It can be facilitated when the application base url is accessible programmatically. - # 'part_url': f'{application_base_url}/part/{stock_item.part.id}', - - # quantity is in decimal field datatype. Since the same datatype is used in models, - # it is not converted to number/integer, - 'part_quantity': part.total_stock, - 'minimum_quantity': part.minimum_stock + # Pass the "Part" object through to the template context + 'part': part, + 'link': InvenTree.helpers.construct_absolute_url(part.get_absolute_url()), } - subject = _(f'Attention! {part.name} is low on stock') - html_message = render_to_string('stock/low_stock_notification.html', context) + + subject = _(f'[InvenTree] {part.name} is low on stock') + html_message = render_to_string('email/low_stock_notification.html', context) recipients = starred_users_email.values_list('email', flat=True) - inventree_tasks.send_email(subject, '', recipients, html_message=html_message) + + InvenTree.tasks.send_email(subject, '', recipients, html_message=html_message) def notify_low_stock_if_required(part: Part): """ - Check if the stock quantity has fallen below the minimum threshold of part. If yes, notify the users who have - starred the part + Check if the stock quantity has fallen below the minimum threshold of part. + + If true, notify the users who have subscribed to the part """ if part.is_part_low_on_stock(): - inventree_tasks.offload_task( + InvenTree.tasks.offload_task( 'part.tasks.notify_low_stock', part ) diff --git a/InvenTree/part/templatetags/inventree_extras.py b/InvenTree/part/templatetags/inventree_extras.py index 2230077a81..590ea20a6f 100644 --- a/InvenTree/part/templatetags/inventree_extras.py +++ b/InvenTree/part/templatetags/inventree_extras.py @@ -122,6 +122,12 @@ def inventree_title(*args, **kwargs): return version.inventreeInstanceTitle() +@register.simple_tag() +def inventree_base_url(*args, **kwargs): + """ Return the INVENTREE_BASE_URL setting """ + return InvenTreeSetting.get_setting('INVENTREE_BASE_URL') + + @register.simple_tag() def python_version(*args, **kwargs): """ diff --git a/InvenTree/stock/templates/stock/low_stock_notification.html b/InvenTree/stock/templates/stock/low_stock_notification.html deleted file mode 100644 index 3126cd11c1..0000000000 --- a/InvenTree/stock/templates/stock/low_stock_notification.html +++ /dev/null @@ -1,30 +0,0 @@ -{% load i18n %} - -

{% trans "Hi, " %} {{ part_name }} {% trans "is low on stock. Kindly do the needful." %}

- -
Part low on stock{% trans "Part low on stock" %}
Part NameAvailable QuantityMinimum Quantity{% trans "Part Name" %}{% trans "Available Quantity" %}{% trans "Minimum Quantity" %}
- - - - - - - - - - - - - - - - - - - - - -
{% trans "Part low on stock" %}
{% trans "Part Name" %}{% trans "Available Quantity" %}{% trans "Minimum Quantity" %}
{{ part_name }}{{ part_quantity }}{{ minimum_quantity }}
{% trans "You are receiving this mail because you have starred the part " %} {{ part_name }} - {% trans "Inventree application" %} -
- diff --git a/InvenTree/templates/email/low_stock_notification.html b/InvenTree/templates/email/low_stock_notification.html new file mode 100644 index 0000000000..4fa4e55295 --- /dev/null +++ b/InvenTree/templates/email/low_stock_notification.html @@ -0,0 +1,35 @@ +{% load i18n %} +{% load inventree_extras %} + + + + + + + + + + + + + + + + + + + + + + + +
+

{% blocktrans with part=part.name %} The available stock for {{ part }} has fallen below the configured minimum level{% endblocktrans %}

+ {% if link %} +

{% trans "Click on the following link to view this part" %}: {{ link }}

+ {% endif %} +
{% trans "Part Name" %}{% trans "Available Quantity" %}{% trans "Minimum Quantity" %}
{{ part.full_name }}{{ part.total_stock }}{{ part.minimum_stock }}
+

{% blocktrans with part=part.name %}You are receiving this email because you are subscribed to notifications for this part {% endblocktrans %}.

+

{% trans "InvenTree version" %}: {% inventree_version %}

+
+ From 2abcb114a8941e4228819a6f57bf4dba88ee0fbd Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 11:28:46 +1100 Subject: [PATCH 11/19] Visual improvements for "currency" page --- .../InvenTree/settings/currencies.html | 18 ++++++++++-------- InvenTree/templates/panel.html | 8 +++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/InvenTree/templates/InvenTree/settings/currencies.html b/InvenTree/templates/InvenTree/settings/currencies.html index ba6e782508..706f836317 100644 --- a/InvenTree/templates/InvenTree/settings/currencies.html +++ b/InvenTree/templates/InvenTree/settings/currencies.html @@ -13,29 +13,31 @@ {% include "InvenTree/settings/setting.html" with key="INVENTREE_DEFAULT_CURRENCY" icon="fa-globe" %} - -
- - + - + + {% for rate in rates %} - + + + + {% endfor %} + - diff --git a/InvenTree/templates/panel.html b/InvenTree/templates/panel.html index 1491991e8c..53c5ca997a 100644 --- a/InvenTree/templates/panel.html +++ b/InvenTree/templates/panel.html @@ -1,7 +1,13 @@
{% block panel_heading %}
-

{% block heading %}HEADING{% endblock %}

+
+

{% block heading %}HEADING{% endblock %}

+
+
+ {% block actions %} + {% endblock %} +
{% endblock %} {% block panel_content %} From 66b078e4b9b1e4a0b7675c756e5e91a9634eaadb Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 11:31:24 +1100 Subject: [PATCH 12/19] Refactor part settings page --- .../templates/InvenTree/settings/part.html | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/InvenTree/templates/InvenTree/settings/part.html b/InvenTree/templates/InvenTree/settings/part.html index 351810b7dc..1b2a3e5498 100644 --- a/InvenTree/templates/InvenTree/settings/part.html +++ b/InvenTree/templates/InvenTree/settings/part.html @@ -9,8 +9,6 @@ {% block content %} -

{% trans "Part Options" %}

-
{% trans "Base Currency" %} {{ base_currency }}
{% trans "Exchange Rates" %}{% trans "Exchange Rates" %}
{{ rate.currency }} {{ rate.value }}{{ rate.currency }}
{% trans "Last Update" %} + {% if rates_updated %} {{ rates_updated }} {% else %} @@ -44,7 +46,7 @@
{% csrf_token %} - +
{% include "InvenTree/settings/setting.html" with key="PART_IPN_REGEX" %} @@ -40,12 +38,17 @@
-

{% trans "Part Import" %}

- - - +
+
+

{% trans "Part Import" %}

+ {% include "spacer.html" %} +
+ +
+
+
@@ -53,14 +56,16 @@
- - -

{% trans "Part Parameter Templates" %}

- -
- +
+ +

{% trans "Part Parameter Templates" %}

+ {% include "spacer.html" %} +
+ +
+
From 489d085de83aa1d03f19f7491efa450335c90909 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 11:32:57 +1100 Subject: [PATCH 13/19] Refactor "category" settings page --- InvenTree/templates/InvenTree/settings/category.html | 12 ++++++------ InvenTree/templates/panel.html | 9 +++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/InvenTree/templates/InvenTree/settings/category.html b/InvenTree/templates/InvenTree/settings/category.html index 9eb595ddde..f90d1e8d11 100644 --- a/InvenTree/templates/InvenTree/settings/category.html +++ b/InvenTree/templates/InvenTree/settings/category.html @@ -7,6 +7,12 @@ {% trans "Category Settings" %} {% endblock %} +{% block actions %} + +{% endblock %} + {% block content %}
@@ -21,12 +27,6 @@
-
- -
-
diff --git a/InvenTree/templates/panel.html b/InvenTree/templates/panel.html index 53c5ca997a..86867f07b4 100644 --- a/InvenTree/templates/panel.html +++ b/InvenTree/templates/panel.html @@ -3,10 +3,11 @@

{% block heading %}HEADING{% endblock %}

-
-
- {% block actions %} - {% endblock %} + {% include "spacer.html" %} +
+ {% block actions %} + {% endblock %} +
{% endblock %} From d1f2d960be9f9b1cf4733e16d1aaf02b7b119aa0 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 12:15:46 +1100 Subject: [PATCH 14/19] Refactor "user account" page --- .../templates/InvenTree/settings/user.html | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index d6cbf998a7..29813465cd 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -11,18 +11,18 @@ {% trans "Account Settings" %} {% endblock %} +{% block actions %} +
+ {% trans "Edit" %} +
+
+ {% trans "Set Password" %} +
+{% endblock %} + {% block content %} {% mail_configured as mail_conf %} -
-
- {% trans "Edit" %} -
-
- {% trans "Set Password" %} -
-
- @@ -39,7 +39,10 @@
{% trans "Username" %}
-

{% trans "Email" %}

+
+

{% trans "Email" %}

+ {% include "spacer.html" %} +
@@ -89,8 +92,18 @@
{% csrf_token %} - {{ add_email_form|crispy }} - + + +
+
@
+ +
+ +
+
+
{% endif %}
From ec147ea25fbe85a5e6fbd9a50f22ad7b1d5411ca Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 12:49:44 +1100 Subject: [PATCH 15/19] Further work on email settings page --- .../templates/InvenTree/settings/user.html | 124 +++++++++--------- 1 file changed, 64 insertions(+), 60 deletions(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index 29813465cd..1b5e507047 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -45,50 +45,51 @@
-
- {% if user.emailaddress_set.all %} -

{% trans 'The following email addresses are associated with your account:' %}

+
+
+ {% if user.emailaddress_set.all %} +

{% trans 'The following email addresses are associated with your account:' %}

- - + {% else %} +

{% trans 'Warning:'%} + {% trans "You currently do not have any email address set up. You should really add an email address so you can receive notifications, reset your password, etc." %} +

- {% else %} -

{% trans 'Warning:'%} - {% trans "You currently do not have any email address set up. You should really add an email address so you can receive notifications, reset your password, etc." %} -

- - {% endif %} - - {% if can_add_email %} -
-

{% trans "Add Email Address" %}

+ {% endif %} +
+
+ {% if can_add_email %} +
{% trans "Add Email Address" %}
{% csrf_token %} @@ -106,7 +107,7 @@
{% endif %} -
+
@@ -168,26 +169,26 @@
-
- {% csrf_token %} - -
-
-
- +
+ + {% csrf_token %} + + +
+ +
+
-
-
- -
- - + +
@@ -199,7 +200,10 @@
{% csrf_token %} -
+ +
-
-
- +
+ +
- +

{% trans "Help the translation efforts!" %}

From fc9ca5e48138e8f1f513d7c4af4d9fdbeea2e748 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 12:54:41 +1100 Subject: [PATCH 16/19] Pretty badges for email accounts --- .../templates/InvenTree/settings/user.html | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index 1b5e507047..aaf16cf853 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -55,20 +55,26 @@
{% for emailaddress in user.emailaddress_set.all %} +
{% if emailaddress.verified %} - {% trans "Verified" %} + {% trans "Verified" %} {% else %} - {% trans "Unverified" %} + {% trans "Unverified" %} {% endif %} - {% if emailaddress.primary %}{% trans "Primary" %}{% endif %} - + {% if emailaddress.primary %}{% trans "Primary" %}{% endif %}
+
{% endfor %}
From a3889c709e5cc46fde650af5437166ceb69744d7 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 12:57:40 +1100 Subject: [PATCH 17/19] More tweaks --- InvenTree/common/models.py | 6 +++--- InvenTree/templates/InvenTree/settings/login.html | 2 +- InvenTree/templates/InvenTree/settings/setting.html | 6 ++---- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 3dae13c3e0..1809f437f7 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -807,19 +807,19 @@ class InvenTreeSetting(BaseInvenTreeSetting): # login / SSO 'LOGIN_ENABLE_PWD_FORGOT': { 'name': _('Enable password forgot'), - 'description': _('Enable password forgot function on the login-pages'), + 'description': _('Enable password forgot function on the login pages'), 'default': True, 'validator': bool, }, 'LOGIN_ENABLE_REG': { 'name': _('Enable registration'), - 'description': _('Enable self-registration for users on the login-pages'), + 'description': _('Enable self-registration for users on the login pages'), 'default': False, 'validator': bool, }, 'LOGIN_ENABLE_SSO': { 'name': _('Enable SSO'), - 'description': _('Enable SSO on the login-pages'), + 'description': _('Enable SSO on the login pages'), 'default': False, 'validator': bool, }, diff --git a/InvenTree/templates/InvenTree/settings/login.html b/InvenTree/templates/InvenTree/settings/login.html index d3cba1180f..96d986d6c7 100644 --- a/InvenTree/templates/InvenTree/settings/login.html +++ b/InvenTree/templates/InvenTree/settings/login.html @@ -17,7 +17,7 @@ {% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_PWD_FORGOT" icon="fa-info-circle" %} {% include "InvenTree/settings/setting.html" with key="LOGIN_MAIL_REQUIRED" icon="fa-info-circle" %} - {% trans 'Signup' %} +
{% trans 'Signup' %}
{% include "InvenTree/settings/setting.html" with key="LOGIN_ENABLE_REG" icon="fa-info-circle" %} diff --git a/InvenTree/templates/InvenTree/settings/setting.html b/InvenTree/templates/InvenTree/settings/setting.html index 4a506b46e6..7419b7ff34 100644 --- a/InvenTree/templates/InvenTree/settings/setting.html +++ b/InvenTree/templates/InvenTree/settings/setting.html @@ -21,15 +21,13 @@
{% else %}
- {% if setting.value %} - {{ setting.value }} + {{ setting.value }} {% else %} - {% trans "No value set" %} + {% trans "No value set" %} {% endif %} - {{ setting.units }}
{% endif %} From e3dfb6cbc8f3d2c8cb9c7a25b21771dc4d2d80f5 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 13:08:10 +1100 Subject: [PATCH 18/19] Improve messaging --- InvenTree/templates/InvenTree/settings/user.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/InvenTree/templates/InvenTree/settings/user.html b/InvenTree/templates/InvenTree/settings/user.html index aaf16cf853..d1baf1ba6e 100644 --- a/InvenTree/templates/InvenTree/settings/user.html +++ b/InvenTree/templates/InvenTree/settings/user.html @@ -155,7 +155,9 @@ {% else %} -

{% trans 'You currently have no social network accounts connected to this account.' %}

+
+ {% trans "There are no social network accounts connected to your InvenTree account" %} +
{% endif %}
From 1dea7861d053ad699c51531dac446b81946c1eb0 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Nov 2021 14:43:57 +1100 Subject: [PATCH 19/19] Refactor email body out into a template - Will be useful in the future when more email functionality is implemented --- InvenTree/templates/email/email.html | 43 +++++++++++++++ .../email/low_stock_notification.html | 52 ++++++++----------- 2 files changed, 66 insertions(+), 29 deletions(-) create mode 100644 InvenTree/templates/email/email.html diff --git a/InvenTree/templates/email/email.html b/InvenTree/templates/email/email.html new file mode 100644 index 0000000000..97e9a40f37 --- /dev/null +++ b/InvenTree/templates/email/email.html @@ -0,0 +1,43 @@ +{% load i18n %} +{% load static %} +{% load inventree_extras %} + + + + {% block header %} + + + + + {% endblock %} + + {% block body %} + + {% block body_row %} + + {% endblock %} + + {% endblock %} + + {% block footer %} + + + + {% endblock %} + +
+ {% block header_row %} +

{% block title %}{% endblock %}

+ {% block subtitle %} + + {% endblock %} + {% endblock %} +
+ {% block footer_prefix %} + + {% endblock %} +

{% trans "InvenTree version" %}: {% inventree_version %} - inventree.readthedocs.io

+ {% block footer_suffix %} + + {% endblock %} +
diff --git a/InvenTree/templates/email/low_stock_notification.html b/InvenTree/templates/email/low_stock_notification.html index 4fa4e55295..ecb350925a 100644 --- a/InvenTree/templates/email/low_stock_notification.html +++ b/InvenTree/templates/email/low_stock_notification.html @@ -1,35 +1,29 @@ +{% extends "email/email.html" %} + {% load i18n %} {% load inventree_extras %} - +{% block title %} +{% blocktrans with part=part.name %} The available stock for {{ part }} has fallen below the configured minimum level{% endblocktrans %} +{% if link %} +

{% trans "Click on the following link to view this part" %}: {{ link }}

+{% endif %} +{% endblock %} - - - +{% block subtitle %} +

{% blocktrans with part=part.name %}You are receiving this email because you are subscribed to notifications for this part {% endblocktrans %}.

+{% endblock %} - - - - - - - - - - - - - - - - -
-

{% blocktrans with part=part.name %} The available stock for {{ part }} has fallen below the configured minimum level{% endblocktrans %}

- {% if link %} -

{% trans "Click on the following link to view this part" %}: {{ link }}

- {% endif %} -
{% trans "Part Name" %}{% trans "Available Quantity" %}{% trans "Minimum Quantity" %}
{{ part.full_name }}{{ part.total_stock }}{{ part.minimum_stock }}
-

{% blocktrans with part=part.name %}You are receiving this email because you are subscribed to notifications for this part {% endblocktrans %}.

-

{% trans "InvenTree version" %}: {% inventree_version %}

-
+{% block body %} + + {% trans "Part Name" %} + {% trans "Available Quantity" %} + {% trans "Minimum Quantity" %} + + + {{ part.full_name }} + {{ part.total_stock }} + {{ part.minimum_stock }} + +{% endblock %}