diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index dd126d5730..29a4f85c7c 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -399,24 +399,6 @@ class Part(MPTTModel): self.category = category self.save() - def get_test_report_templates(self): - """ - Return all the TestReport template objects which map to this Part. - """ - - templates = [] - - for report in ReportModels.TestReport.objects.all(): - if report.matches_part(self): - templates.append(report) - - return templates - - def has_test_report_templates(self): - """ Return True if this part has a TestReport defined """ - - return len(self.get_test_report_templates()) > 0 - def get_absolute_url(self): """ Return the web URL for viewing this part """ return reverse('part-detail', kwargs={'pk': self.id}) diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index 47d3b053ac..100ea87c75 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -202,6 +202,17 @@ class TestReport(ReportTemplateBase, PartFilterMixin): # Requires a stock_item object to be given to it before rendering stock_item = None + def matches_stock_item(self, item): + """ + Test if this report template matches a given StockItem objects + """ + + filters = validateFilterString(self.part_filters) + + items = StockItem.objects.filter(**filters) + + return items.exists() + def get_context_data(self, request): return { 'stock_item': self.stock_item, diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 234da6d53b..97d028ec2b 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -15,6 +15,8 @@ from InvenTree.helpers import GetExportFormats from InvenTree.forms import HelperForm from InvenTree.fields import RoundingDecimalFormField +from report.models import TestReport + from .models import StockLocation, StockItem, StockItemTracking from .models import StockItemAttachment from .models import StockItemTestResult @@ -225,12 +227,17 @@ class TestReportFormatForm(HelperForm): self.fields['template'].choices = self.get_template_choices() def get_template_choices(self): - """ Available choices """ + """ + Generate a list of of TestReport options for the StockItem + """ choices = [] - for report in self.stock_item.part.get_test_report_templates(): - choices.append((report.pk, report)) + templates = TestReport.objects.filter(enabled=True) + + for template in templates: + if template.matches_stock_item(self.stock_item): + choices.append(template) return choices diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 1b4171297e..bcc2087b25 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -124,11 +124,9 @@ InvenTree | {% trans "Stock Item" %} - {{ item }} {% endif %} - {% if item.part.has_test_report_templates %} - {% endif %} {% endblock %} @@ -303,7 +301,6 @@ $("#stock-serialize").click(function() { ); }); -{% if item.part.has_test_report_templates %} $("#stock-test-report").click(function() { launchModalForm( "{% url 'stock-item-test-report-select' item.id %}", @@ -312,7 +309,6 @@ $("#stock-test-report").click(function() { } ); }); -{% endif %} $("#print-label").click(function() { launchModalForm( diff --git a/InvenTree/stock/templates/stock/item_tests.html b/InvenTree/stock/templates/stock/item_tests.html index c79068349d..10bb0950e4 100644 --- a/InvenTree/stock/templates/stock/item_tests.html +++ b/InvenTree/stock/templates/stock/item_tests.html @@ -17,9 +17,7 @@ {% endif %} - {% if item.part.has_test_report_templates %} - {% endif %}