Clean up implementation of test report matching

This commit is contained in:
Oliver Walters 2020-08-23 21:03:29 +10:00
parent b7ae95686e
commit c849f618d5
5 changed files with 21 additions and 27 deletions

View File

@ -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})

View File

@ -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,

View File

@ -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

View File

@ -124,11 +124,9 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
{% endif %}
</ul>
</div>
{% if item.part.has_test_report_templates %}
<button type='button' class='btn btn-default' id='stock-test-report' title='{% trans "Generate test report" %}'>
<span class='fas fa-file-invoice'/>
</button>
{% endif %}
</div>
{% 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(

View File

@ -17,9 +17,7 @@
<button type='button' class='btn btn-danger' id='delete-test-results'>{% trans "Delete Test Data" %}</button>
{% endif %}
<button type='button' class='btn btn-success' id='add-test-result'>{% trans "Add Test Data" %}</button>
{% if item.part.has_test_report_templates %}
<button type='button' class='btn btn-default' id='test-report'>{% trans "Test Report" %} <span class='fas fa-tasks'></span></button>
{% endif %}
</div>
<div class='filter-list' id='filter-list-stocktests'>
<!-- Empty div -->