Attachment URL method (#5584)

* Attachment fully-qualified-url

Adds method to generate a fully-qualified URL for an attachment

* Construct absolute URL including host information
This commit is contained in:
Oliver 2023-09-21 13:55:14 +10:00 committed by GitHub
parent 0f8cddd23d
commit 6cde460567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -440,7 +440,8 @@ class InvenTreeAttachment(models.Model):
An attachment can be either an uploaded file, or an external URL
Attributes:
attachment: File
attachment: Upload file
link: External URL
comment: String descriptor for the attachment
user: User associated with file upload
upload_date: Date the file was uploaded
@ -564,6 +565,22 @@ class InvenTreeAttachment(models.Model):
except Exception:
raise ValidationError(_("Error renaming file"))
def fully_qualified_url(self):
"""Return a 'fully qualified' URL for this attachment.
- If the attachment is a link to an external resource, return the link
- If the attachment is an uploaded file, return the fully qualified media URL
"""
if self.link:
return self.link
if self.attachment:
media_url = InvenTree.helpers.getMediaUrl(self.attachment.url)
return InvenTree.helpers_model.construct_absolute_url(media_url)
return ''
class InvenTreeTree(MPTTModel):
"""Provides an abstracted self-referencing tree model for data categories.