Add form for selecting label template for a StockItem

This commit is contained in:
Oliver Walters 2020-08-16 12:10:58 +10:00
parent 30e24f19d9
commit 1807ba4e7b
5 changed files with 95 additions and 0 deletions

View File

@ -43,6 +43,12 @@ class LabelTemplate(models.Model):
def template(self):
return self.label.path
def __str__(self):
return "{n} - {d}".format(
n=self.name,
d=self.description
)
name = models.CharField(
unique=True,
blank=False, max_length=100,
@ -137,6 +143,7 @@ class StockItemLabel(LabelTemplate):
'uid': item.uid,
'pk': item.pk,
'qr_data': item.format_short_barcode(),
'tests': item.testResultMap()
})
return records

View File

@ -178,6 +178,37 @@ class SerializeStockForm(HelperForm):
]
class StockItemLabelSelectForm(HelperForm):
""" Form for selecting a label template for a StockItem """
label = forms.ChoiceField(
label=_('Label'),
help_text=_('Select test report template')
)
class Meta:
model = StockItem
fields = [
'label',
]
def get_label_choices(self, labels):
choices = []
if len(labels) > 0:
for label in labels:
choices.append((label.pk, label))
return choices
def __init__(self, labels, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['label'].choices = self.get_label_choices(labels)
class TestReportFormatForm(HelperForm):
""" Form for selection a test report template """

View File

@ -129,6 +129,9 @@ InvenTree | {% trans "Stock Item" %} - {{ item }}
<span class='fas fa-tasks'/>
</button>
{% endif %}
<button type='button' class='btn btn-default' id='stock-print-label' title='{% trans "Print labels" %}'>
<span class='fas fa-tag'></span>
</button>
</div>
{% endblock %}
@ -314,6 +317,15 @@ $("#stock-test-report").click(function() {
});
{% endif %}
$("#stock-print-label").click(function() {
launchModalForm(
"{% url 'stock-item-label-select' item.id %}",
{
follow: true,
}
)
});
$("#stock-duplicate").click(function() {
launchModalForm(
"{% url 'stock-item-create' %}",

View File

@ -29,6 +29,7 @@ stock_item_detail_urls = [
url(r'^add_tracking/', views.StockItemTrackingCreate.as_view(), name='stock-tracking-create'),
url(r'^test-report-select/', views.StockItemTestReportSelect.as_view(), name='stock-item-test-report-select'),
url(r'^label-select/', views.StockItemSelectLabels.as_view(), name='stock-item-label-select'),
url(r'^test/', views.StockItemDetail.as_view(template_name='stock/item_tests.html'), name='stock-item-test-results'),
url(r'^children/', views.StockItemDetail.as_view(template_name='stock/item_childs.html'), name='stock-item-children'),

View File

@ -296,6 +296,50 @@ class StockItemReturnToStock(AjaxUpdateView):
return self.renderJsonResponse(request, self.get_form(), data)
class StockItemSelectLabels(AjaxView):
"""
View for selecting a template for printing labels for one (or more) StockItem objects
"""
model = StockItem
ajax_form_title = _('Select Label Template')
def get_form(self):
item = StockItem.objects.get(pk=self.kwargs['pk'])
labels = []
for label in StockItemLabel.objects.all():
if label.matches_stock_item(item):
labels.append(label)
return StockForms.StockItemLabelSelectForm(labels)
def post(self, request, *args, **kwargs):
label = request.POST.get('label', None)
try:
label = StockItemLabel.objects.get(pk=label)
except (ValueError, StockItemLabel.DoesNotExist):
raise ValidationError({'label': _("Select valid label")})
stock_item = StockItem.objects.get(pk=self.kwargs['pk'])
url = reverse('stock-item-print-labels')
url += '?label={pk}'.format(pk=label.pk)
url += '&items[]={pk}'.format(pk=stock_item.pk)
data = {
'form_valid': True,
'url': url,
}
return self.renderJsonResponse(request, self.get_form(), data=data)
class StockItemPrintLabels(AjaxView):
"""
View for printing labels and returning a PDF