<reponame>favours-io/favours
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse


class Category(models.Model):
    name = models.CharField(max_length=50, unique=True)

    class Meta:
        verbose_name_plural = "categories"

    def __str__(self):
        return self.name


class Post(models.Model):
    title = models.CharField(max_length=100)
    description = models.TextField(max_length=200)
    price = models.DecimalField(max_digits=5, decimal_places=2, default=0)
    date_posted = models.DateTimeField(default=timezone.now)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, blank=True)
    owner = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE, null=False, blank=False)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        # Sets where to redirect user after submitting a post
        return reverse('post-detail', kwargs={"pk": self.pk})

    def save(self, *args, **kwargs):
        super(Post, self).save(*args, **kwargs)