from django.conf import settings
from django.urls import reverse
from django.db import models


"""
Post Model
"""
class Post(models.Model):
    VIEWERS_CHOICES = (
        ('all', 'All'),
        ('followers', 'Followers'),
        ('only me', 'Only Me'),
    )
    user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='posts_created', on_delete=models.CASCADE)
    url = models.URLField()
    description = models.TextField(blank=True)
    likes = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    viewers = models.CharField(choices=VIEWERS_CHOICES, max_length=20)
    
    def __str__(self):
        return self.id

    def get_absolute_url(self):
        return reverse('posts:post_detail', args=[self.post.id])
    
    def total_likes(self):
        return self.likes.count()


"""
Model for post's photoss
"""
class PostAlbum(models.Model):
    post = models.ForeignKey(Post, related_name='post_album', default=None, on_delete=models.CASCADE)
    image = models.FileField(upload_to='photos/%Y/%m/%d/')

    def __str__(self):
        return self.post.id

    def get_absolute_url(self):
        return reverse('posts:post_album', args=[self.post.id])


"""
Model for comments under Post
"""
class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    text = models.TextField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)
    likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='comment_likes', blank=True)

    def __str__(self):
        return '%s - %s' %(self.post, self.author)

    def total_likes(self):
        return self.likes.count()

    def get_absolute_url(self):
        return reverse('posts:post_comment', args=[self.id])
from django.conf import settings
from django.urls import reverse
from django.db import models


"""
Post Model
"""
class Post(models.Model):
    VIEWERS_CHOICES = (
        ('all', 'All'),
        ('followers', 'Followers'),
        ('only me', 'Only Me'),
    )
    user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='posts_created', on_delete=models.CASCADE)
    url = models.URLField()
    description = models.TextField(blank=True)
    likes = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    viewers = models.CharField(choices=VIEWERS_CHOICES, max_length=20)
    
    def __str__(self):
        return self.id

    def get_absolute_url(self):
        return reverse('posts:post_detail', args=[self.post.id])
    
    def total_likes(self):
        return self.likes.count()


"""
Model for post's photoss
"""
class PostAlbum(models.Model):
    post = models.ForeignKey(Post, related_name='post_album', default=None, on_delete=models.CASCADE)
    image = models.FileField(upload_to='photos/%Y/%m/%d/')

    def __str__(self):
        return self.post.id

    def get_absolute_url(self):
        return reverse('posts:post_album', args=[self.post.id])


"""
Model for comments under Post
"""
class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    text = models.TextField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)
    likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='comment_likes', blank=True)

    def __str__(self):
        return '%s - %s' %(self.post, self.author)

    def total_likes(self):
        return self.likes.count()

    def get_absolute_url(self):
        return reverse('posts:post_comment', args=[self.id])
Post Model Model for post's photoss Model for comments under Post