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.category = category
self.save() 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): def get_absolute_url(self):
""" Return the web URL for viewing this part """ """ Return the web URL for viewing this part """
return reverse('part-detail', kwargs={'pk': self.id}) 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 # Requires a stock_item object to be given to it before rendering
stock_item = None 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): def get_context_data(self, request):
return { return {
'stock_item': self.stock_item, 'stock_item': self.stock_item,

View File

@ -15,6 +15,8 @@ from InvenTree.helpers import GetExportFormats
from InvenTree.forms import HelperForm from InvenTree.forms import HelperForm
from InvenTree.fields import RoundingDecimalFormField from InvenTree.fields import RoundingDecimalFormField
from report.models import TestReport
from .models import StockLocation, StockItem, StockItemTracking from .models import StockLocation, StockItem, StockItemTracking
from .models import StockItemAttachment from .models import StockItemAttachment
from .models import StockItemTestResult from .models import StockItemTestResult
@ -225,12 +227,17 @@ class TestReportFormatForm(HelperForm):
self.fields['template'].choices = self.get_template_choices() self.fields['template'].choices = self.get_template_choices()
def get_template_choices(self): def get_template_choices(self):
""" Available choices """ """
Generate a list of of TestReport options for the StockItem
"""
choices = [] choices = []
for report in self.stock_item.part.get_test_report_templates(): templates = TestReport.objects.filter(enabled=True)
choices.append((report.pk, report))
for template in templates:
if template.matches_stock_item(self.stock_item):
choices.append(template)
return choices return choices

View File

@ -124,11 +124,9 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
{% endif %} {% endif %}
</ul> </ul>
</div> </div>
{% if item.part.has_test_report_templates %}
<button type='button' class='btn btn-default' id='stock-test-report' title='{% trans "Generate test report" %}'> <button type='button' class='btn btn-default' id='stock-test-report' title='{% trans "Generate test report" %}'>
<span class='fas fa-file-invoice'/> <span class='fas fa-file-invoice'/>
</button> </button>
{% endif %}
</div> </div>
{% endblock %} {% endblock %}
@ -303,7 +301,6 @@ $("#stock-serialize").click(function() {
); );
}); });
{% if item.part.has_test_report_templates %}
$("#stock-test-report").click(function() { $("#stock-test-report").click(function() {
launchModalForm( launchModalForm(
"{% url 'stock-item-test-report-select' item.id %}", "{% url 'stock-item-test-report-select' item.id %}",
@ -312,7 +309,6 @@ $("#stock-test-report").click(function() {
} }
); );
}); });
{% endif %}
$("#print-label").click(function() { $("#print-label").click(function() {
launchModalForm( launchModalForm(

View File

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