Source code for apps.posts.models

import os

from django.conf import settings
from django.core.validators import FileExtensionValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

from apps.posts.constants import ContentTypeChoices, StatusPostChoice
from apps.posts.managers import NotDeletedCommentImageManager, NotDeletedCommentManager, NotDeletedManager
from apps.posts.validators import FileSizeValidator


[docs] def commentImageFile(instance, filename): """Function returns path to background image""" return "/".join(["comment-images", str(instance.comment.id), filename])
[docs] def postPreviewPath(instance, filename): return "/".join(["post-content", str(instance.post.id), instance.type, "preview", filename])
[docs] def postFormattedPath(instance, filename): return "/".join(["post-content", str(instance.post.id), instance.type, "formatted", filename])
[docs] def postImageFile(instance, filename): """Function returns path to image""" if settings.IS_TESTING: filename = os.path.basename(filename) return "/".join(["post-content", str(instance.post.id), instance.type, filename])
[docs] def preview_path(): pass
[docs] class Post(models.Model): """ Post model """
[docs] class VisibilityChoices(models.TextChoices): PUBLIC = "public" PRIVATE = "private"
author = models.ForeignKey("users.User", on_delete=models.CASCADE, related_name="posts") title = models.CharField(blank=True, default="", max_length=2200) content = models.TextField(blank=True) status = models.CharField( _("Status"), max_length=17, choices=StatusPostChoice.choices, default=StatusPostChoice.PROCEED ) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) views_quantity = models.PositiveIntegerField(default=0) favorites_quantity = models.PositiveIntegerField(default=0) likes_quantity = models.PositiveIntegerField(default=0) is_deleted = models.BooleanField(default=False) tags = models.ManyToManyField("Tag", blank=True) # type: ignore categories = models.ManyToManyField("users.Category", blank=True) is_commenting_allowed = models.BooleanField(default=True) visibility = models.CharField( max_length=25, choices=VisibilityChoices.choices, default=VisibilityChoices.PRIVATE, ) is_reported = models.BooleanField( default=False, help_text="Indicates whether the post has violated community rules" ) objects = NotDeletedManager() def __str__(self): return f"#{self.pk} {self.title}"
[docs] class Tag(models.Model): """Hashtag model""" name = models.CharField(max_length=128) def __str__(self): return self.name
[docs] class PostMedia(models.Model): """Post Media model""" post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="linked_media") type = models.CharField(_("Type"), choices=ContentTypeChoices.choices, max_length=5) original = models.FileField(_("Original content"), upload_to=postImageFile) formatted_path = models.FileField(_("Formatted path"), null=True, default=None, upload_to=postFormattedPath) preview_path = models.FileField(_("Preview path"), null=True, default=None, upload_to=postPreviewPath) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True)
[docs] class View(models.Model): """ View model """ author = models.ForeignKey("users.User", on_delete=models.CASCADE, related_name="viewed_posts") post = models.ForeignKey("Post", on_delete=models.CASCADE, related_name="views") class Meta: verbose_name = _("View") verbose_name_plural = _("Views") unique_together = ("author", "post")
[docs] class Favorite(models.Model): """ Favorite model """ author = models.ForeignKey("users.User", on_delete=models.CASCADE, related_name="favorite_posts") post = models.ForeignKey("Post", on_delete=models.CASCADE, related_name="favorites") created_at = models.DateTimeField(auto_now_add=True) class Meta: verbose_name = _("Favorite post") verbose_name_plural = _("Favorite posts") unique_together = ("author", "post")
[docs] class Like(models.Model): """Like model""" author = models.ForeignKey("users.User", on_delete=models.CASCADE, related_name="liked_posts") post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="liked") created_at = models.DateTimeField(auto_now_add=True) class Meta: """Metaclass for like model""" verbose_name = _("Like") verbose_name_plural = _("Like") unique_together = ("author", "post") def __str__(self): return "Like for %s from %s" % (self.post, self.author)
[docs] class Comment(models.Model): """Comment model""" author = models.ForeignKey("users.User", on_delete=models.CASCADE, related_name="comments") post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="comments") text = models.TextField(blank=True, null=True) is_deleted = models.BooleanField(_("Is Deleted"), default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) is_reported = models.BooleanField( default=False, help_text="Indicates whether the post has violated community rules" ) objects = NotDeletedCommentManager() class Meta: """Metaclass for Comment model""" verbose_name = _("Comment") verbose_name_plural = _("Comments") def __str__(self): return "Comment for %s" % self.post
[docs] class CommentImage(models.Model): """Comment Media model""" comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name="images") image = models.ImageField( upload_to=commentImageFile, validators=[FileExtensionValidator(["png", "jpg", "jpeg", "heic"]), FileSizeValidator("10MB")], help_text=_("Only png, jpg or jpeg format"), ) is_deleted = models.BooleanField(_("Is Deleted"), default=False) created_at = models.DateTimeField(auto_now_add=True) objects = NotDeletedCommentImageManager() class Meta: """Metaclass for Comment model""" verbose_name = _("Сomment Image") verbose_name_plural = _("Comment Images") def __str__(self): return "Image %s" % self.id