<reponame>ahanaf-islam/blog-api
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.db import models
from django.db.models.signals import pre_save
from datetime import datetime
from django.utils.safestring import mark_safe
from django.utils.text import slugify


from comments.models import Comment


class BlogPost(models.Model):
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE
    )
    title = models.CharField(max_length=100, blank=False, null=False)
    blog_image = models.ImageField(upload_to='blog_image/%Y/%m/%d/', blank=False)
    content = models.TextField(blank=False)
    date_posted = models.DateTimeField(default=datetime.now)
    slug = models.SlugField(unique=True)

    def __unicode__(self):
        return self.title

    def __str__(self):
        return self.title

    """def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})

    def get_api_url(self):
        return reverse("posts-api:detail", kwargs={"slug": self.slug})"""
        

    @property
    def comments(self):
        instance = self
        qs = Comment.objects.filter_by_instance(instance)
        return qs

    @property
    def get_content_type(self):
        instance = self
        content_type = ContentType.objects.get_for_model(instance.__class__)
        return content_type


def create_slug(instance, new_slug=None):
    slug = slugify(instance.title)
    if new_slug is not None:
        slug = new_slug
    qs = BlogPost.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" %(slug, qs.first().id)
        return create_slug(instance, new_slug=new_slug)
    return slug


def pre_save_post_receiver(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance)


pre_save.connect(pre_save_post_receiver, sender=BlogPost)
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.db import models
from django.db.models.signals import pre_save
from datetime import datetime
from django.utils.safestring import mark_safe
from django.utils.text import slugify


from comments.models import Comment


class BlogPost(models.Model):
    author = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE
    )
    title = models.CharField(max_length=100, blank=False, null=False)
    blog_image = models.ImageField(upload_to='blog_image/%Y/%m/%d/', blank=False)
    content = models.TextField(blank=False)
    date_posted = models.DateTimeField(default=datetime.now)
    slug = models.SlugField(unique=True)

    def __unicode__(self):
        return self.title

    def __str__(self):
        return self.title

    """def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})

    def get_api_url(self):
        return reverse("posts-api:detail", kwargs={"slug": self.slug})"""
        

    @property
    def comments(self):
        instance = self
        qs = Comment.objects.filter_by_instance(instance)
        return qs

    @property
    def get_content_type(self):
        instance = self
        content_type = ContentType.objects.get_for_model(instance.__class__)
        return content_type


def create_slug(instance, new_slug=None):
    slug = slugify(instance.title)
    if new_slug is not None:
        slug = new_slug
    qs = BlogPost.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" %(slug, qs.first().id)
        return create_slug(instance, new_slug=new_slug)
    return slug


def pre_save_post_receiver(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance)


pre_save.connect(pre_save_post_receiver, sender=BlogPost)
def get_absolute_url(self):         return reverse("posts:detail", kwargs={"slug": self.slug})      def get_api_url(self):         return reverse("posts-api:detail", kwargs={"slug": self.slug})