From bb72658e767be44b4db6359ad27151754e2c2632 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 7 Jan 2021 00:18:18 +1100 Subject: [PATCH] Rearrange button options for StockItem --- InvenTree/report/models.py | 4 +-- InvenTree/stock/forms.py | 2 +- InvenTree/stock/models.py | 36 +++++++++++++++++++ .../stock/templates/stock/item_base.html | 24 +++++++++---- 4 files changed, 56 insertions(+), 10 deletions(-) diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index b3725cadc0..4dd5fbfa5c 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -16,7 +16,7 @@ from django.conf import settings from django.core.validators import FileExtensionValidator from django.core.exceptions import ValidationError -from stock.models import StockItem +import stock.models from InvenTree.helpers import validateFilterString @@ -191,7 +191,7 @@ class TestReport(ReportTemplateBase): filters = validateFilterString(self.filters) - items = StockItem.objects.filter(**filters) + items = stock.models.StockItem.objects.filter(**filters) # Ensure the provided StockItem object matches the filters items = items.filter(pk=item.pk) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 5583411009..ec7cbf7805 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -247,7 +247,7 @@ class TestReportFormatForm(HelperForm): templates = TestReport.objects.filter(enabled=True) for template in templates: - if template.matches_stock_item(self.stock_item): + if template.enabled and template.matches_stock_item(self.stock_item): choices.append((template.pk, template)) return choices diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index d399c0daf1..807d6644ca 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -31,6 +31,7 @@ from datetime import datetime, timedelta from InvenTree import helpers import common.models +import report.models from InvenTree.status_codes import StockStatus from InvenTree.models import InvenTreeTree, InvenTreeAttachment @@ -1306,6 +1307,41 @@ class StockItem(MPTTModel): return status['passed'] >= status['total'] + def available_test_reports(self): + """ + Return a list of TestReport objects which match this StockItem. + """ + + reports = [] + + item_query = StockItem.objects.filter(pk=self.pk) + + for test_report in report.models.TestReport.objects.filter(enabled=True): + + filters = helpers.validateFilterString(test_report.filters) + + if item_query.filter(**filters).exists(): + reports.append(test_report) + + return reports + + @property + def has_test_reports(self): + """ + Return True if there are test reports available for this stock item + """ + + return len(self.available_test_reports()) > 0 + + @property + def has_labels(self): + """ + Return True if there are any label templates available for this stock item + """ + + # TODO - Implement this + return True + @receiver(pre_delete, sender=StockItem, dispatch_uid='stock_item_pre_delete_log') def before_delete_stock_item(sender, instance, using, **kwargs): diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index cbb6eeafb2..bea7057351 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -108,16 +108,29 @@ InvenTree | {% trans "Stock Item" %} - {{ item }} + + + {% if item.has_labels or item.has_test_reports %} +
+ +
+ {% endif %} {% if roles.stock.change and not item.is_building %}
@@ -168,9 +181,6 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
{% endif %} - {% endblock %}