from django.conf import settings
from django.db import models
from django.utils import timezone

def upload_location(instance, filename):
    return "%s/%s" %(instance.id, filename)

class Category(models.Model):
    name = models.CharField(max_length=200)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.name

class Post(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    category = models.CharField(max_length=200, default ="uncategorized")
    location = models.CharField(max_length=200, default ="uncategorized")
    text = models.TextField()
    thumb = models.ImageField(upload_to=upload_location, default='default.png', blank=True)
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

class Task(models.Model):
    todo = models.CharField(max_length=200)
    done= models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.todo
