diff --git a/InvenTree/InvenTree/models.py b/InvenTree/InvenTree/models.py index 4e7ed89e63..52c5384b63 100644 --- a/InvenTree/InvenTree/models.py +++ b/InvenTree/InvenTree/models.py @@ -107,7 +107,10 @@ class InvenTreeAttachment(models.Model): return "attachments" def __str__(self): - return os.path.basename(self.attachment.name) + if self.attachment is not None: + return os.path.basename(self.attachment.name) + else: + return str(self.link) attachment = models.FileField(upload_to=rename_attachment, verbose_name=_('Attachment'), help_text=_('Select file to attach'), @@ -134,7 +137,10 @@ class InvenTreeAttachment(models.Model): @property def basename(self): - return os.path.basename(self.attachment.name) + if self.attachment: + return os.path.basename(self.attachment.name) + else: + return None @basename.setter def basename(self, fn): diff --git a/InvenTree/InvenTree/serializers.py b/InvenTree/InvenTree/serializers.py index 90cc857cdd..c236bad603 100644 --- a/InvenTree/InvenTree/serializers.py +++ b/InvenTree/InvenTree/serializers.py @@ -239,22 +239,6 @@ class InvenTreeModelSerializer(serializers.ModelSerializer): return data -class InvenTreeAttachmentSerializer(InvenTreeModelSerializer): - """ - Special case of an InvenTreeModelSerializer, which handles an "attachment" model. - - The only real addition here is that we support "renaming" of the attachment file. - """ - - # The 'filename' field must be present in the serializer - filename = serializers.CharField( - label=_('Filename'), - required=False, - source='basename', - allow_blank=False, - ) - - class InvenTreeAttachmentSerializerField(serializers.FileField): """ Override the DRF native FileField serializer, @@ -284,6 +268,46 @@ class InvenTreeAttachmentSerializerField(serializers.FileField): return os.path.join(str(settings.MEDIA_URL), str(value)) +class InvenTreeAttachmentSerializer(InvenTreeModelSerializer): + """ + Special case of an InvenTreeModelSerializer, which handles an "attachment" model. + + The only real addition here is that we support "renaming" of the attachment file. + """ + + def validate(self, data): + """ + Validation for an attachment. Either a file or external link must be provided + """ + + data = super().validate(data) + + attachment = data.get('attachment', None) + link = data.get('link', None) + + if not attachment and not link: + raise ValidationError({ + 'attachment': _('Missing file'), + 'link': _('Missing external link'), + }) + + return data + + attachment = InvenTreeAttachmentSerializerField( + required=False, + allow_null=False, + ) + + # The 'filename' field must be present in the serializer + filename = serializers.CharField( + label=_('Filename'), + required=False, + source='basename', + allow_blank=False, + ) + + + class InvenTreeImageSerializerField(serializers.ImageField): """ Custom image serializer. diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 12f57f980a..338e4a38b3 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -516,8 +516,6 @@ class BuildAttachmentSerializer(InvenTreeAttachmentSerializer): Serializer for a BuildAttachment """ - attachment = InvenTreeAttachmentSerializerField(required=True) - class Meta: model = BuildOrderAttachment diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index a3674afb59..c1356d78c9 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -377,8 +377,6 @@ class POAttachmentSerializer(InvenTreeAttachmentSerializer): Serializers for the PurchaseOrderAttachment model """ - attachment = InvenTreeAttachmentSerializerField(required=True) - class Meta: model = PurchaseOrderAttachment @@ -598,8 +596,6 @@ class SOAttachmentSerializer(InvenTreeAttachmentSerializer): Serializers for the SalesOrderAttachment model """ - attachment = InvenTreeAttachmentSerializerField(required=True) - class Meta: model = SalesOrderAttachment diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 189665fb32..1be81c16ba 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -75,8 +75,6 @@ class PartAttachmentSerializer(InvenTreeAttachmentSerializer): Serializer for the PartAttachment class """ - attachment = InvenTreeAttachmentSerializerField(required=True) - class Meta: model = PartAttachment diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 39ebd13acb..c74d674275 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -420,8 +420,6 @@ class StockItemAttachmentSerializer(InvenTree.serializers.InvenTreeAttachmentSer user_detail = InvenTree.serializers.UserSerializerBrief(source='user', read_only=True) - attachment = InvenTree.serializers.InvenTreeAttachmentSerializerField(required=True) - # TODO: Record the uploading user when creating or updating an attachment! class Meta: