mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Improve / simplify logic for file attachments against test result object
This commit is contained in:
		| @@ -693,11 +693,6 @@ class StockItemTestResultList(generics.ListCreateAPIView): | |||||||
|         except: |         except: | ||||||
|             pass |             pass | ||||||
|  |  | ||||||
|         try: |  | ||||||
|             kwargs['attachment_detail'] = str2bool(self.request.query_params.get('attachment_detail', False)) |  | ||||||
|         except: |  | ||||||
|             pass |  | ||||||
|  |  | ||||||
|         kwargs['context'] = self.get_serializer_context() |         kwargs['context'] = self.get_serializer_context() | ||||||
|  |  | ||||||
|         return self.serializer_class(*args, **kwargs) |         return self.serializer_class(*args, **kwargs) | ||||||
| @@ -713,23 +708,6 @@ class StockItemTestResultList(generics.ListCreateAPIView): | |||||||
|         # Capture the user information |         # Capture the user information | ||||||
|         test_result = serializer.save() |         test_result = serializer.save() | ||||||
|         test_result.user = self.request.user |         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() |         test_result.save() | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								InvenTree/stock/migrations/0042_auto_20200523_0121.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								InvenTree/stock/migrations/0042_auto_20200523_0121.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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'), | ||||||
|  |         ), | ||||||
|  |     ] | ||||||
| @@ -1094,6 +1094,11 @@ class StockItemTracking(models.Model): | |||||||
|     # file = models.FileField() |     # 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): | class StockItemTestResult(models.Model): | ||||||
|     """ |     """ | ||||||
|     A StockItemTestResult records results of custom tests against individual StockItem objects. |     A StockItemTestResult records results of custom tests against individual StockItem objects. | ||||||
| @@ -1123,13 +1128,11 @@ class StockItemTestResult(models.Model): | |||||||
|  |  | ||||||
|         super().clean() |         super().clean() | ||||||
|  |  | ||||||
|         """ |  | ||||||
|         # If this test result corresponds to a template, check the requirements of the template |         # If this test result corresponds to a template, check the requirements of the template | ||||||
|         key = helpers.generateTestKey(self.test) |         key = helpers.generateTestKey(self.test) | ||||||
|  |  | ||||||
|         templates = self.stock_item.part.getTestTemplates() |         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: |         for template in templates: | ||||||
|             if key == template.key: |             if key == template.key: | ||||||
|                  |                  | ||||||
| @@ -1146,17 +1149,6 @@ class StockItemTestResult(models.Model): | |||||||
|                         }) |                         }) | ||||||
|  |  | ||||||
|                 break |                 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( |     stock_item = models.ForeignKey( | ||||||
|         StockItem, |         StockItem, | ||||||
| @@ -1182,10 +1174,9 @@ class StockItemTestResult(models.Model): | |||||||
|         help_text=_('Test output value') |         help_text=_('Test output value') | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     attachment = models.ForeignKey( |     attachment = models.FileField( | ||||||
|         StockItemAttachment, |         null=True, blank=True, | ||||||
|         on_delete=models.SET_NULL, |         upload_to=rename_stock_item_test_result_attachment, | ||||||
|         blank=True, null=True, |  | ||||||
|         verbose_name=_('Attachment'), |         verbose_name=_('Attachment'), | ||||||
|         help_text=_('Test result attachment'), |         help_text=_('Test result attachment'), | ||||||
|     ) |     ) | ||||||
|   | |||||||
| @@ -229,20 +229,15 @@ class StockItemTestResultSerializer(InvenTreeModelSerializer): | |||||||
|     """ Serializer for the StockItemTestResult model """ |     """ Serializer for the StockItemTestResult model """ | ||||||
|  |  | ||||||
|     user_detail = UserSerializerBrief(source='user', read_only=True) |     user_detail = UserSerializerBrief(source='user', read_only=True) | ||||||
|     attachment_detail = StockItemAttachmentSerializer(source='attachment', read_only=True) |  | ||||||
|  |  | ||||||
|     def __init__(self, *args, **kwargs): |     def __init__(self, *args, **kwargs): | ||||||
|         user_detail = kwargs.pop('user_detail', False) |         user_detail = kwargs.pop('user_detail', False) | ||||||
|         attachment_detail = kwargs.pop('attachment_detail', False) |  | ||||||
|  |  | ||||||
|         super().__init__(*args, **kwargs) |         super().__init__(*args, **kwargs) | ||||||
|  |  | ||||||
|         if user_detail is not True: |         if user_detail is not True: | ||||||
|             self.fields.pop('user_detail') |             self.fields.pop('user_detail') | ||||||
|  |  | ||||||
|         if attachment_detail is not True: |  | ||||||
|             self.fields.pop('attachment_detail') |  | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         model = StockItemTestResult |         model = StockItemTestResult | ||||||
|  |  | ||||||
| @@ -253,7 +248,6 @@ class StockItemTestResultSerializer(InvenTreeModelSerializer): | |||||||
|             'result', |             'result', | ||||||
|             'value', |             'value', | ||||||
|             'attachment', |             'attachment', | ||||||
|             'attachment_detail', |  | ||||||
|             'notes', |             'notes', | ||||||
|             'user', |             'user', | ||||||
|             'user_detail', |             'user_detail', | ||||||
|   | |||||||
| @@ -292,17 +292,6 @@ class StockItemTestResultCreate(AjaxCreateView): | |||||||
|         form = super().get_form() |         form = super().get_form() | ||||||
|         form.fields['stock_item'].widget = HiddenInput() |         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 |         return form | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -320,8 +309,6 @@ class StockItemTestResultEdit(AjaxUpdateView): | |||||||
|         form = super().get_form() |         form = super().get_form() | ||||||
|  |  | ||||||
|         form.fields['stock_item'].widget = HiddenInput() |         form.fields['stock_item'].widget = HiddenInput() | ||||||
|  |  | ||||||
|         form.fields['attachment'].queryset = self.object.stock_item.attachments.all() |  | ||||||
|          |          | ||||||
|         return form |         return form | ||||||
|  |  | ||||||
|   | |||||||
| @@ -56,8 +56,8 @@ function loadStockTestResultsTable(table, options) { | |||||||
|             html += `<span class='badge'>${row.user_detail.username}</span>`; |             html += `<span class='badge'>${row.user_detail.username}</span>`; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (row.attachment_detail) { |         if (row.attachment) { | ||||||
|             html += `<a href='${row.attachment_detail.attachment}'><span class='fas fa-file-alt label-right'></span></a>`; |             html += `<a href='${row.attachment}'><span class='fas fa-file-alt label-right'></span></a>`; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return html; |         return html; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user