From cd0e66e3c6a010751bd343b950b0a4dcdd0c93e7 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 17 May 2020 16:17:05 +1000 Subject: [PATCH] Add ability to edit / assign attatched files to test result data --- InvenTree/InvenTree/models.py | 3 +++ InvenTree/stock/forms.py | 1 + InvenTree/stock/views.py | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/InvenTree/InvenTree/models.py b/InvenTree/InvenTree/models.py index 5ec2e2945d..d0ed73f27a 100644 --- a/InvenTree/InvenTree/models.py +++ b/InvenTree/InvenTree/models.py @@ -53,6 +53,9 @@ class InvenTreeAttachment(models.Model): return "attachments" + def __str__(self): + return os.path.basename(self.attachment.name) + attachment = models.FileField(upload_to=rename_attachment, help_text=_('Select file to attach')) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 0d543b1315..9576447997 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -46,6 +46,7 @@ class EditStockItemTestResultForm(HelperForm): 'test', 'result', 'value', + 'attachment', 'notes', ] diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 39bc7138b1..11c2065c35 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -261,6 +261,17 @@ class StockItemTestResultCreate(AjaxCreateView): form = super().get_form() form.fields['stock_item'].widget = HiddenInput() + # Extract the StockItem object + item_id = form['stock_item'].value() + + # Limit the options for the file attachments + try: + stock_item = StockItem.objects.get(pk=item_id) + form.fields['attachment'].queryset = stock_item.attachments.all() + except (ValueError, StockItem.DoesNotExist): + # Hide the attachments field + form.fields['attachment'].widget = HiddenInput() + return form @@ -278,6 +289,8 @@ class StockItemTestResultEdit(AjaxUpdateView): form = super().get_form() form.fields['stock_item'].widget = HiddenInput() + + form.fields['attachment'].queryset = self.object.stock_item.attachments.all() return form