from django.db import models
from django.db.models.signals import pre_save, post_save
from moby_dick.utils import slugify_instance_title

class Anime(models.Model):
    image = models.ImageField(upload_to='anime/% Y/% m/% d/', height_field=1800, width_field=900, )
    title = models.CharField(max_length=10000)
    slug = models.SlugField(blank=True, null=True)
    content = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    publish = models.DateField(auto_now_add=False, auto_now=False, null=True, blank=True)

    def __str__(self):
        return self.title

def anime_pre_save(instance):
    if instance.slug is None:
        slugify_instance_title(instance, save=False)

def anime_post_save(instance, created):
    if created:
        slugify_instance_title(instance, save=True)

pre_save.connect(anime_pre_save, sender=Anime)
post_save.connect(anime_post_save, sender=Anime)
