from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class PublishedManager(models.Manager):
     def get_queryset(self):
        return super().get_queryset()\
                      . filter(status=Post.Status.PUBLISHED)

class Post(models.Model):

    class Status(models.TextChoices):
        DRAFT = 'DF', 'Roboczy'
        PUBLISHED = 'PB', 'Opublikowany'

    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250)
    author = models.ForeignKey(User,
             on_delete=models.CASCADE,
             related_name='blog_posts')

    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=2,
                            choices=Status.choices,
                            default=Status.DRAFT)
    
    object = models.Manager() # Menedżer domyślny.
    published = PublishedManager() # Menedżer niestandardowy.


    class Meta:
        ordering = ['-publish']
        indexes = [
             models.Index(fields=['-publish']),
         ]



    def __str__(self):
        return self.title
