diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 1a75b97af0..55585c7670 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -1,31 +1,47 @@ --- -name: Bug report -about: Create a bug report to help us improve InvenTree +name: Bug +about: Create a bug report to help us improve InvenTree! title: "[BUG] Enter bug description" labels: bug, question assignees: '' --- -**Describe the bug** -A clear and concise description of what the bug is. + + + +**Describe the bug** + + +**Steps to Reproduce** -**To Reproduce** Steps to reproduce the behavior: + **Expected behavior** + + **Deployment Method** -Docker -Bare Metal +- [ ] Docker +- [ ] Bare Metal **Version Information** -You can get this by going to the "About InvenTree" section in the upper right corner and cicking on to the "copy version information" + diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 31a887d736..5f347dd1e5 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -76,6 +76,12 @@ class InvenTreeConfig(AppConfig): minutes=30, ) + # Delete old notification records + InvenTree.tasks.schedule_task( + 'common.tasks.delete_old_notifications', + schedule_type=Schedule.DAILY, + ) + def update_exchange_rates(self): """ Update exchange rates each time the server is started, *if*: diff --git a/InvenTree/InvenTree/management/commands/rebuild_thumbnails.py b/InvenTree/InvenTree/management/commands/rebuild_thumbnails.py index 07e700a1cf..bf36a612d1 100644 --- a/InvenTree/InvenTree/management/commands/rebuild_thumbnails.py +++ b/InvenTree/InvenTree/management/commands/rebuild_thumbnails.py @@ -17,7 +17,7 @@ from company.models import Company from part.models import Part -logger = logging.getLogger("inventree-thumbnails") +logger = logging.getLogger('inventree') class Command(BaseCommand): diff --git a/InvenTree/InvenTree/static/css/inventree.css b/InvenTree/InvenTree/static/css/inventree.css index 61037c9c54..478734383f 100644 --- a/InvenTree/InvenTree/static/css/inventree.css +++ b/InvenTree/InvenTree/static/css/inventree.css @@ -28,9 +28,8 @@ padding: 20px; padding-bottom: 35px; background-color: rgba(50, 50, 50, 0.75); - width: 100%; - max-width: 330px; + max-width: 550px; margin: auto; } @@ -180,10 +179,6 @@ float: right; } -.starred-part { - color: #ffbb00; -} - .red-cell { background-color: #ec7f7f; } @@ -565,6 +560,11 @@ transition: 0.1s; } +.search-autocomplete-item { + border-top: 1px solid #EEE; + margin-bottom: 2px; +} + .modal { overflow: hidden; z-index: 9999; @@ -745,13 +745,7 @@ input[type="submit"] { } .notification-area { - position: fixed; - top: 0px; - margin-top: 20px; - width: 100%; - padding: 20px; - z-index: 5000; - pointer-events: none; /* Prevent this div from blocking links underneath */ + opacity: 0.8; } .notes { @@ -761,7 +755,6 @@ input[type="submit"] { } .alert { - display: none; border-radius: 5px; opacity: 0.9; pointer-events: all; @@ -771,9 +764,8 @@ input[type="submit"] { display: block; } -.btn { - margin-left: 2px; - margin-right: 2px; +.navbar .btn { + margin-left: 5px; } .btn-secondary { diff --git a/InvenTree/templates/js/dynamic/inventree.js b/InvenTree/InvenTree/static/script/inventree/inventree.js similarity index 72% rename from InvenTree/templates/js/dynamic/inventree.js rename to InvenTree/InvenTree/static/script/inventree/inventree.js index 0172e47706..df48cf7d60 100644 --- a/InvenTree/templates/js/dynamic/inventree.js +++ b/InvenTree/InvenTree/static/script/inventree/inventree.js @@ -1,5 +1,3 @@ -{% load inventree_extras %} - /* globals ClipboardJS, inventreeFormDataUpload, @@ -130,61 +128,71 @@ function inventreeDocReady() { attachClipboard('.clip-btn-version', 'modal-about', 'about-copy-text'); // Add autocomplete to the search-bar - $('#search-bar').autocomplete({ - source: function(request, response) { - $.ajax({ - url: '/api/part/', - data: { - search: request.term, - limit: user_settings.SEARCH_PREVIEW_RESULTS, - offset: 0 - }, - success: function(data) { + if ($('#search-bar').exists()) { + $('#search-bar').autocomplete({ + source: function(request, response) { + $.ajax({ + url: '/api/part/', + data: { + search: request.term, + limit: user_settings.SEARCH_PREVIEW_RESULTS, + offset: 0 + }, + success: function(data) { - var transformed = $.map(data.results, function(el) { - return { - label: el.full_name, - id: el.pk, - thumbnail: el.thumbnail, - data: el, - }; - }); - response(transformed); - }, - error: function() { - response([]); - } - }); - }, - create: function() { - $(this).data('ui-autocomplete')._renderItem = function(ul, item) { + var transformed = $.map(data.results, function(el) { + return { + label: el.full_name, + id: el.pk, + thumbnail: el.thumbnail, + data: el, + }; + }); + response(transformed); + }, + error: function() { + response([]); + } + }); + }, + create: function() { + $(this).data('ui-autocomplete')._renderItem = function(ul, item) { - var html = ``; + var html = ` +
+ + ${item.label} + + + `; + + if (user_settings.SEARCH_SHOW_STOCK_LEVELS) { + html += partStockLabel( + item.data, + { + classes: 'badge-right', + } + ); + } - html += ` `; - html += item.label; + html += '
'; - html += ''; - - if (user_settings.SEARCH_SHOW_STOCK_LEVELS) { - html += partStockLabel(item.data); - } - - html += ''; - - return $('
  • ').append(html).appendTo(ul); - }; - }, - select: function( event, ui ) { - window.location = '/part/' + ui.item.id + '/'; - }, - minLength: 2, - classes: { - 'ui-autocomplete': 'dropdown-menu search-menu', - }, - }); + return $('
  • ').append(html).appendTo(ul); + }; + }, + select: function( event, ui ) { + window.location = '/part/' + ui.item.id + '/'; + }, + minLength: 2, + classes: { + 'ui-autocomplete': 'dropdown-menu search-menu', + }, + position: { + my : "right top", + at: "right bottom" + } + }); + } // Generate brand-icons $('.brand-icon').each(function(i, obj) { @@ -197,6 +205,9 @@ function inventreeDocReady() { location.href = url; }); + + // Display any cached alert messages + showCachedAlerts(); } function isFileTransfer(transfer) { diff --git a/InvenTree/InvenTree/static/script/inventree/notification.js b/InvenTree/InvenTree/static/script/inventree/notification.js index 01754bceaf..399ba1d359 100644 --- a/InvenTree/InvenTree/static/script/inventree/notification.js +++ b/InvenTree/InvenTree/static/script/inventree/notification.js @@ -1,44 +1,113 @@ -function showAlert(target, message, timeout=5000) { +/* + * Add a cached alert message to sesion storage + */ +function addCachedAlert(message, style) { - $(target).find(".alert-msg").html(message); - $(target).show(); - $(target).delay(timeout).slideUp(200, function() { + var alerts = sessionStorage.getItem('inventree-alerts'); + + if (alerts) { + alerts = JSON.parse(alerts); + } else { + alerts = []; + } + + alerts.push({ + message: message, + style: style + }); + + sessionStorage.setItem('inventree-alerts', JSON.stringify(alerts)); +} + + +/* + * Remove all cached alert messages + */ +function clearCachedAlerts() { + sessionStorage.removeItem('inventree-alerts'); +} + + +/* + * Display an alert, or cache to display on reload + */ +function showAlertOrCache(message, style, cache=false) { + + if (cache) { + addCachedAlert(message, style); + } else { + + showMessage(message, {style: style}); + } +} + + +/* + * Display cached alert messages when loading a page + */ +function showCachedAlerts() { + + var alerts = JSON.parse(sessionStorage.getItem('inventree-alerts')) || []; + + alerts.forEach(function(alert) { + showMessage(alert.message, {style: alert.style}); + }); + + clearCachedAlerts(); +} + + +/* + * Display an alert message at the top of the screen. + * The message will contain a "close" button, + * and also dismiss automatically after a certain amount of time. + * + * arguments: + * - message: Text / HTML content to display + * + * options: + * - style: alert style e.g. 'success' / 'warning' + * - timeout: Time (in milliseconds) after which the message will be dismissed + */ +function showMessage(message, options={}) { + + var style = options.style || 'info'; + + var timeout = options.timeout || 5000; + + var details = ''; + + if (options.details) { + details = `

    ${options.details}

    `; + } + + // Hacky function to get the next available ID + var id = 1; + + while ($(`#alert-${id}`).exists()) { + id++; + } + + var icon = ''; + + if (options.icon) { + icon = ``; + } + + // Construct the alert + var html = ` + + `; + + $('#alerts').append(html); + + // Remove the alert automatically after a specified period of time + $(`#alert-${id}`).delay(timeout).slideUp(200, function() { $(this).alert(close); }); } - -function showAlertOrCache(alertType, message, cache, timeout=5000) { - if (cache) { - sessionStorage.setItem("inventree-" + alertType, message); - } - else { - showAlert('#' + alertType, message, timeout); - } -} - -function showCachedAlerts() { - - // Success Message - if (sessionStorage.getItem("inventree-alert-success")) { - showAlert("#alert-success", sessionStorage.getItem("inventree-alert-success")); - sessionStorage.removeItem("inventree-alert-success"); - } - - // Info Message - if (sessionStorage.getItem("inventree-alert-info")) { - showAlert("#alert-info", sessionStorage.getItem("inventree-alert-info")); - sessionStorage.removeItem("inventree-alert-info"); - } - - // Warning Message - if (sessionStorage.getItem("inventree-alert-warning")) { - showAlert("#alert-warning", sessionStorage.getItem("inventree-alert-warning")); - sessionStorage.removeItem("inventree-alert-warning"); - } - - // Danger Message - if (sessionStorage.getItem("inventree-alert-danger")) { - showAlert("#alert-danger", sessionStorage.getItem("inventree-alert-danger")); - sessionStorage.removeItem("inventree-alert-danger"); - } -} \ No newline at end of file diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 2bd04b009b..e0c88c6b7e 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -98,7 +98,6 @@ settings_urls = [ # These javascript files are served "dynamically" - i.e. rendered on demand dynamic_javascript_urls = [ - url(r'^inventree.js', DynamicJsView.as_view(template_name='js/dynamic/inventree.js'), name='inventree.js'), url(r'^calendar.js', DynamicJsView.as_view(template_name='js/dynamic/calendar.js'), name='calendar.js'), url(r'^nav.js', DynamicJsView.as_view(template_name='js/dynamic/nav.js'), name='nav.js'), url(r'^settings.js', DynamicJsView.as_view(template_name='js/dynamic/settings.js'), name='settings.js'), diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 00f38ce89d..989fb1bc9d 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -655,17 +655,6 @@ class IndexView(TemplateView): context = super(TemplateView, self).get_context_data(**kwargs) - # TODO - Re-implement this when a less expensive method is worked out - # context['starred'] = [star.part for star in self.request.user.starred_parts.all()] - - # Generate a list of orderable parts which have stock below their minimum values - # TODO - Is there a less expensive way to get these from the database - # context['to_order'] = [part for part in Part.objects.filter(purchaseable=True) if part.need_to_restock()] - - # Generate a list of assembly parts which have stock below their minimum values - # TODO - Is there a less expensive way to get these from the database - # context['to_build'] = [part for part in Part.objects.filter(assembly=True) if part.need_to_restock()] - return context diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 403b3a9430..0dd6a404e5 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -9,16 +9,16 @@ import decimal import os from datetime import datetime -from django.utils.translation import ugettext_lazy as _ - from django.contrib.auth.models import User from django.core.exceptions import ValidationError - -from django.urls import reverse +from django.core.validators import MinValueValidator from django.db import models, transaction from django.db.models import Sum, Q from django.db.models.functions import Coalesce -from django.core.validators import MinValueValidator +from django.db.models.signals import post_save +from django.dispatch.dispatcher import receiver +from django.urls import reverse +from django.utils.translation import ugettext_lazy as _ from markdownx.models import MarkdownxField @@ -27,16 +27,17 @@ from mptt.exceptions import InvalidMove from InvenTree.status_codes import BuildStatus, StockStatus, StockHistoryCode from InvenTree.helpers import increment, getSetting, normalize, MakeBarcode -from InvenTree.validators import validate_build_order_reference from InvenTree.models import InvenTreeAttachment, ReferenceIndexingMixin +from InvenTree.validators import validate_build_order_reference import common.models import InvenTree.fields import InvenTree.helpers +import InvenTree.tasks -from stock import models as StockModels from part import models as PartModels +from stock import models as StockModels from users import models as UserModels @@ -1014,6 +1015,19 @@ class Build(MPTTModel, ReferenceIndexingMixin): return self.status == BuildStatus.COMPLETE +@receiver(post_save, sender=Build, dispatch_uid='build_post_save_log') +def after_save_build(sender, instance: Build, created: bool, **kwargs): + """ + Callback function to be executed after a Build instance is saved + """ + + if created: + # A new Build has just been created + + # Run checks on required parts + InvenTree.tasks.offload_task('build.tasks.check_build_stock', instance) + + class BuildOrderAttachment(InvenTreeAttachment): """ Model for storing file attachments against a BuildOrder object diff --git a/InvenTree/build/tasks.py b/InvenTree/build/tasks.py new file mode 100644 index 0000000000..6fe4be5119 --- /dev/null +++ b/InvenTree/build/tasks.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from decimal import Decimal +import logging + +from django.utils.translation import ugettext_lazy as _ +from django.template.loader import render_to_string + +from allauth.account.models import EmailAddress + +import build.models +import InvenTree.helpers +import InvenTree.tasks +import part.models as part_models + + +logger = logging.getLogger('inventree') + + +def check_build_stock(build: build.models.Build): + """ + Check the required stock for a newly created build order, + and send an email out to any subscribed users if stock is low. + """ + + # Iterate through each of the parts required for this build + + lines = [] + + if not build: + logger.error("Invalid build passed to 'build.tasks.check_build_stock'") + return + + try: + part = build.part + except part_models.Part.DoesNotExist: + # Note: This error may be thrown during unit testing... + logger.error("Invalid build.part passed to 'build.tasks.check_build_stock'") + return + + for bom_item in part.get_bom_items(): + + sub_part = bom_item.sub_part + + # The 'in stock' quantity depends on whether the bom_item allows variants + in_stock = sub_part.get_stock_count(include_variants=bom_item.allow_variants) + + allocated = sub_part.allocation_count() + + available = max(0, in_stock - allocated) + + required = Decimal(bom_item.quantity) * Decimal(build.quantity) + + if available < required: + # There is not sufficient stock for this part + + lines.append({ + 'link': InvenTree.helpers.construct_absolute_url(sub_part.get_absolute_url()), + 'part': sub_part, + 'in_stock': in_stock, + 'allocated': allocated, + 'available': available, + 'required': required, + }) + + if len(lines) == 0: + # Nothing to do + return + + # Are there any users subscribed to these parts? + subscribers = build.part.get_subscribers() + + emails = EmailAddress.objects.filter( + user__in=subscribers, + ) + + if len(emails) > 0: + + logger.info(f"Notifying users of stock required for build {build.pk}") + + context = { + 'link': InvenTree.helpers.construct_absolute_url(build.get_absolute_url()), + 'build': build, + 'part': build.part, + 'lines': lines, + } + + # Render the HTML message + html_message = render_to_string('email/build_order_required_stock.html', context) + + subject = "[InvenTree] " + _("Stock required for build order") + + recipients = emails.values_list('email', flat=True) + + InvenTree.tasks.send_email(subject, '', recipients, html_message=html_message) diff --git a/InvenTree/build/templates/build/detail.html b/InvenTree/build/templates/build/detail.html index d53122cdd1..31e9f38080 100644 --- a/InvenTree/build/templates/build/detail.html +++ b/InvenTree/build/templates/build/detail.html @@ -247,7 +247,9 @@ diff --git a/InvenTree/common/admin.py b/InvenTree/common/admin.py index 088f53dc39..19361af325 100644 --- a/InvenTree/common/admin.py +++ b/InvenTree/common/admin.py @@ -5,7 +5,7 @@ from django.contrib import admin from import_export.admin import ImportExportModelAdmin -from .models import InvenTreeSetting, InvenTreeUserSetting, WebhookEndpoint, WebhookMessage +import common.models class SettingsAdmin(ImportExportModelAdmin): @@ -23,7 +23,15 @@ class WebhookAdmin(ImportExportModelAdmin): list_display = ('endpoint_id', 'name', 'active', 'user') -admin.site.register(InvenTreeSetting, SettingsAdmin) -admin.site.register(InvenTreeUserSetting, UserSettingsAdmin) -admin.site.register(WebhookEndpoint, WebhookAdmin) -admin.site.register(WebhookMessage, ImportExportModelAdmin) +admin.site.register(common.models.InvenTreeSetting, SettingsAdmin) +admin.site.register(common.models.InvenTreeUserSetting, UserSettingsAdmin) +admin.site.register(common.models.WebhookEndpoint, WebhookAdmin) +admin.site.register(common.models.WebhookMessage, ImportExportModelAdmin) +class NotificationEntryAdmin(admin.ModelAdmin): + + list_display = ('key', 'uid', 'updated', ) + + +admin.site.register(common.models.InvenTreeSetting, SettingsAdmin) +admin.site.register(common.models.InvenTreeUserSetting, UserSettingsAdmin) +admin.site.register(common.models.NotificationEntry, NotificationEntryAdmin) diff --git a/InvenTree/common/migrations/0012_notificationentry.py b/InvenTree/common/migrations/0012_notificationentry.py new file mode 100644 index 0000000000..77439c9f8c --- /dev/null +++ b/InvenTree/common/migrations/0012_notificationentry.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.5 on 2021-11-03 13:54 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('common', '0011_auto_20210722_2114'), + ] + + operations = [ + migrations.CreateModel( + name='NotificationEntry', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('key', models.CharField(max_length=250)), + ('uid', models.IntegerField()), + ('updated', models.DateTimeField(auto_now=True)), + ], + options={ + 'unique_together': {('key', 'uid')}, + }, + ), + ] diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 8b5f1bbd98..a2522564ea 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -15,6 +15,7 @@ import json import hashlib import base64 from secrets import compare_digest +from datetime import datetime, timedelta from django.db import models, transaction from django.contrib.auth.models import User, Group @@ -921,8 +922,14 @@ class InvenTreeUserSetting(BaseInvenTreeSetting): GLOBAL_SETTINGS = { 'HOMEPAGE_PART_STARRED': { - 'name': _('Show starred parts'), - 'description': _('Show starred parts on the homepage'), + 'name': _('Show subscribed parts'), + 'description': _('Show subscribed parts on the homepage'), + 'default': True, + 'validator': bool, + }, + 'HOMEPAGE_CATEGORY_STARRED': { + 'name': _('Show subscribed categories'), + 'description': _('Show subscribed part categories on the homepage'), 'default': True, 'validator': bool, }, @@ -1445,3 +1452,63 @@ class WebhookMessage(models.Model): verbose_name=_('Worked on'), help_text=_('Was the work on this message finished?'), ) + + +class NotificationEntry(models.Model): + """ + A NotificationEntry records the last time a particular notifaction was sent out. + + It is recorded to ensure that notifications are not sent out "too often" to users. + + Attributes: + - key: A text entry describing the notification e.g. 'part.notify_low_stock' + - uid: An (optional) numerical ID for a particular instance + - date: The last time this notification was sent + """ + + class Meta: + unique_together = [ + ('key', 'uid'), + ] + + key = models.CharField( + max_length=250, + blank=False, + ) + + uid = models.IntegerField( + ) + + updated = models.DateTimeField( + auto_now=True, + null=False, + ) + + @classmethod + def check_recent(cls, key: str, uid: int, delta: timedelta): + """ + Test if a particular notification has been sent in the specified time period + """ + + since = datetime.now().date() - delta + + entries = cls.objects.filter( + key=key, + uid=uid, + updated__gte=since + ) + + return entries.exists() + + @classmethod + def notify(cls, key: str, uid: int): + """ + Notify the database that a particular notification has been sent out + """ + + entry, created = cls.objects.get_or_create( + key=key, + uid=uid + ) + + entry.save() diff --git a/InvenTree/common/tasks.py b/InvenTree/common/tasks.py new file mode 100644 index 0000000000..409acf5a13 --- /dev/null +++ b/InvenTree/common/tasks.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +import logging +from datetime import timedelta, datetime + +from django.core.exceptions import AppRegistryNotReady + + +logger = logging.getLogger('inventree') + + +def delete_old_notifications(): + """ + Remove old notifications from the database. + + Anything older than ~3 months is removed + """ + + try: + from common.models import NotificationEntry + except AppRegistryNotReady: + logger.info("Could not perform 'delete_old_notifications' - App registry not ready") + return + + before = datetime.now() - timedelta(days=90) + + # Delete notification records before the specified date + NotificationEntry.objects.filter(updated__lte=before).delete() diff --git a/InvenTree/common/tests.py b/InvenTree/common/tests.py index 2fbb6b902f..0ec1812774 100644 --- a/InvenTree/common/tests.py +++ b/InvenTree/common/tests.py @@ -2,11 +2,12 @@ from __future__ import unicode_literals from http import HTTPStatus import json +from datetime import timedelta from django.test import TestCase, Client from django.contrib.auth import get_user_model -from .models import InvenTreeSetting, WebhookEndpoint, WebhookMessage +from .models import InvenTreeSetting, WebhookEndpoint, WebhookMessage, NotificationEntry from .api import WebhookView @@ -200,3 +201,23 @@ class WebhookMessageTests(TestCase): assert str(response.content, 'utf-8') == WebhookView.model_class.MESSAGE_OK message = WebhookMessage.objects.get() assert message.body == {"this": "is a message"} + + +class NotificationTest(TestCase): + + def test_check_notification_entries(self): + + # Create some notification entries + + self.assertEqual(NotificationEntry.objects.count(), 0) + + NotificationEntry.notify('test.notification', 1) + + self.assertEqual(NotificationEntry.objects.count(), 1) + + delta = timedelta(days=1) + + self.assertFalse(NotificationEntry.check_recent('test.notification', 2, delta)) + self.assertFalse(NotificationEntry.check_recent('test.notification2', 1, delta)) + + self.assertTrue(NotificationEntry.check_recent('test.notification', 1, delta)) diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py index 2e434d928d..90543d429d 100644 --- a/InvenTree/part/admin.py +++ b/InvenTree/part/admin.py @@ -8,13 +8,7 @@ from import_export.resources import ModelResource from import_export.fields import Field import import_export.widgets as widgets -from .models import PartCategory, Part -from .models import PartAttachment, PartStar, PartRelated -from .models import BomItem -from .models import PartParameterTemplate, PartParameter -from .models import PartCategoryParameterTemplate -from .models import PartTestTemplate -from .models import PartSellPriceBreak, PartInternalPriceBreak +import part.models as models from stock.models import StockLocation from company.models import SupplierPart @@ -24,7 +18,7 @@ class PartResource(ModelResource): """ Class for managing Part data import/export """ # ForeignKey fields - category = Field(attribute='category', widget=widgets.ForeignKeyWidget(PartCategory)) + category = Field(attribute='category', widget=widgets.ForeignKeyWidget(models.PartCategory)) default_location = Field(attribute='default_location', widget=widgets.ForeignKeyWidget(StockLocation)) @@ -32,7 +26,7 @@ class PartResource(ModelResource): category_name = Field(attribute='category__name', readonly=True) - variant_of = Field(attribute='variant_of', widget=widgets.ForeignKeyWidget(Part)) + variant_of = Field(attribute='variant_of', widget=widgets.ForeignKeyWidget(models.Part)) suppliers = Field(attribute='supplier_count', readonly=True) @@ -48,7 +42,7 @@ class PartResource(ModelResource): building = Field(attribute='quantity_being_built', readonly=True, widget=widgets.IntegerWidget()) class Meta: - model = Part + model = models.Part skip_unchanged = True report_skipped = False clean_model_instances = True @@ -86,14 +80,14 @@ class PartAdmin(ImportExportModelAdmin): class PartCategoryResource(ModelResource): """ Class for managing PartCategory data import/export """ - parent = Field(attribute='parent', widget=widgets.ForeignKeyWidget(PartCategory)) + parent = Field(attribute='parent', widget=widgets.ForeignKeyWidget(models.PartCategory)) parent_name = Field(attribute='parent__name', readonly=True) default_location = Field(attribute='default_location', widget=widgets.ForeignKeyWidget(StockLocation)) class Meta: - model = PartCategory + model = models.PartCategory skip_unchanged = True report_skipped = False clean_model_instances = True @@ -108,14 +102,14 @@ class PartCategoryResource(ModelResource): super().after_import(dataset, result, using_transactions, dry_run, **kwargs) # Rebuild the PartCategory tree(s) - PartCategory.objects.rebuild() + models.PartCategory.objects.rebuild() class PartCategoryInline(admin.TabularInline): """ Inline for PartCategory model """ - model = PartCategory + model = models.PartCategory class PartCategoryAdmin(ImportExportModelAdmin): @@ -146,6 +140,11 @@ class PartStarAdmin(admin.ModelAdmin): list_display = ('part', 'user') +class PartCategoryStarAdmin(admin.ModelAdmin): + + list_display = ('category', 'user') + + class PartTestTemplateAdmin(admin.ModelAdmin): list_display = ('part', 'test_name', 'required') @@ -159,7 +158,7 @@ class BomItemResource(ModelResource): bom_id = Field(attribute='pk') # ID of the parent part - parent_part_id = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part)) + parent_part_id = Field(attribute='part', widget=widgets.ForeignKeyWidget(models.Part)) # IPN of the parent part parent_part_ipn = Field(attribute='part__IPN', readonly=True) @@ -168,7 +167,7 @@ class BomItemResource(ModelResource): parent_part_name = Field(attribute='part__name', readonly=True) # ID of the sub-part - part_id = Field(attribute='sub_part', widget=widgets.ForeignKeyWidget(Part)) + part_id = Field(attribute='sub_part', widget=widgets.ForeignKeyWidget(models.Part)) # IPN of the sub-part part_ipn = Field(attribute='sub_part__IPN', readonly=True) @@ -233,7 +232,7 @@ class BomItemResource(ModelResource): return fields class Meta: - model = BomItem + model = models.BomItem skip_unchanged = True report_skipped = False clean_model_instances = True @@ -262,16 +261,16 @@ class ParameterTemplateAdmin(ImportExportModelAdmin): class ParameterResource(ModelResource): """ Class for managing PartParameter data import/export """ - part = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part)) + part = Field(attribute='part', widget=widgets.ForeignKeyWidget(models.Part)) part_name = Field(attribute='part__name', readonly=True) - template = Field(attribute='template', widget=widgets.ForeignKeyWidget(PartParameterTemplate)) + template = Field(attribute='template', widget=widgets.ForeignKeyWidget(models.PartParameterTemplate)) template_name = Field(attribute='template__name', readonly=True) class Meta: - model = PartParameter + model = models.PartParameter skip_unchanged = True report_skipped = False clean_model_instance = True @@ -292,7 +291,7 @@ class PartCategoryParameterAdmin(admin.ModelAdmin): class PartSellPriceBreakAdmin(admin.ModelAdmin): class Meta: - model = PartSellPriceBreak + model = models.PartSellPriceBreak list_display = ('part', 'quantity', 'price',) @@ -300,20 +299,21 @@ class PartSellPriceBreakAdmin(admin.ModelAdmin): class PartInternalPriceBreakAdmin(admin.ModelAdmin): class Meta: - model = PartInternalPriceBreak + model = models.PartInternalPriceBreak list_display = ('part', 'quantity', 'price',) -admin.site.register(Part, PartAdmin) -admin.site.register(PartCategory, PartCategoryAdmin) -admin.site.register(PartRelated, PartRelatedAdmin) -admin.site.register(PartAttachment, PartAttachmentAdmin) -admin.site.register(PartStar, PartStarAdmin) -admin.site.register(BomItem, BomItemAdmin) -admin.site.register(PartParameterTemplate, ParameterTemplateAdmin) -admin.site.register(PartParameter, ParameterAdmin) -admin.site.register(PartCategoryParameterTemplate, PartCategoryParameterAdmin) -admin.site.register(PartTestTemplate, PartTestTemplateAdmin) -admin.site.register(PartSellPriceBreak, PartSellPriceBreakAdmin) -admin.site.register(PartInternalPriceBreak, PartInternalPriceBreakAdmin) +admin.site.register(models.Part, PartAdmin) +admin.site.register(models.PartCategory, PartCategoryAdmin) +admin.site.register(models.PartRelated, PartRelatedAdmin) +admin.site.register(models.PartAttachment, PartAttachmentAdmin) +admin.site.register(models.PartStar, PartStarAdmin) +admin.site.register(models.PartCategoryStar, PartCategoryStarAdmin) +admin.site.register(models.BomItem, BomItemAdmin) +admin.site.register(models.PartParameterTemplate, ParameterTemplateAdmin) +admin.site.register(models.PartParameter, ParameterAdmin) +admin.site.register(models.PartCategoryParameterTemplate, PartCategoryParameterAdmin) +admin.site.register(models.PartTestTemplate, PartTestTemplateAdmin) +admin.site.register(models.PartSellPriceBreak, PartSellPriceBreakAdmin) +admin.site.register(models.PartInternalPriceBreak, PartInternalPriceBreakAdmin) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index a11bb1b088..b08834445c 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -58,6 +58,18 @@ class CategoryList(generics.ListCreateAPIView): queryset = PartCategory.objects.all() serializer_class = part_serializers.CategorySerializer + def get_serializer_context(self): + + ctx = super().get_serializer_context() + + try: + ctx['starred_categories'] = [star.category for star in self.request.user.starred_categories.all()] + except AttributeError: + # Error is thrown if the view does not have an associated request + ctx['starred_categories'] = [] + + return ctx + def filter_queryset(self, queryset): """ Custom filtering: @@ -110,6 +122,18 @@ class CategoryList(generics.ListCreateAPIView): except (ValueError, PartCategory.DoesNotExist): pass + # Filter by "starred" status + starred = params.get('starred', None) + + if starred is not None: + starred = str2bool(starred) + starred_categories = [star.category.pk for star in self.request.user.starred_categories.all()] + + if starred: + queryset = queryset.filter(pk__in=starred_categories) + else: + queryset = queryset.exclude(pk__in=starred_categories) + return queryset filter_backends = [ @@ -149,6 +173,29 @@ class CategoryDetail(generics.RetrieveUpdateDestroyAPIView): serializer_class = part_serializers.CategorySerializer queryset = PartCategory.objects.all() + def get_serializer_context(self): + + ctx = super().get_serializer_context() + + try: + ctx['starred_categories'] = [star.category for star in self.request.user.starred_categories.all()] + except AttributeError: + # Error is thrown if the view does not have an associated request + ctx['starred_categories'] = [] + + return ctx + + def update(self, request, *args, **kwargs): + + if 'starred' in request.data: + starred = str2bool(request.data.get('starred', False)) + + self.get_object().set_starred(request.user, starred) + + response = super().update(request, *args, **kwargs) + + return response + class CategoryParameterList(generics.ListAPIView): """ API endpoint for accessing a list of PartCategoryParameterTemplate objects. @@ -389,7 +436,7 @@ class PartDetail(generics.RetrieveUpdateDestroyAPIView): # Ensure the request context is passed through kwargs['context'] = self.get_serializer_context() - # Pass a list of "starred" parts fo the current user to the serializer + # Pass a list of "starred" parts of the current user to the serializer # We do this to reduce the number of database queries required! if self.starred_parts is None and self.request is not None: self.starred_parts = [star.part for star in self.request.user.starred_parts.all()] @@ -418,9 +465,9 @@ class PartDetail(generics.RetrieveUpdateDestroyAPIView): """ if 'starred' in request.data: - starred = str2bool(request.data.get('starred', None)) + starred = str2bool(request.data.get('starred', False)) - self.get_object().setStarred(request.user, starred) + self.get_object().set_starred(request.user, starred) response = super().update(request, *args, **kwargs) diff --git a/InvenTree/part/bom.py b/InvenTree/part/bom.py index f67e4ffe8f..d09cc5130a 100644 --- a/InvenTree/part/bom.py +++ b/InvenTree/part/bom.py @@ -160,171 +160,108 @@ def ExportBom(part, fmt='csv', cascade=False, max_levels=None, parameter_data=Fa # Add stock columns to dataset add_columns_to_dataset(stock_cols, len(bom_items)) - if manufacturer_data and supplier_data: + if manufacturer_data or supplier_data: """ If requested, add extra columns for each SupplierPart and ManufacturerPart associated with each line item """ - # Expand dataset with manufacturer parts - manufacturer_headers = [ - _('Manufacturer'), - _('MPN'), - ] - - supplier_headers = [ - _('Supplier'), - _('SKU'), - ] + # Keep track of the supplier parts we have already exported + supplier_parts_used = set() manufacturer_cols = {} - for b_idx, bom_item in enumerate(bom_items): + for bom_idx, bom_item in enumerate(bom_items): # Get part instance b_part = bom_item.sub_part - # Filter manufacturer parts - manufacturer_parts = ManufacturerPart.objects.filter(part__pk=b_part.pk) - manufacturer_parts = manufacturer_parts.prefetch_related('supplier_parts') + # Include manufacturer data for each BOM item + if manufacturer_data: - # Process manufacturer part - for manufacturer_idx, manufacturer_part in enumerate(manufacturer_parts): + # Filter manufacturer parts + manufacturer_parts = ManufacturerPart.objects.filter(part__pk=b_part.pk).prefetch_related('supplier_parts') + + for mp_idx, mp_part in enumerate(manufacturer_parts): - if manufacturer_part and manufacturer_part.manufacturer: - manufacturer_name = manufacturer_part.manufacturer.name - else: - manufacturer_name = '' + # Extract the "name" field of the Manufacturer (Company) + if mp_part and mp_part.manufacturer: + manufacturer_name = mp_part.manufacturer.name + else: + manufacturer_name = '' - if manufacturer_part: - manufacturer_mpn = manufacturer_part.MPN - else: - manufacturer_mpn = '' + # Extract the "MPN" field from the Manufacturer Part + if mp_part: + manufacturer_mpn = mp_part.MPN + else: + manufacturer_mpn = '' - # Generate column names for this manufacturer - k_man = manufacturer_headers[0] + "_" + str(manufacturer_idx) - k_mpn = manufacturer_headers[1] + "_" + str(manufacturer_idx) + # Generate a column name for this manufacturer + k_man = f'{_("Manufacturer")}_{mp_idx}' + k_mpn = f'{_("MPN")}_{mp_idx}' + + try: + manufacturer_cols[k_man].update({bom_idx: manufacturer_name}) + manufacturer_cols[k_mpn].update({bom_idx: manufacturer_mpn}) + except KeyError: + manufacturer_cols[k_man] = {bom_idx: manufacturer_name} + manufacturer_cols[k_mpn] = {bom_idx: manufacturer_mpn} - try: - manufacturer_cols[k_man].update({b_idx: manufacturer_name}) - manufacturer_cols[k_mpn].update({b_idx: manufacturer_mpn}) - except KeyError: - manufacturer_cols[k_man] = {b_idx: manufacturer_name} - manufacturer_cols[k_mpn] = {b_idx: manufacturer_mpn} + # We wish to include supplier data for this manufacturer part + if supplier_data: + + for sp_idx, sp_part in enumerate(mp_part.supplier_parts.all()): - # Process supplier parts - for supplier_idx, supplier_part in enumerate(manufacturer_part.supplier_parts.all()): + supplier_parts_used.add(sp_part) - if supplier_part.supplier and supplier_part.supplier: - supplier_name = supplier_part.supplier.name + if sp_part.supplier and sp_part.supplier: + supplier_name = sp_part.supplier.name + else: + supplier_name = '' + + if sp_part: + supplier_sku = sp_part.SKU + else: + supplier_sku = '' + + # Generate column names for this supplier + k_sup = str(_("Supplier")) + "_" + str(mp_idx) + "_" + str(sp_idx) + k_sku = str(_("SKU")) + "_" + str(mp_idx) + "_" + str(sp_idx) + + try: + manufacturer_cols[k_sup].update({bom_idx: supplier_name}) + manufacturer_cols[k_sku].update({bom_idx: supplier_sku}) + except KeyError: + manufacturer_cols[k_sup] = {bom_idx: supplier_name} + manufacturer_cols[k_sku] = {bom_idx: supplier_sku} + + if supplier_data: + # Add in any extra supplier parts, which are not associated with a manufacturer part + + for sp_idx, sp_part in enumerate(SupplierPart.objects.filter(part__pk=b_part.pk)): + + if sp_part in supplier_parts_used: + continue + + supplier_parts_used.add(sp_part) + + if sp_part.supplier: + supplier_name = sp_part.supplier.name else: supplier_name = '' - if supplier_part: - supplier_sku = supplier_part.SKU - else: - supplier_sku = '' + supplier_sku = sp_part.SKU # Generate column names for this supplier - k_sup = str(supplier_headers[0]) + "_" + str(manufacturer_idx) + "_" + str(supplier_idx) - k_sku = str(supplier_headers[1]) + "_" + str(manufacturer_idx) + "_" + str(supplier_idx) + k_sup = str(_("Supplier")) + "_" + str(sp_idx) + k_sku = str(_("SKU")) + "_" + str(sp_idx) try: - manufacturer_cols[k_sup].update({b_idx: supplier_name}) - manufacturer_cols[k_sku].update({b_idx: supplier_sku}) + manufacturer_cols[k_sup].update({bom_idx: supplier_name}) + manufacturer_cols[k_sku].update({bom_idx: supplier_sku}) except KeyError: - manufacturer_cols[k_sup] = {b_idx: supplier_name} - manufacturer_cols[k_sku] = {b_idx: supplier_sku} + manufacturer_cols[k_sup] = {bom_idx: supplier_name} + manufacturer_cols[k_sku] = {bom_idx: supplier_sku} - # Add manufacturer columns to dataset - add_columns_to_dataset(manufacturer_cols, len(bom_items)) - - elif manufacturer_data: - """ - If requested, add extra columns for each ManufacturerPart associated with each line item - """ - - # Expand dataset with manufacturer parts - manufacturer_headers = [ - _('Manufacturer'), - _('MPN'), - ] - - manufacturer_cols = {} - - for b_idx, bom_item in enumerate(bom_items): - # Get part instance - b_part = bom_item.sub_part - - # Filter supplier parts - manufacturer_parts = ManufacturerPart.objects.filter(part__pk=b_part.pk) - - for idx, manufacturer_part in enumerate(manufacturer_parts): - - if manufacturer_part: - manufacturer_name = manufacturer_part.manufacturer.name - else: - manufacturer_name = '' - - manufacturer_mpn = manufacturer_part.MPN - - # Add manufacturer data to the manufacturer columns - - # Generate column names for this manufacturer - k_man = manufacturer_headers[0] + "_" + str(idx) - k_mpn = manufacturer_headers[1] + "_" + str(idx) - - try: - manufacturer_cols[k_man].update({b_idx: manufacturer_name}) - manufacturer_cols[k_mpn].update({b_idx: manufacturer_mpn}) - except KeyError: - manufacturer_cols[k_man] = {b_idx: manufacturer_name} - manufacturer_cols[k_mpn] = {b_idx: manufacturer_mpn} - - # Add manufacturer columns to dataset - add_columns_to_dataset(manufacturer_cols, len(bom_items)) - - elif supplier_data: - """ - If requested, add extra columns for each SupplierPart associated with each line item - """ - - # Expand dataset with manufacturer parts - manufacturer_headers = [ - _('Supplier'), - _('SKU'), - ] - - manufacturer_cols = {} - - for b_idx, bom_item in enumerate(bom_items): - # Get part instance - b_part = bom_item.sub_part - - # Filter supplier parts - supplier_parts = SupplierPart.objects.filter(part__pk=b_part.pk) - - for idx, supplier_part in enumerate(supplier_parts): - - if supplier_part.supplier: - supplier_name = supplier_part.supplier.name - else: - supplier_name = '' - - supplier_sku = supplier_part.SKU - - # Add manufacturer data to the manufacturer columns - - # Generate column names for this supplier - k_sup = manufacturer_headers[0] + "_" + str(idx) - k_sku = manufacturer_headers[1] + "_" + str(idx) - - try: - manufacturer_cols[k_sup].update({b_idx: supplier_name}) - manufacturer_cols[k_sku].update({b_idx: supplier_sku}) - except KeyError: - manufacturer_cols[k_sup] = {b_idx: supplier_name} - manufacturer_cols[k_sku] = {b_idx: supplier_sku} - - # Add manufacturer columns to dataset + # Add supplier columns to dataset add_columns_to_dataset(manufacturer_cols, len(bom_items)) data = dataset.export(fmt) diff --git a/InvenTree/part/migrations/0074_partcategorystar.py b/InvenTree/part/migrations/0074_partcategorystar.py new file mode 100644 index 0000000000..0015212d2e --- /dev/null +++ b/InvenTree/part/migrations/0074_partcategorystar.py @@ -0,0 +1,27 @@ +# Generated by Django 3.2.5 on 2021-11-03 07:03 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('part', '0073_auto_20211013_1048'), + ] + + operations = [ + migrations.CreateModel( + name='PartCategoryStar', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='starred_users', to='part.partcategory', verbose_name='Category')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='starred_categories', to=settings.AUTH_USER_MODEL, verbose_name='User')), + ], + options={ + 'unique_together': {('category', 'user')}, + }, + ), + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 050b46058a..23aea29dbd 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -20,7 +20,7 @@ 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 jinja2 import Template @@ -47,6 +47,7 @@ from InvenTree import validators from InvenTree.models import InvenTreeTree, InvenTreeAttachment from InvenTree.fields import InvenTreeURLField from InvenTree.helpers import decimal2string, normalize, decimal2money +import InvenTree.tasks from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus, SalesOrderStatus @@ -56,6 +57,7 @@ from company.models import SupplierPart from stock import models as StockModels import common.models + import part.settings as part_settings @@ -102,11 +104,11 @@ class PartCategory(InvenTreeTree): if cascade: """ Select any parts which exist in this category or any child categories """ - query = Part.objects.filter(category__in=self.getUniqueChildren(include_self=True)) + queryset = Part.objects.filter(category__in=self.getUniqueChildren(include_self=True)) else: - query = Part.objects.filter(category=self.pk) + queryset = Part.objects.filter(category=self.pk) - return query + return queryset @property def item_count(self): @@ -201,6 +203,60 @@ class PartCategory(InvenTreeTree): return prefetch.filter(category=self.id) + def get_subscribers(self, include_parents=True): + """ + Return a list of users who subscribe to this PartCategory + """ + + cats = self.get_ancestors(include_self=True) + + subscribers = set() + + if include_parents: + queryset = PartCategoryStar.objects.filter( + category__pk__in=[cat.pk for cat in cats] + ) + else: + queryset = PartCategoryStar.objects.filter( + category=self, + ) + + for result in queryset: + subscribers.add(result.user) + + return [s for s in subscribers] + + def is_starred_by(self, user, **kwargs): + """ + Returns True if the specified user subscribes to this category + """ + + return user in self.get_subscribers(**kwargs) + + def set_starred(self, user, status): + """ + Set the "subscription" status of this PartCategory against the specified user + """ + + if not user: + return + + if self.is_starred_by(user) == status: + return + + if status: + PartCategoryStar.objects.create( + category=self, + user=user + ) + else: + # Note that this won't actually stop the user being subscribed, + # if the user is subscribed to a parent category + PartCategoryStar.objects.filter( + category=self, + user=user, + ).delete() + @receiver(pre_delete, sender=PartCategory, dispatch_uid='partcategory_delete_log') def before_delete_part_category(sender, instance, using, **kwargs): @@ -332,9 +388,16 @@ class Part(MPTTModel): context = {} - context['starred'] = self.isStarredBy(request.user) context['disabled'] = not self.active + # Subscription status + context['starred'] = self.is_starred_by(request.user) + context['starred_directly'] = context['starred'] and self.is_starred_by( + request.user, + include_variants=False, + include_categories=False + ) + # Pre-calculate complex queries so they only need to be performed once context['total_stock'] = self.total_stock @@ -1040,30 +1103,65 @@ class Part(MPTTModel): return self.total_stock - self.allocation_count() + self.on_order - def isStarredBy(self, user): - """ Return True if this part has been starred by a particular user """ - - try: - PartStar.objects.get(part=self, user=user) - return True - except PartStar.DoesNotExist: - return False - - def setStarred(self, user, starred): + def get_subscribers(self, include_variants=True, include_categories=True): """ - Set the "starred" status of this Part for the given user + Return a list of users who are 'subscribed' to this part. + + A user may 'subscribe' to this part in the following ways: + + a) Subscribing to the part instance directly + b) Subscribing to a template part "above" this part (if it is a variant) + c) Subscribing to the part category that this part belongs to + d) Subscribing to a parent category of the category in c) + + """ + + subscribers = set() + + # Start by looking at direct subscriptions to a Part model + queryset = PartStar.objects.all() + + if include_variants: + queryset = queryset.filter( + part__pk__in=[part.pk for part in self.get_ancestors(include_self=True)] + ) + else: + queryset = queryset.filter(part=self) + + for star in queryset: + subscribers.add(star.user) + + if include_categories and self.category: + + for sub in self.category.get_subscribers(): + subscribers.add(sub) + + return [s for s in subscribers] + + def is_starred_by(self, user, **kwargs): + """ + Return True if the specified user subscribes to this part + """ + + return user in self.get_subscribers(**kwargs) + + def set_starred(self, user, status): + """ + Set the "subscription" status of this Part against the specified user """ if not user: return - # Do not duplicate efforts - if self.isStarredBy(user) == starred: + # Already subscribed? + if self.is_starred_by(user) == status: return - if starred: + if status: PartStar.objects.create(part=self, user=user) else: + # Note that this won't actually stop the user being subscribed, + # if the user is subscribed to a parent part or category PartStar.objects.filter(part=self, user=user).delete() def need_to_restock(self): @@ -1226,6 +1324,17 @@ class Part(MPTTModel): return query + def get_stock_count(self, include_variants=True): + """ + Return the total "in stock" count for this part + """ + + entries = self.stock_entries(in_stock=True, include_variants=include_variants) + + query = entries.aggregate(t=Coalesce(Sum('quantity'), Decimal(0))) + + return query['t'] + @property def total_stock(self): """ Return the total stock quantity for this part. @@ -1234,11 +1343,7 @@ class Part(MPTTModel): - If this part is a "template" (variants exist) then these are counted too """ - entries = self.stock_entries(in_stock=True) - - query = entries.aggregate(t=Coalesce(Sum('quantity'), Decimal(0))) - - return query['t'] + return self.get_stock_count() def get_bom_item_filter(self, include_inherited=True): """ @@ -1989,7 +2094,24 @@ class Part(MPTTModel): return len(self.get_related_parts()) def is_part_low_on_stock(self): - return self.total_stock <= self.minimum_stock + """ + Returns True if the total stock for this part is less than the minimum stock level + """ + + return self.get_stock_count() < self.minimum_stock + + +@receiver(post_save, sender=Part, dispatch_uid='part_post_save_log') +def after_save_part(sender, instance: Part, created, **kwargs): + """ + Function to be executed after a Part is saved + """ + + if not created: + # Check part stock only if we are *updating* the part (not creating it) + + # Run this check in the background + InvenTree.tasks.offload_task('part.tasks.notify_low_stock_if_required', instance) def attach_file(instance, filename): @@ -2062,10 +2184,9 @@ class PartInternalPriceBreak(common.models.PriceBreak): class PartStar(models.Model): - """ A PartStar object creates a relationship between a User and a Part. + """ A PartStar object creates a subscription relationship between a User and a Part. - It is used to designate a Part as 'starred' (or favourited) for a given User, - so that the user can track a list of their favourite parts. + It is used to designate a Part as 'subscribed' for a given User. Attributes: part: Link to a Part object @@ -2077,7 +2198,30 @@ class PartStar(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('User'), related_name='starred_parts') class Meta: - unique_together = ['part', 'user'] + unique_together = [ + 'part', + 'user' + ] + + +class PartCategoryStar(models.Model): + """ + A PartCategoryStar creates a subscription relationship between a User and a PartCategory. + + Attributes: + category: Link to a PartCategory object + user: Link to a User object + """ + + category = models.ForeignKey(PartCategory, on_delete=models.CASCADE, verbose_name=_('Category'), related_name='starred_users') + + user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('User'), related_name='starred_categories') + + class Meta: + unique_together = [ + 'category', + 'user', + ] class PartTestTemplate(models.Model): diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index ff1fb2c8c6..47ce3f66c8 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -33,12 +33,25 @@ from .models import (BomItem, BomItemSubstitute, class CategorySerializer(InvenTreeModelSerializer): """ Serializer for PartCategory """ + def __init__(self, *args, **kwargs): + + super().__init__(*args, **kwargs) + + def get_starred(self, category): + """ + Return True if the category is directly "starred" by the current user + """ + + return category in self.context.get('starred_categories', []) + url = serializers.CharField(source='get_absolute_url', read_only=True) parts = serializers.IntegerField(source='item_count', read_only=True) level = serializers.IntegerField(read_only=True) + starred = serializers.SerializerMethodField() + class Meta: model = PartCategory fields = [ @@ -51,6 +64,7 @@ class CategorySerializer(InvenTreeModelSerializer): 'parent', 'parts', 'pathstring', + 'starred', 'url', ] @@ -241,6 +255,9 @@ class PartSerializer(InvenTreeModelSerializer): to reduce database trips. """ + # TODO: Update the "in_stock" annotation to include stock for variants of the part + # Ref: https://github.com/inventree/InvenTree/issues/2240 + # Annotate with the total 'in stock' quantity queryset = queryset.annotate( in_stock=Coalesce( diff --git a/InvenTree/part/tasks.py b/InvenTree/part/tasks.py index 72d996e772..0cd9cf09a7 100644 --- a/InvenTree/part/tasks.py +++ b/InvenTree/part/tasks.py @@ -2,34 +2,47 @@ 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 -from part.models import Part +import part.models logger = logging.getLogger("inventree") -def notify_low_stock(part: Part): +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}") - starred_users_email = EmailAddress.objects.filter(user__starred_parts__part=part) + # Get a list of users who are subcribed to this part + subscribers = part.get_subscribers() + + emails = EmailAddress.objects.filter( + user__in=subscribers, + ) # TODO: In the future, include the part image in the email template - if len(starred_users_email) > 0: + if len(emails) > 0: logger.info(f"Notify users regarding low stock of {part.name}") context = { # Pass the "Part" object through to the template context @@ -37,22 +50,28 @@ def notify_low_stock(part: Part): 'link': InvenTree.helpers.construct_absolute_url(part.get_absolute_url()), } - subject = _(f'[InvenTree] {part.name} is low on stock') + subject = "[InvenTree] " + _("Low stock notification") html_message = render_to_string('email/low_stock_notification.html', context) - recipients = starred_users_email.values_list('email', flat=True) + recipients = emails.values_list('email', flat=True) 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): + +def notify_low_stock_if_required(part: part.models.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( - 'part.tasks.notify_low_stock', - part - ) + # Run "up" the tree, to allow notification for "parent" parts + parts = part.get_ancestors(include_self=True, ascending=True) + + for p in parts: + if p.is_part_low_on_stock(): + InvenTree.tasks.offload_task( + 'part.tasks.notify_low_stock', + p + ) diff --git a/InvenTree/part/templates/part/category.html b/InvenTree/part/templates/part/category.html index 03369b093d..48677ee71d 100644 --- a/InvenTree/part/templates/part/category.html +++ b/InvenTree/part/templates/part/category.html @@ -20,15 +20,37 @@ {% include "admin_button.html" with url=url %} {% endif %} {% if category %} -{% if roles.part_category.change %} - +{% elif starred %} + +{% else %} + {% endif %} -{% if roles.part_category.delete %} - +{% if roles.part_category.change or roles.part_category.delete %} +
    + + +
    {% endif %} {% endif %} {% if roles.part_category.add %} @@ -198,6 +220,14 @@ data: {{ parameters|safe }}, } ); + + $("#toggle-starred").click(function() { + toggleStar({ + url: '{% url "api-part-category-detail" category.pk %}', + button: '#category-star-icon' + }); + }); + {% endif %} enableSidebar('category'); diff --git a/InvenTree/part/templates/part/detail.html b/InvenTree/part/templates/part/detail.html index fc9795b6e0..706bf5e329 100644 --- a/InvenTree/part/templates/part/detail.html +++ b/InvenTree/part/templates/part/detail.html @@ -35,7 +35,7 @@ {% trans "Category" %} - {{ part.category }} + {{ part.category.name }} {% endif %} @@ -62,7 +62,7 @@ {% endif %} {% if part.minimum_stock %} - + {% trans "Minimum stock level" %} {{ part.minimum_stock }} diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index 16b119e02d..bb7aea3abb 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -23,9 +23,19 @@ {% include "admin_button.html" with url=url %} {% endif %} - +{% elif starred %} + +{% else %} + +{% endif %} {% if barcodes %} @@ -137,8 +147,6 @@ - -
    {% if part.variant_of %} @@ -164,6 +172,13 @@ {% trans "In Stock" %} {% include "part/stock_count.html" %} + {% if part.minimum_stock %} + + + {% trans "Minimum Stock" %} + {{ part.minimum_stock }} + + {% endif %} {% if on_order > 0 %} @@ -310,7 +325,7 @@ $("#toggle-starred").click(function() { toggleStar({ - part: {{ part.id }}, + url: '{% url "api-part-detail" part.pk %}', button: '#part-star-icon', }); }); diff --git a/InvenTree/part/test_part.py b/InvenTree/part/test_part.py index 1bd9fdf87d..755bd45cea 100644 --- a/InvenTree/part/test_part.py +++ b/InvenTree/part/test_part.py @@ -11,7 +11,7 @@ from django.core.exceptions import ValidationError import os -from .models import Part, PartCategory, PartTestTemplate +from .models import Part, PartCategory, PartCategoryStar, PartStar, PartTestTemplate from .models import rename_part_image from .templatetags import inventree_extras @@ -347,3 +347,120 @@ class PartSettingsTest(TestCase): with self.assertRaises(ValidationError): part = Part(name='Hello', description='A thing', IPN='IPN123', revision='C') part.full_clean() + + +class PartSubscriptionTests(TestCase): + + fixtures = [ + 'location', + 'category', + 'part', + ] + + def setUp(self): + # Create a user for auth + user = get_user_model() + + self.user = user.objects.create_user( + username='testuser', + email='test@testing.com', + password='password', + is_staff=True + ) + + # electronics / IC / MCU + self.category = PartCategory.objects.get(pk=4) + + self.part = Part.objects.create( + category=self.category, + name='STM32F103', + description='Currently worth a lot of money', + is_template=True, + ) + + def test_part_subcription(self): + """ + Test basic subscription against a part + """ + + # First check that the user is *not* subscribed to the part + self.assertFalse(self.part.is_starred_by(self.user)) + + # Now, subscribe directly to the part + self.part.set_starred(self.user, True) + + self.assertEqual(PartStar.objects.count(), 1) + + self.assertTrue(self.part.is_starred_by(self.user)) + + # Now, unsubscribe + self.part.set_starred(self.user, False) + + self.assertFalse(self.part.is_starred_by(self.user)) + + def test_variant_subscription(self): + """ + Test subscription against a parent part + """ + + # Construct a sub-part to star against + sub_part = Part.objects.create( + name='sub_part', + description='a sub part', + variant_of=self.part, + ) + + self.assertFalse(sub_part.is_starred_by(self.user)) + + # Subscribe to the "parent" part + self.part.set_starred(self.user, True) + + self.assertTrue(self.part.is_starred_by(self.user)) + self.assertTrue(sub_part.is_starred_by(self.user)) + + def test_category_subscription(self): + """ + Test subscription against a PartCategory + """ + + self.assertEqual(PartCategoryStar.objects.count(), 0) + + self.assertFalse(self.part.is_starred_by(self.user)) + self.assertFalse(self.category.is_starred_by(self.user)) + + # Subscribe to the direct parent category + self.category.set_starred(self.user, True) + + self.assertEqual(PartStar.objects.count(), 0) + self.assertEqual(PartCategoryStar.objects.count(), 1) + + self.assertTrue(self.category.is_starred_by(self.user)) + self.assertTrue(self.part.is_starred_by(self.user)) + + # Check that the "parent" category is not starred + self.assertFalse(self.category.parent.is_starred_by(self.user)) + + # Un-subscribe + self.category.set_starred(self.user, False) + + self.assertFalse(self.category.is_starred_by(self.user)) + self.assertFalse(self.part.is_starred_by(self.user)) + + def test_parent_category_subscription(self): + """ + Check that a parent category can be subscribed to + """ + + # Top-level "electronics" category + cat = PartCategory.objects.get(pk=1) + + cat.set_starred(self.user, True) + + # Check base category + self.assertTrue(cat.is_starred_by(self.user)) + + # Check lower level category + self.assertTrue(self.category.is_starred_by(self.user)) + + # Check part + self.assertTrue(self.part.is_starred_by(self.user)) diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 5a4167ea05..65c42f7e36 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -42,11 +42,12 @@ from common.files import FileManager from common.views import FileManagementFormView, FileManagementAjaxView from common.forms import UploadFileForm, MatchFieldForm -from stock.models import StockLocation +from stock.models import StockItem, StockLocation import common.settings as inventree_settings from . import forms as part_forms +from . import settings as part_settings from .bom import MakeBomTemplate, ExportBom, IsValidBOMFormat from order.models import PurchaseOrderLineItem @@ -245,6 +246,7 @@ class PartImport(FileManagementFormView): 'Category', 'default_location', 'default_supplier', + 'variant_of', ] OPTIONAL_HEADERS = [ @@ -256,6 +258,17 @@ class PartImport(FileManagementFormView): 'minimum_stock', 'Units', 'Notes', + 'Active', + 'base_cost', + 'Multiple', + 'Assembly', + 'Component', + 'is_template', + 'Purchaseable', + 'Salable', + 'Trackable', + 'Virtual', + 'Stock', ] name = 'part' @@ -284,6 +297,18 @@ class PartImport(FileManagementFormView): 'category': 'category', 'default_location': 'default_location', 'default_supplier': 'default_supplier', + 'variant_of': 'variant_of', + 'active': 'active', + 'base_cost': 'base_cost', + 'multiple': 'multiple', + 'assembly': 'assembly', + 'component': 'component', + 'is_template': 'is_template', + 'purchaseable': 'purchaseable', + 'salable': 'salable', + 'trackable': 'trackable', + 'virtual': 'virtual', + 'stock': 'stock', } file_manager_class = PartFileManager @@ -299,6 +324,8 @@ class PartImport(FileManagementFormView): self.matches['default_location'] = ['name__contains'] self.allowed_items['default_supplier'] = SupplierPart.objects.all() self.matches['default_supplier'] = ['SKU__contains'] + self.allowed_items['variant_of'] = Part.objects.all() + self.matches['variant_of'] = ['name__contains'] # setup self.file_manager.setup() @@ -364,9 +391,29 @@ class PartImport(FileManagementFormView): category=optional_matches['Category'], default_location=optional_matches['default_location'], default_supplier=optional_matches['default_supplier'], + variant_of=optional_matches['variant_of'], + active=str2bool(part_data.get('active', True)), + base_cost=part_data.get('base_cost', 0), + multiple=part_data.get('multiple', 1), + assembly=str2bool(part_data.get('assembly', part_settings.part_assembly_default())), + component=str2bool(part_data.get('component', part_settings.part_component_default())), + is_template=str2bool(part_data.get('is_template', part_settings.part_template_default())), + purchaseable=str2bool(part_data.get('purchaseable', part_settings.part_purchaseable_default())), + salable=str2bool(part_data.get('salable', part_settings.part_salable_default())), + trackable=str2bool(part_data.get('trackable', part_settings.part_trackable_default())), + virtual=str2bool(part_data.get('virtual', part_settings.part_virtual_default())), ) try: new_part.save() + + # add stock item if set + if part_data.get('stock', None): + stock = StockItem( + part=new_part, + location=new_part.default_location, + quantity=int(part_data.get('stock', 1)), + ) + stock.save() import_done += 1 except ValidationError as _e: import_error.append(', '.join(set(_e.messages))) @@ -412,6 +459,7 @@ class PartDetail(InvenTreeRoleMixin, DetailView): part = self.get_object() ctx = part.get_context_data(self.request) + context.update(**ctx) # Pricing information @@ -1469,18 +1517,29 @@ class CategoryDetail(InvenTreeRoleMixin, DetailView): if category: cascade = kwargs.get('cascade', True) + # Prefetch parts parameters parts_parameters = category.prefetch_parts_parameters(cascade=cascade) + # Get table headers (unique parameters names) context['headers'] = category.get_unique_parameters(cascade=cascade, prefetch=parts_parameters) + # Insert part information context['headers'].insert(0, 'description') context['headers'].insert(0, 'part') + # Get parameters data context['parameters'] = category.get_parts_parameters(cascade=cascade, prefetch=parts_parameters) + # Insert "starred" information + context['starred'] = category.is_starred_by(self.request.user) + context['starred_directly'] = context['starred'] and category.is_starred_by( + self.request.user, + include_parents=False, + ) + return context diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 657469a744..eb0e6aa12f 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -27,7 +27,9 @@ from mptt.managers import TreeManager from decimal import Decimal, InvalidOperation from datetime import datetime, timedelta + from InvenTree import helpers +import InvenTree.tasks import common.models import report.models @@ -41,7 +43,6 @@ 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): @@ -1658,16 +1659,18 @@ 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) + # Run this check in the background + InvenTree.tasks.offload_task('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): """ - Hook function to be executed after StockItem object is saved/updated + Hook function to be executed after StockItem object is saved/updated """ - part_tasks.notify_low_stock_if_required(instance.part) + # Run this check in the background + InvenTree.tasks.offload_task('part.tasks.notify_low_stock_if_required', instance.part) class StockItemAttachment(InvenTreeAttachment): diff --git a/InvenTree/templates/InvenTree/index.html b/InvenTree/templates/InvenTree/index.html index 95b6c15bf9..44bc70fc37 100644 --- a/InvenTree/templates/InvenTree/index.html +++ b/InvenTree/templates/InvenTree/index.html @@ -76,6 +76,7 @@ function addHeaderAction(label, title, icon, options) { } {% settings_value 'HOMEPAGE_PART_STARRED' user=request.user as setting_part_starred %} +{% settings_value 'HOMEPAGE_CATEGORY_STARRED' user=request.user as setting_category_starred %} {% settings_value 'HOMEPAGE_PART_LATEST' user=request.user as setting_part_latest %} {% settings_value 'HOMEPAGE_BOM_VALIDATION' user=request.user as setting_bom_validation %} {% to_list setting_part_starred setting_part_latest setting_bom_validation as settings_list_part %} @@ -84,15 +85,25 @@ function addHeaderAction(label, title, icon, options) { addHeaderTitle('{% trans "Parts" %}'); {% if setting_part_starred %} -addHeaderAction('starred-parts', '{% trans "Starred Parts" %}', 'fa-star'); +addHeaderAction('starred-parts', '{% trans "Subscribed Parts" %}', 'fa-bell'); loadSimplePartTable("#table-starred-parts", "{% url 'api-part-list' %}", { params: { - "starred": true, + starred: true, }, name: 'starred_parts', }); {% endif %} +{% if setting_category_starred %} +addHeaderAction('starred-categories', '{% trans "Subscribed Categories" %}', 'fa-bell'); +loadPartCategoryTable($('#table-starred-categories'), { + params: { + starred: true, + }, + name: 'starred_categories' +}); +{% endif %} + {% if setting_part_latest %} addHeaderAction('latest-parts', '{% trans "Latest Parts" %}', 'fa-newspaper'); loadSimplePartTable("#table-latest-parts", "{% url 'api-part-list' %}", { @@ -128,8 +139,7 @@ loadSimplePartTable("#table-bom-validation", "{% url 'api-part-list' %}", { {% to_list setting_stock_recent setting_stock_low setting_stock_depleted setting_stock_needed as settings_list_stock %} {% endif %} -{% if roles.stock.view and True in settings_list_stock %} -addHeaderTitle('{% trans "Stock" %}'); +{% if roles.stock.view %} {% if setting_stock_recent %} addHeaderAction('recently-updated-stock', '{% trans "Recently Updated" %}', 'fa-clock'); @@ -145,7 +155,7 @@ loadStockTable($('#table-recently-updated-stock'), { {% endif %} {% if setting_stock_low %} -addHeaderAction('low-stock', '{% trans "Low Stock" %}', 'fa-shopping-cart'); +addHeaderAction('low-stock', '{% trans "Low Stock" %}', 'fa-flag'); loadSimplePartTable("#table-low-stock", "{% url 'api-part-list' %}", { params: { low_stock: true, diff --git a/InvenTree/templates/InvenTree/settings/user_homepage.html b/InvenTree/templates/InvenTree/settings/user_homepage.html index 7eed850ad5..54e3bdcefd 100644 --- a/InvenTree/templates/InvenTree/settings/user_homepage.html +++ b/InvenTree/templates/InvenTree/settings/user_homepage.html @@ -14,7 +14,8 @@
    - {% include "InvenTree/settings/setting.html" with key="HOMEPAGE_PART_STARRED" icon='fa-star' user_setting=True %} + {% include "InvenTree/settings/setting.html" with key="HOMEPAGE_PART_STARRED" icon='fa-bell' user_setting=True %} + {% include "InvenTree/settings/setting.html" with key="HOMEPAGE_CATEGORY_STARRED" icon='fa-bell' user_setting=True %} {% include "InvenTree/settings/setting.html" with key="HOMEPAGE_PART_LATEST" icon='fa-history' user_setting=True %} {% include "InvenTree/settings/setting.html" with key="PART_RECENT_COUNT" icon="fa-clock" user_setting=True %} {% include "InvenTree/settings/setting.html" with key="HOMEPAGE_BOM_VALIDATION" user_setting=True %} diff --git a/InvenTree/templates/account/base.html b/InvenTree/templates/account/base.html index 048496c4a5..fa2a34f79d 100644 --- a/InvenTree/templates/account/base.html +++ b/InvenTree/templates/account/base.html @@ -10,6 +10,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -33,16 +53,23 @@ +
    +
    + +
    +
    {% trans "The following parts are low on required stock" %} + + + + + + + +{% for line in lines %} + + + + + + +{% endfor %} + +{% endblock body %} + +{% block footer_prefix %} +

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

    +{% endblock footer_prefix %} diff --git a/InvenTree/templates/email/low_stock_notification.html b/InvenTree/templates/email/low_stock_notification.html index ecb350925a..f922187f33 100644 --- a/InvenTree/templates/email/low_stock_notification.html +++ b/InvenTree/templates/email/low_stock_notification.html @@ -8,22 +8,25 @@ {% if link %}

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

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

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

    -{% endblock %} {% block body %} - - + + + - - + + + -{% endblock %} +{% endblock body %} + +{% block footer_prefix %} +

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

    +{% endblock footer_prefix %} diff --git a/InvenTree/templates/js/translated/api.js b/InvenTree/templates/js/translated/api.js index 841cf467ba..d0640408be 100644 --- a/InvenTree/templates/js/translated/api.js +++ b/InvenTree/templates/js/translated/api.js @@ -2,8 +2,6 @@ {% load inventree_extras %} /* globals - renderErrorMessage, - showAlertDialog, */ /* exported @@ -68,6 +66,8 @@ function inventreeGet(url, filters={}, options={}) { options.error({ error: thrownError }); + } else { + showApiError(xhr, url); } } }); @@ -104,6 +104,8 @@ function inventreeFormDataUpload(url, data, options={}) { if (options.error) { options.error(xhr, status, error); + } else { + showApiError(xhr, url); } } }); @@ -139,6 +141,8 @@ function inventreePut(url, data={}, options={}) { } else { console.error(`Error on ${method} to '${url}' - STATUS ${xhr.status}`); console.error(thrownError); + + showApiError(xhr, url); } }, complete: function(xhr, status) { @@ -162,8 +166,10 @@ function inventreeDelete(url, options={}) { return inventreePut(url, {}, options); } - -function showApiError(xhr) { +/* + * Display a notification with error information + */ +function showApiError(xhr, url) { var title = null; var message = null; @@ -208,7 +214,11 @@ function showApiError(xhr) { } message += '
    '; - message += renderErrorMessage(xhr); + message += `URL: ${url}`; - showAlertDialog(title, message); + showMessage(title, { + style: 'danger', + icon: 'fas fa-server icon-red', + details: message, + }); } diff --git a/InvenTree/templates/js/translated/barcode.js b/InvenTree/templates/js/translated/barcode.js index a1d6fb7adf..4b61249d0b 100644 --- a/InvenTree/templates/js/translated/barcode.js +++ b/InvenTree/templates/js/translated/barcode.js @@ -36,7 +36,7 @@ function makeBarcodeInput(placeholderText='', hintText='') {
    - + @@ -59,7 +59,7 @@ function makeNotesField(options={}) {
    - + @@ -480,10 +480,10 @@ function barcodeCheckIn(location_id) { $(modal).modal('hide'); if (status == 'success' && 'success' in response) { - showAlertOrCache('alert-success', response.success, true); + showAlertOrCache(response.success, 'success', true); location.reload(); } else { - showAlertOrCache('alert-success', '{% trans "Error transferring stock" %}', false); + showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false); } } } @@ -604,10 +604,10 @@ function scanItemsIntoLocation(item_id_list, options={}) { $(modal).modal('hide'); if (status == 'success' && 'success' in response) { - showAlertOrCache('alert-success', response.success, true); + showAlertOrCache(response.success, 'success', true); location.reload(); } else { - showAlertOrCache('alert-danger', '{% trans "Error transferring stock" %}', false); + showAlertOrCache('{% trans "Error transferring stock" %}', 'danger', false); } } } diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index dec9a4aaf7..499fde9bec 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -339,7 +339,7 @@ function completeBuildOutputs(build_id, outputs, options={}) { break; default: $(opts.modal).modal('hide'); - showApiError(xhr); + showApiError(xhr, opts.url); break; } } @@ -1527,7 +1527,7 @@ function allocateStockToBuild(build_id, part_id, bom_items, options={}) { break; default: $(opts.modal).modal('hide'); - showApiError(xhr); + showApiError(xhr, opts.url); break; } } diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 396226d52b..7ca2e77a3f 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -123,9 +123,10 @@ function getApiEndpointOptions(url, callback) { json: 'application/json', }, success: callback, - error: function() { + error: function(xhr) { // TODO: Handle error console.log(`ERROR in getApiEndpointOptions at '${url}'`); + showApiError(xhr, url); } }); } @@ -199,12 +200,14 @@ function constructChangeForm(fields, options) { // Store the entire data object options.instance = data; - + constructFormBody(fields, options); }, - error: function() { + error: function(xhr) { // TODO: Handle error here console.log(`ERROR in constructChangeForm at '${options.url}'`); + + showApiError(xhr, options.url); } }); } @@ -241,9 +244,11 @@ function constructDeleteForm(fields, options) { constructFormBody(fields, options); }, - error: function() { + error: function(xhr) { // TODO: Handle error here console.log(`ERROR in constructDeleteForm at '${options.url}`); + + showApiError(xhr, options.url); } }); } @@ -708,7 +713,7 @@ function submitFormData(fields, options) { break; default: $(options.modal).modal('hide'); - showApiError(xhr); + showApiError(xhr, options.url); break; } } @@ -885,19 +890,19 @@ function handleFormSuccess(response, options) { // Display any messages if (response && response.success) { - showAlertOrCache('alert-success', response.success, cache); + showAlertOrCache(response.success, 'success', cache); } if (response && response.info) { - showAlertOrCache('alert-info', response.info, cache); + showAlertOrCache(response.info, 'info', cache); } if (response && response.warning) { - showAlertOrCache('alert-warning', response.warning, cache); + showAlertOrCache(response.warning, 'warning', cache); } if (response && response.danger) { - showAlertOrCache('alert-danger', response.danger, cache); + showAlertOrCache(response.danger, 'dagner', cache); } if (options.onSuccess) { diff --git a/InvenTree/templates/js/translated/modals.js b/InvenTree/templates/js/translated/modals.js index 32b5260e76..aefdba604f 100644 --- a/InvenTree/templates/js/translated/modals.js +++ b/InvenTree/templates/js/translated/modals.js @@ -399,19 +399,19 @@ function afterForm(response, options) { // Display any messages if (response.success) { - showAlertOrCache('alert-success', response.success, cache); + showAlertOrCache(response.success, 'success', cache); } if (response.info) { - showAlertOrCache('alert-info', response.info, cache); + showAlertOrCache(response.info, 'info', cache); } if (response.warning) { - showAlertOrCache('alert-warning', response.warning, cache); + showAlertOrCache(response.warning, 'warning', cache); } if (response.danger) { - showAlertOrCache('alert-danger', response.danger, cache); + showAlertOrCache(response.danger, 'danger', cache); } // Was a callback provided? diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index 1008c77909..b0e3009720 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -555,7 +555,7 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { break; default: $(opts.modal).modal('hide'); - showApiError(xhr); + showApiError(xhr, opts.url); break; } } diff --git a/InvenTree/templates/js/translated/part.js b/InvenTree/templates/js/translated/part.js index 44295e67ea..742083bbe4 100644 --- a/InvenTree/templates/js/translated/part.js +++ b/InvenTree/templates/js/translated/part.js @@ -373,24 +373,23 @@ function duplicatePart(pk, options={}) { } +/* Toggle the 'starred' status of a part. + * Performs AJAX queries and updates the display on the button. + * + * options: + * - button: ID of the button (default = '#part-star-icon') + * - URL: API url of the object + * - user: pk of the user + */ function toggleStar(options) { - /* Toggle the 'starred' status of a part. - * Performs AJAX queries and updates the display on the button. - * - * options: - * - button: ID of the button (default = '#part-star-icon') - * - part: pk of the part object - * - user: pk of the user - */ - var url = `/api/part/${options.part}/`; - - inventreeGet(url, {}, { + inventreeGet(options.url, {}, { success: function(response) { + var starred = response.starred; inventreePut( - url, + options.url, { starred: !starred, }, @@ -398,9 +397,19 @@ function toggleStar(options) { method: 'PATCH', success: function(response) { if (response.starred) { - $(options.button).addClass('icon-yellow'); + $(options.button).removeClass('fa fa-bell-slash').addClass('fas fa-bell icon-green'); + $(options.button).attr('title', '{% trans "You are subscribed to notifications for this item" %}'); + + showMessage('{% trans "You have subscribed to notifications for this item" %}', { + style: 'success', + }); } else { - $(options.button).removeClass('icon-yellow'); + $(options.button).removeClass('fas fa-bell icon-green').addClass('fa fa-bell-slash'); + $(options.button).attr('title', '{% trans "Subscribe to notifications for this item" %}'); + + showMessage('{% trans "You have unsubscribed to notifications for this item" %}', { + style: 'warning', + }); } } } @@ -410,12 +419,12 @@ function toggleStar(options) { } -function partStockLabel(part) { +function partStockLabel(part, options={}) { if (part.in_stock) { - return `{% trans "Stock" %}: ${part.in_stock}`; + return `{% trans "Stock" %}: ${part.in_stock}`; } else { - return `{% trans "No Stock" %}`; + return `{% trans "No Stock" %}`; } } @@ -443,7 +452,7 @@ function makePartIcons(part) { } if (part.starred) { - html += makeIconBadge('fa-star', '{% trans "Starred part" %}'); + html += makeIconBadge('fa-bell icon-green', '{% trans "Subscribed part" %}'); } if (part.salable) { @@ -451,7 +460,7 @@ function makePartIcons(part) { } if (!part.active) { - html += `{% trans "Inactive" %}`; + html += `{% trans "Inactive" %} `; } return html; @@ -1150,6 +1159,13 @@ function loadPartCategoryTable(table, options) { filters = loadTableFilters(filterKey); } + + var tree_view = options.allowTreeView && inventreeLoad('category-tree-view') == 1; + + if (tree_view) { + params.cascade = true; + } + var original = {}; for (var key in params) { @@ -1159,8 +1175,6 @@ function loadPartCategoryTable(table, options) { setupFilterList(filterKey, table, filterListElement); - var tree_view = options.allowTreeView && inventreeLoad('category-tree-view') == 1; - table.inventreeTable({ treeEnable: tree_view, rootParentId: tree_view ? options.params.parent : null, @@ -1258,10 +1272,17 @@ function loadPartCategoryTable(table, options) { switchable: true, sortable: true, formatter: function(value, row) { - return renderLink( + + var html = renderLink( value, `/part/category/${row.pk}/` ); + + if (row.starred) { + html += makeIconBadge('fa-bell icon-green', '{% trans "Subscribed category" %}'); + } + + return html; } }, { diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index 261cb4d1d6..a62e049651 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -427,7 +427,7 @@ function adjustStock(action, items, options={}) { break; default: $(opts.modal).modal('hide'); - showApiError(xhr); + showApiError(xhr, opts.url); break; } } @@ -1426,6 +1426,12 @@ function loadStockLocationTable(table, options) { var filterListElement = options.filterList || '#filter-list-location'; + var tree_view = options.allowTreeView && inventreeLoad('location-tree-view') == 1; + + if (tree_view) { + params.cascade = true; + } + var filters = {}; var filterKey = options.filterKey || options.name || 'location'; @@ -1446,8 +1452,6 @@ function loadStockLocationTable(table, options) { filters[key] = params[key]; } - var tree_view = options.allowTreeView && inventreeLoad('location-tree-view') == 1; - table.inventreeTable({ treeEnable: tree_view, rootParentId: tree_view ? options.params.parent : null, diff --git a/InvenTree/templates/js/translated/table_filters.js b/InvenTree/templates/js/translated/table_filters.js index 4d12f69780..537adefee9 100644 --- a/InvenTree/templates/js/translated/table_filters.js +++ b/InvenTree/templates/js/translated/table_filters.js @@ -103,6 +103,10 @@ function getAvailableTableFilters(tableKey) { title: '{% trans "Include subcategories" %}', description: '{% trans "Include subcategories" %}', }, + starred: { + type: 'bool', + title: '{% trans "Subscribed" %}', + }, }; } @@ -368,7 +372,7 @@ function getAvailableTableFilters(tableKey) { }, starred: { type: 'bool', - title: '{% trans "Starred" %}', + title: '{% trans "Subscribed" %}', }, salable: { type: 'bool', diff --git a/InvenTree/templates/notification.html b/InvenTree/templates/notification.html deleted file mode 100644 index 9919b0d6f5..0000000000 --- a/InvenTree/templates/notification.html +++ /dev/null @@ -1,18 +0,0 @@ -
    -
    - × -
    Success alert
    -
    -
    - × -
    Info alert
    -
    -
    - × -
    Warning alert
    -
    -
    - × -
    Danger alert
    -
    -
    \ No newline at end of file diff --git a/InvenTree/users/models.py b/InvenTree/users/models.py index 6c03e87733..4599408163 100644 --- a/InvenTree/users/models.py +++ b/InvenTree/users/models.py @@ -77,6 +77,7 @@ class RuleSet(models.Model): 'part_category': [ 'part_partcategory', 'part_partcategoryparametertemplate', + 'part_partcategorystar', ], 'part': [ 'part_part', @@ -90,6 +91,7 @@ class RuleSet(models.Model): 'part_partparameter', 'part_partrelated', 'part_partstar', + 'part_partcategorystar', 'company_supplierpart', 'company_manufacturerpart', 'company_manufacturerpartparameter', @@ -151,6 +153,7 @@ class RuleSet(models.Model): 'common_inventreeusersetting', 'common_webhookendpoint', 'common_webhookmessage', + 'common_notificationentry', 'company_contact', 'users_owner', diff --git a/tasks.py b/tasks.py index e80ee929fb..57b0e41ad9 100644 --- a/tasks.py +++ b/tasks.py @@ -286,6 +286,7 @@ def content_excludes(): "users.owner", "exchange.rate", "exchange.exchangebackend", + "common.notificationentry", ] output = ""
    {% trans "Part" %}{% trans "Required Quantity" %}{% trans "Available" %}
    + {{ line.part.full_name }}{% if part.description %} - {{ part.description }}{% endif %} + + {% decimal line.required %} {% if line.part.units %}{{ line.part.units }}{% endif %} + {% decimal line.available %} {% if line.part.units %}{{ line.part.units }}{% endif %}
    {% trans "Part Name" %}{% trans "Available Quantity" %}{% trans "Part" %}{% trans "Total Stock" %}{% trans "Available" %} {% trans "Minimum Quantity" %}
    {{ part.full_name }}{{ part.total_stock }}{{ part.minimum_stock }}{% decimal part.total_stock %}{% decimal part.available_stock %}{% decimal part.minimum_stock %}