mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Move validation to the model class
This commit is contained in:
parent
4ee55847f1
commit
78309c1915
@ -98,6 +98,7 @@ class InvenTreeAttachment(models.Model):
|
|||||||
user: User associated with file upload
|
user: User associated with file upload
|
||||||
upload_date: Date the file was uploaded
|
upload_date: Date the file was uploaded
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def getSubdir(self):
|
def getSubdir(self):
|
||||||
"""
|
"""
|
||||||
Return the subdirectory under which attachments should be stored.
|
Return the subdirectory under which attachments should be stored.
|
||||||
@ -106,6 +107,16 @@ class InvenTreeAttachment(models.Model):
|
|||||||
|
|
||||||
return "attachments"
|
return "attachments"
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
# Either 'attachment' or 'link' must be specified!
|
||||||
|
if not self.attachment and not self.link:
|
||||||
|
raise ValidationError({
|
||||||
|
'attachment': _('Missing file'),
|
||||||
|
'link': _('Missing external link'),
|
||||||
|
})
|
||||||
|
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if self.attachment is not None:
|
if self.attachment is not None:
|
||||||
return os.path.basename(self.attachment.name)
|
return os.path.basename(self.attachment.name)
|
||||||
|
@ -275,24 +275,6 @@ class InvenTreeAttachmentSerializer(InvenTreeModelSerializer):
|
|||||||
The only real addition here is that we support "renaming" of the attachment file.
|
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(
|
attachment = InvenTreeAttachmentSerializerField(
|
||||||
required=False,
|
required=False,
|
||||||
allow_null=False,
|
allow_null=False,
|
||||||
|
@ -88,6 +88,12 @@ function loadAttachmentTable(url, options) {
|
|||||||
link: {},
|
link: {},
|
||||||
comment: {},
|
comment: {},
|
||||||
},
|
},
|
||||||
|
processResults: function(data, fields, opts) {
|
||||||
|
// Remove the "link" field if the attachment is a file!
|
||||||
|
if (data.attachment) {
|
||||||
|
delete opts.fields.link;
|
||||||
|
}
|
||||||
|
},
|
||||||
onSuccess: reloadAttachmentTable,
|
onSuccess: reloadAttachmentTable,
|
||||||
title: '{% trans "Edit Attachment" %}',
|
title: '{% trans "Edit Attachment" %}',
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user