Split models

This commit is contained in:
Nyymix 2025-01-09 20:18:43 +02:00
parent ea92c2c788
commit 7a95a468d2
5 changed files with 69 additions and 55 deletions

View file

@ -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 datetime
import django.db.models.deletion import django.db.models.deletion
import gallery.models import gallery.models.photo
from django.db import migrations, models 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')), ('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')),
('album_date', models.DateField(default=datetime.datetime.now, verbose_name='Album Date')), ('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')), ('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')), ('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=[ fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('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')), ('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')), ('width', models.PositiveIntegerField(default=0, editable=False, verbose_name='Photo Width')),
('height', models.PositiveIntegerField(default=0, editable=False, verbose_name='Photo Height')), ('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')), ('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')), ('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')), ('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='gallery.album', verbose_name='Album')),
], ],
), ),

View file

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

30
gallery/models/album.py Normal file
View file

@ -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)

View file

@ -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)

View file

@ -1,63 +1,13 @@
import os import os
from datetime import datetime, timedelta from datetime import datetime
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils.text import slugify
from imagekit.models import ImageSpecField from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill from imagekit.processors import ResizeToFill
from PIL import Image
from gallery.exif import Exif from gallery.exif import Exif
from gallery.models import Album
# 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)
class Photo(models.Model): class Photo(models.Model):
@ -79,6 +29,8 @@ class Photo(models.Model):
height = models.PositiveIntegerField(default=0, editable=False, verbose_name="Photo Height") 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") 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") 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): def save(self, *args, **kwargs):
self.exif_data = Exif(self.photo.file) self.exif_data = Exif(self.photo.file)