File size: 1,517 Bytes
e2344df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Q


class Comment(models.Model):
    article = models.ForeignKey("articles.Article", on_delete=models.CASCADE, related_name="comments")
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="comments")
    parent = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE, related_name="replies")
    depth = models.PositiveSmallIntegerField(default=0)
    body = models.TextField(max_length=4000)
    hidden = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        indexes = [
            models.Index(fields=["article", "parent", "-created_at"], name="comment_article_parent_idx"),
        ]
        constraints = [
            models.CheckConstraint(check=Q(depth__lte=1), name="comment_depth_at_most_one"),
            models.CheckConstraint(
                check=Q(parent__isnull=True, depth=0) | Q(parent__isnull=False, depth=1),
                name="comment_depth_matches_parent",
            ),
        ]

    def clean(self):
        if self.parent is not None and self.parent.parent_id is not None:
            raise ValidationError("Replies can only be made to top-level comments.")

    def save(self, *args, **kwargs):
        self.depth = 0 if self.parent_id is None else 1
        self.full_clean()
        super().save(*args, **kwargs)