diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 9972b1c982..9abbd8cda2 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -693,11 +693,6 @@ class StockItemTestResultList(generics.ListCreateAPIView): except: pass - try: - kwargs['attachment_detail'] = str2bool(self.request.query_params.get('attachment_detail', False)) - except: - pass - kwargs['context'] = self.get_serializer_context() return self.serializer_class(*args, **kwargs) @@ -713,23 +708,6 @@ class StockItemTestResultList(generics.ListCreateAPIView): # Capture the user information test_result = serializer.save() test_result.user = self.request.user - - # Check if a file has been attached to the request - attachment_file = self.request.FILES.get('attachment', None) - - if attachment_file: - # Create a new attachment associated with the stock item - attachment = StockItemAttachment( - attachment=attachment_file, - stock_item=test_result.stock_item, - user=test_result.user - ) - - attachment.save() - - # Link the attachment back to the test result - test_result.attachment = attachment - test_result.save() diff --git a/InvenTree/stock/migrations/0042_auto_20200523_0121.py b/InvenTree/stock/migrations/0042_auto_20200523_0121.py new file mode 100644 index 0000000000..66db1441e3 --- /dev/null +++ b/InvenTree/stock/migrations/0042_auto_20200523_0121.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.5 on 2020-05-23 01:21 + +from django.db import migrations, models +import stock.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stock', '0041_stockitemtestresult_notes'), + ] + + operations = [ + migrations.AlterField( + model_name='stockitemtestresult', + name='attachment', + field=models.FileField(blank=True, help_text='Test result attachment', null=True, upload_to=stock.models.rename_stock_item_test_result_attachment, verbose_name='Attachment'), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 468f72df37..9ed2a55d4a 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -1094,6 +1094,11 @@ class StockItemTracking(models.Model): # file = models.FileField() +def rename_stock_item_test_result_attachment(instance, filename): + + return os.path.join('stock_files', str(instance.stock_item.pk), os.path.basename(filename)) + + class StockItemTestResult(models.Model): """ A StockItemTestResult records results of custom tests against individual StockItem objects. @@ -1123,13 +1128,11 @@ class StockItemTestResult(models.Model): super().clean() - """ # If this test result corresponds to a template, check the requirements of the template key = helpers.generateTestKey(self.test) templates = self.stock_item.part.getTestTemplates() - TODO: Re-introduce this at a later stage, it is buggy when uplaoding an attachment via the API for template in templates: if key == template.key: @@ -1146,17 +1149,6 @@ class StockItemTestResult(models.Model): }) break - """ - - # If an attachment is linked to this result, the attachment must also point to the item - try: - if self.attachment: - if not self.attachment.stock_item == self.stock_item: - raise ValidationError({ - 'attachment': _("Test result attachment must be linked to the same StockItem"), - }) - except (StockItem.DoesNotExist, StockItemAttachment.DoesNotExist): - pass stock_item = models.ForeignKey( StockItem, @@ -1182,10 +1174,9 @@ class StockItemTestResult(models.Model): help_text=_('Test output value') ) - attachment = models.ForeignKey( - StockItemAttachment, - on_delete=models.SET_NULL, - blank=True, null=True, + attachment = models.FileField( + null=True, blank=True, + upload_to=rename_stock_item_test_result_attachment, verbose_name=_('Attachment'), help_text=_('Test result attachment'), ) diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index a84ea92540..d0d7162370 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -229,20 +229,15 @@ class StockItemTestResultSerializer(InvenTreeModelSerializer): """ Serializer for the StockItemTestResult model """ user_detail = UserSerializerBrief(source='user', read_only=True) - attachment_detail = StockItemAttachmentSerializer(source='attachment', read_only=True) def __init__(self, *args, **kwargs): user_detail = kwargs.pop('user_detail', False) - attachment_detail = kwargs.pop('attachment_detail', False) super().__init__(*args, **kwargs) if user_detail is not True: self.fields.pop('user_detail') - if attachment_detail is not True: - self.fields.pop('attachment_detail') - class Meta: model = StockItemTestResult @@ -253,7 +248,6 @@ class StockItemTestResultSerializer(InvenTreeModelSerializer): 'result', 'value', 'attachment', - 'attachment_detail', 'notes', 'user', 'user_detail', diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index d0a57d3d1f..02a7ee9719 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -292,17 +292,6 @@ 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 @@ -320,8 +309,6 @@ 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 diff --git a/InvenTree/templates/js/stock.html b/InvenTree/templates/js/stock.html index 49c3151ff0..68f1a0c5c5 100644 --- a/InvenTree/templates/js/stock.html +++ b/InvenTree/templates/js/stock.html @@ -56,8 +56,8 @@ function loadStockTestResultsTable(table, options) { html += `${row.user_detail.username}`; } - if (row.attachment_detail) { - html += ``; + if (row.attachment) { + html += ``; } return html;