Add redir model, remove children albums

This commit is contained in:
Nyymix 2025-01-20 21:21:09 +02:00
parent 684982eca5
commit 1dbd1c1779
9 changed files with 84 additions and 23 deletions

View file

@ -1,3 +1,4 @@
from .album import *
from .location import *
from .photo import *
from .redir import *

View file

@ -12,7 +12,6 @@ from gallery.models.location import Location
class Album(models.Model):
name = models.CharField(max_length=150, unique=True, verbose_name="Album")
slug = models.SlugField(max_length=150, unique=True, verbose_name="Slug")
parent = models.ForeignKey('self', on_delete=models.SET_NULL, blank=True, null=True, limit_choices_to={'parent__isnull': True}, related_name='children', verbose_name="Parent Album")
location = models.ForeignKey(Location, blank=True, null=True, on_delete=models.SET_NULL, related_name='albums', verbose_name="Location")
album_date = models.DateField(default=datetime.now, verbose_name="Album Date")
cover = models.ForeignKey("Photo", blank=True, null=True, on_delete=models.SET_NULL, related_name='cover_to', verbose_name="Album cover")
@ -21,12 +20,12 @@ class Album(models.Model):
@property
def photos_in_album(self):
return self.photos.count()
@property
def photos_views(self):
total_views = sum(self.photos.views())
return total_views
@property
def cover_url(self):
if self.cover:
@ -35,11 +34,6 @@ class Album(models.Model):
if random_cover:
return random_cover.photo_md.url
return static('img/placeholder.png')
def clean(self):
super().clean()
if self.parent and self.parent == self:
raise ValidationError({'parent': "An album cannot be its own parent."})
def save(self, *args, **kwargs):
if not self.slug:

View file

@ -18,6 +18,8 @@ class Location(models.Model):
class Meta:
verbose_name_plural = "Locations"
unique_together = ('place', "city")
ordering = ['city']
def __str__(self):
if self.place:

14
gallery/models/redir.py Normal file
View file

@ -0,0 +1,14 @@
from django.db import models
from gallery.models import Album
class Redir(models.Model):
path = models.CharField(max_length=255, db_index=True, unique=True, verbose_name="Path")
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='pathurl', verbose_name="Album")
class Meta:
verbose_name_plural = "Redirs"
def __str__(self):
return '{0} - {1}'.format(self.path, self.album.slug)