From 7a95a468d2bdca8d2032ea2012e8ddc340b887d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9DNyymix=E2=80=9D?= Date: Thu, 9 Jan 2025 20:18:43 +0200 Subject: [PATCH] Split models --- gallery/migrations/0001_initial.py | 9 +++-- gallery/models/__init__.py | 3 ++ gallery/models/album.py | 30 ++++++++++++++ gallery/models/location.py | 26 ++++++++++++ gallery/{models.py => models/photo.py} | 56 ++------------------------ 5 files changed, 69 insertions(+), 55 deletions(-) create mode 100644 gallery/models/__init__.py create mode 100644 gallery/models/album.py create mode 100644 gallery/models/location.py rename gallery/{models.py => models/photo.py} (54%) diff --git a/gallery/migrations/0001_initial.py b/gallery/migrations/0001_initial.py index 179863c..d4fd931 100644 --- a/gallery/migrations/0001_initial.py +++ b/gallery/migrations/0001_initial.py @@ -1,8 +1,8 @@ -# Generated by Django 5.1.4 on 2025-01-05 15:24 +# Generated by Django 5.1.4 on 2025-01-09 18:18 import datetime import django.db.models.deletion -import gallery.models +import gallery.models.photo from django.db import migrations, models @@ -43,6 +43,7 @@ class Migration(migrations.Migration): ('name', models.CharField(max_length=150, unique=True, verbose_name='Album')), ('slug', models.SlugField(max_length=150, unique=True, verbose_name='Slug')), ('album_date', models.DateField(default=datetime.datetime.now, verbose_name='Album Date')), + ('cover', models.ImageField(blank=True, null=True, upload_to='covers/', verbose_name='Album Cover')), ('is_public', models.BooleanField(default=False, verbose_name='Published')), ('location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='albums', to='gallery.location', verbose_name='Location')), ], @@ -55,11 +56,13 @@ class Migration(migrations.Migration): fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('slug', models.CharField(editable=False, max_length=15, verbose_name='Photo Slug')), - ('photo', models.ImageField(height_field='height', upload_to=gallery.models.Photo._get_upload_path, verbose_name='Photo', width_field='width')), + ('photo', models.ImageField(height_field='height', upload_to=gallery.models.photo.Photo._get_upload_path, verbose_name='Photo', width_field='width')), ('width', models.PositiveIntegerField(default=0, editable=False, verbose_name='Photo Width')), ('height', models.PositiveIntegerField(default=0, editable=False, verbose_name='Photo Height')), ('taken_at', models.DateTimeField(blank=True, editable=False, null=True, verbose_name='Taken at')), ('exif', models.JSONField(blank=True, editable=False, null=True, verbose_name='Exif Metadata')), + ('is_favorite', models.BooleanField(default=False, verbose_name='Is Favorite')), + ('views', models.PositiveIntegerField(default=0, verbose_name='Views')), ('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='gallery.album', verbose_name='Album')), ], ), diff --git a/gallery/models/__init__.py b/gallery/models/__init__.py new file mode 100644 index 0000000..0721982 --- /dev/null +++ b/gallery/models/__init__.py @@ -0,0 +1,3 @@ +from .album import * +from .location import * +from .photo import * diff --git a/gallery/models/album.py b/gallery/models/album.py new file mode 100644 index 0000000..da43b73 --- /dev/null +++ b/gallery/models/album.py @@ -0,0 +1,30 @@ +from datetime import datetime + +from django.db import models +from django.urls import reverse +from django.utils.text import slugify + +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") + 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.ImageField(upload_to="covers/", blank=True, null=True, verbose_name="Album Cover") + is_public = models.BooleanField(default=False, verbose_name="Published") + + def save(self, *args, **kwargs): + if not self.slug: + self.slug = slugify(self.name) + super(Album, self).save(*args, **kwargs) + + def get_absolute_url(self): + return reverse('gallery:album_url', kwargs={'album_slug': self.slug}) + + class Meta: + verbose_name_plural = "Albums" + + def __str__(self): + return '{}'.format(self.name) diff --git a/gallery/models/location.py b/gallery/models/location.py new file mode 100644 index 0000000..9790efe --- /dev/null +++ b/gallery/models/location.py @@ -0,0 +1,26 @@ +from django.db import models + + +class City(models.Model): + name = models.CharField(max_length=150, unique=True, verbose_name="City") + + class Meta: + verbose_name_plural = "Cities" + + def __str__(self): + return '{}'.format(self.name) + + +class Location(models.Model): + place = models.CharField(max_length=250, blank=True, null=False, verbose_name="Place") + city = models.ForeignKey(City, on_delete=models.CASCADE, related_name='places', verbose_name="City") + + class Meta: + verbose_name_plural = "Locations" + unique_together = ('place', "city") + + def __str__(self): + if self.place: + return '{0}, {1}'.format(self.city, self.place) + else: + return '{}'.format(self.city) diff --git a/gallery/models.py b/gallery/models/photo.py similarity index 54% rename from gallery/models.py rename to gallery/models/photo.py index 8e47cd1..3e16f22 100644 --- a/gallery/models.py +++ b/gallery/models/photo.py @@ -1,63 +1,13 @@ import os -from datetime import datetime, timedelta +from datetime import datetime from django.db import models from django.urls import reverse -from django.utils.text import slugify from imagekit.models import ImageSpecField from imagekit.processors import ResizeToFill -from PIL import Image from gallery.exif import Exif - -# Create your models here. - - -class City(models.Model): - name = models.CharField(max_length=150, unique=True, verbose_name="City") - - class Meta: - verbose_name_plural = "Cities" - - def __str__(self): - return '{}'.format(self.name) - - -class Location(models.Model): - place = models.CharField(max_length=250, blank=True, null=False, verbose_name="Place") - city = models.ForeignKey(City, on_delete=models.CASCADE, related_name='places', verbose_name="City") - - class Meta: - verbose_name_plural = "Locations" - unique_together = ('place', "city") - - def __str__(self): - if self.place: - return '{0}, {1}'.format(self.city, self.place) - else: - return '{}'.format(self.city) - - -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") - 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") - is_public = models.BooleanField(default=False, verbose_name="Published") - - def save(self, *args, **kwargs): - if not self.slug: - self.slug = slugify(self.name) - super(Album, self).save(*args, **kwargs) - - def get_absolute_url(self): - return reverse('gallery:album_url', kwargs={'album_slug': self.slug}) - - class Meta: - verbose_name_plural = "Albums" - - def __str__(self): - return '{}'.format(self.name) +from gallery.models import Album class Photo(models.Model): @@ -79,6 +29,8 @@ class Photo(models.Model): height = models.PositiveIntegerField(default=0, editable=False, verbose_name="Photo Height") taken_at = models.DateTimeField(blank=True, null=True, editable=False, verbose_name="Taken at") exif = models.JSONField(blank=True, null=True, editable=False, verbose_name="Exif Metadata") + is_favorite = models.BooleanField(default=False, verbose_name="Is Favorite") + views = models.PositiveIntegerField(default=0, verbose_name="Views") def save(self, *args, **kwargs): self.exif_data = Exif(self.photo.file)