from datetime import datetime from django.db import models from django.urls import reverse from django.utils.text import slugify # 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)