mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-19 21:45:39 +00:00
- Fixes for construct_absolute_url function
- Refactor notification email generation - Update template file - Add separate templates folder for email
This commit is contained in:
@ -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
|
||||
)
|
||||
|
@ -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):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user