Add parent albums

This commit is contained in:
Nyymix 2025-01-18 09:40:47 +02:00
parent f3a94857d0
commit b7c183d2f3
3 changed files with 16 additions and 4 deletions

View file

@ -39,7 +39,7 @@ class AlbumAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('name',)} prepopulated_fields = {'slug': ('name',)}
list_display = ('__str__', 'location', 'album_date', 'is_public', 'thumbnail', ) list_display = ('__str__', 'location', 'album_date', 'is_public', 'thumbnail', )
search_fields = ('name',) search_fields = ('name',)
ordering = ('name',) ordering = ('-album_date',)
list_per_page = 30 list_per_page = 30
list_editable = ('is_public', 'location') list_editable = ('is_public', 'location')

View file

@ -1,4 +1,4 @@
# Generated by Django 5.1.4 on 2025-01-15 18:22 # Generated by Django 5.1.4 on 2025-01-18 07:39
import datetime import datetime
import django.db.models.deletion import django.db.models.deletion
@ -44,10 +44,12 @@ class Migration(migrations.Migration):
('slug', models.SlugField(max_length=150, unique=True, verbose_name='Slug')), ('slug', models.SlugField(max_length=150, unique=True, verbose_name='Slug')),
('album_date', models.DateField(default=datetime.datetime.now, verbose_name='Album Date')), ('album_date', models.DateField(default=datetime.datetime.now, verbose_name='Album Date')),
('is_public', models.BooleanField(default=False, verbose_name='Published')), ('is_public', models.BooleanField(default=False, verbose_name='Published')),
('parent', models.ForeignKey(blank=True, limit_choices_to={'parent__isnull': True}, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='gallery.album', verbose_name='Parent Album')),
('location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='albums', to='gallery.location', verbose_name='Location')), ('location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='albums', to='gallery.location', verbose_name='Location')),
], ],
options={ options={
'verbose_name_plural': 'Albums', 'verbose_name_plural': 'Albums',
'ordering': ('album_date',),
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -65,6 +67,10 @@ class Migration(migrations.Migration):
('likes', models.PositiveIntegerField(default=0, verbose_name='Likes')), ('likes', models.PositiveIntegerField(default=0, verbose_name='Likes')),
('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='gallery.album', verbose_name='Album')), ('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='gallery.album', verbose_name='Album')),
], ],
options={
'verbose_name_plural': 'Photos',
'ordering': ('-taken_at',),
},
), ),
migrations.AddField( migrations.AddField(
model_name='album', model_name='album',

View file

@ -1,5 +1,6 @@
from datetime import datetime from datetime import datetime
from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.templatetags.static import static from django.templatetags.static import static
from django.urls import reverse from django.urls import reverse
@ -7,12 +8,11 @@ from django.utils.text import slugify
from gallery.models.location import Location from gallery.models.location import Location
#from gallery.models.photo import Photo
class Album(models.Model): class Album(models.Model):
name = models.CharField(max_length=150, unique=True, verbose_name="Album") name = models.CharField(max_length=150, unique=True, verbose_name="Album")
slug = models.SlugField(max_length=150, unique=True, verbose_name="Slug") 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") 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") 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") cover = models.ForeignKey("Photo", blank=True, null=True, on_delete=models.SET_NULL, related_name='cover_to', verbose_name="Album cover")
@ -30,6 +30,11 @@ class Album(models.Model):
if random_cover: if random_cover:
return random_cover.photo.url return random_cover.photo.url
return static('img/placeholder.png') 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): def save(self, *args, **kwargs):
if not self.slug: if not self.slug:
@ -41,6 +46,7 @@ class Album(models.Model):
class Meta: class Meta:
verbose_name_plural = "Albums" verbose_name_plural = "Albums"
ordering = ('album_date',)
def __str__(self): def __str__(self):
return '{}'.format(self.name) return '{}'.format(self.name)