Added arbitrary files and links to boxes
This commit is contained in:
@@ -110,3 +110,50 @@ class Thing(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
def thing_file_upload_path(instance, filename):
|
||||
"""Generate a custom path for thing files in format: things/files/<thing_id>/<filename>"""
|
||||
return f'things/files/{instance.thing.id}/{filename}'
|
||||
|
||||
|
||||
class ThingFile(models.Model):
|
||||
"""A file attachment for a Thing."""
|
||||
|
||||
thing = models.ForeignKey(
|
||||
Thing,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='files'
|
||||
)
|
||||
file = models.FileField(upload_to=thing_file_upload_path)
|
||||
title = models.CharField(max_length=255, help_text='Descriptive name for the file')
|
||||
uploaded_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ['-uploaded_at']
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.thing.name} - {self.title}'
|
||||
|
||||
def filename(self):
|
||||
"""Return the original filename."""
|
||||
return os.path.basename(self.file.name)
|
||||
|
||||
|
||||
class ThingLink(models.Model):
|
||||
"""A hyperlink for a Thing."""
|
||||
|
||||
thing = models.ForeignKey(
|
||||
Thing,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='links'
|
||||
)
|
||||
url = models.URLField(max_length=2048)
|
||||
title = models.CharField(max_length=255, help_text='Descriptive title for the link')
|
||||
uploaded_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ['-uploaded_at']
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.thing.name} - {self.title}'
|
||||
|
||||
Reference in New Issue
Block a user