Gallery-Archivist/archivist/apps/files/models.py

102 lines
3.5 KiB
Python
Raw Normal View History

2023-12-28 17:38:25 +01:00
from django.db import models
2024-01-02 21:11:20 +01:00
from django.urls import reverse
2023-12-28 17:38:25 +01:00
from django.utils.translation import gettext_lazy as _
# Create your models here.
2023-12-29 22:45:15 +01:00
def get_upload_to(instance, filename, folder):
return f'{folder}/{instance.file_hash[:2]}/{filename}'
2024-01-02 21:11:20 +01:00
def get_upload_to_metadata(instance, filename):
return get_upload_to(instance, filename, 'metadata')
2023-12-29 22:45:15 +01:00
def get_upload_to_submission(instance, filename):
return get_upload_to(instance, filename, 'submissions')
def get_upload_to_profile(instance, filename):
return get_upload_to(instance, filename, 'profiles')
def get_upload_to_banner(instance, filename):
return get_upload_to(instance, filename, 'banners')
class User_Profile_Images(models.Model):
2024-03-14 22:41:30 +01:00
file_hash = models.CharField(unique=True, max_length=64)
2023-12-29 22:45:15 +01:00
file_name = models.CharField(max_length=150, blank=True)
file = models.FileField(upload_to=get_upload_to_profile, blank=True)
2024-03-14 22:41:30 +01:00
file_mime = models.CharField(max_length=64, blank=True)
file_ext = models.CharField(max_length=64, blank=True)
2023-12-29 22:45:15 +01:00
date_added = models.DateTimeField(auto_now_add=True, editable=True)
class Meta:
verbose_name = _("User Profile Image")
verbose_name_plural = _("User Profile Images")
def __str__(self):
2024-01-02 21:11:20 +01:00
return self.file_name
2023-12-29 22:45:15 +01:00
2024-01-02 21:11:20 +01:00
def get_absolute_url(self):
2024-03-14 19:48:35 +01:00
return reverse('files:serve_content_file', args=['user_profile', self.file_hash])
2023-12-29 22:45:15 +01:00
class User_Banner_Images(models.Model):
2024-03-14 22:41:30 +01:00
file_hash = models.CharField(unique=True, max_length=64)
2023-12-29 22:45:15 +01:00
file_name = models.CharField(max_length=150, blank=True)
file = models.FileField(upload_to=get_upload_to_banner, blank=True)
2024-03-14 22:41:30 +01:00
file_mime = models.CharField(max_length=64, blank=True)
file_ext = models.CharField(max_length=64, blank=True)
2023-12-29 22:45:15 +01:00
date_added = models.DateTimeField(auto_now_add=True, editable=True)
class Meta:
verbose_name = _("User Banner Image")
verbose_name_plural = _("User Banner Images")
def __str__(self):
2024-01-02 21:11:20 +01:00
return self.file_name
def get_absolute_url(self):
2024-03-14 19:48:35 +01:00
return reverse('files:serve_content_file', args=['user_banner', self.file_hash])
2023-12-29 22:45:15 +01:00
2023-12-28 17:38:25 +01:00
class Submission_File(models.Model):
2024-03-14 22:41:30 +01:00
file_hash = models.CharField(unique=True, max_length=64)
2023-12-28 17:38:25 +01:00
file_name = models.CharField(max_length=150, blank=True)
2023-12-29 22:45:15 +01:00
file = models.FileField(upload_to=get_upload_to_submission, blank=True)
2024-03-14 22:41:30 +01:00
file_mime = models.CharField(max_length=64, blank=True)
file_ext = models.CharField(max_length=64, blank=True)
2023-12-28 17:38:25 +01:00
date_added = models.DateTimeField(auto_now_add=True, editable=True)
class Meta:
verbose_name = _("Submission File")
verbose_name_plural = _("Submission Files")
def __str__(self):
2024-01-02 21:11:20 +01:00
return self.file_name
def get_absolute_url(self):
2024-03-14 19:48:35 +01:00
return reverse('files:serve_content_file', args=['submission', self.file_hash])
2024-01-02 21:11:20 +01:00
class Metadata_Files(models.Model):
2024-03-14 22:41:30 +01:00
file_hash = models.CharField(unique=True, max_length=64)
2024-01-02 21:11:20 +01:00
file_name = models.CharField(max_length=150, blank=True)
file = models.FileField(upload_to=get_upload_to_metadata, blank=True)
2024-03-14 22:41:30 +01:00
file_mime = models.CharField(max_length=64, blank=True)
file_ext = models.CharField(max_length=64, blank=True)
2024-01-02 21:11:20 +01:00
date_added = models.DateTimeField(auto_now_add=True, editable=True)
class Meta:
verbose_name = _("Metadata File")
verbose_name_plural = _("Metadata Files")
def __str__(self):
return self.file_name
def get_absolute_url(self):
2024-03-14 19:48:35 +01:00
return reverse('files:serve_content_file', args=['metadata', self.file_hash])