NDOJ/judge/models/comment.py

262 lines
9.1 KiB
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
import itertools
from django.contrib.contenttypes.fields import GenericRelation
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import RegexValidator
from django.db import models
from django.db.models import CASCADE
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from mptt.fields import TreeForeignKey
from mptt.models import MPTTModel
from reversion.models import Version
from judge.models.contest import Contest
from judge.models.interface import BlogPost
2022-05-18 03:34:08 +00:00
from judge.models.problem import Problem, Solution
2020-01-21 06:35:58 +00:00
from judge.models.profile import Profile
from judge.utils.cachedict import CacheDict
2020-07-03 02:50:31 +00:00
2022-05-14 17:57:27 +00:00
__all__ = ["Comment", "CommentLock", "CommentVote", "Notification"]
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
comment_validator = RegexValidator(
r"^[pcs]:[a-z0-9]+$|^b:\d+$", _(r"Page code must be ^[pcs]:[a-z0-9]+$|^b:\d+$")
)
2020-01-21 06:35:58 +00:00
class VersionRelation(GenericRelation):
def __init__(self):
2022-05-14 17:57:27 +00:00
super(VersionRelation, self).__init__(Version, object_id_field="object_id")
2020-01-21 06:35:58 +00:00
def get_extra_restriction(self, where_class, alias, remote_alias):
2022-05-14 17:57:27 +00:00
cond = super(VersionRelation, self).get_extra_restriction(
where_class, alias, remote_alias
)
field = self.remote_field.model._meta.get_field("db")
lookup = field.get_lookup("exact")(field.get_col(remote_alias), "default")
cond.add(lookup, "AND")
2020-01-21 06:35:58 +00:00
return cond
class Comment(MPTTModel):
2022-05-14 17:57:27 +00:00
author = models.ForeignKey(Profile, verbose_name=_("commenter"), on_delete=CASCADE)
time = models.DateTimeField(verbose_name=_("posted time"), auto_now_add=True)
page = models.CharField(
max_length=30,
verbose_name=_("associated page"),
db_index=True,
validators=[comment_validator],
)
score = models.IntegerField(verbose_name=_("votes"), default=0)
body = models.TextField(verbose_name=_("body of comment"), max_length=8192)
hidden = models.BooleanField(verbose_name=_("hide the comment"), default=0)
parent = TreeForeignKey(
"self",
verbose_name=_("parent"),
null=True,
blank=True,
related_name="replies",
on_delete=CASCADE,
)
2020-01-21 06:35:58 +00:00
versions = VersionRelation()
class Meta:
2022-05-14 17:57:27 +00:00
verbose_name = _("comment")
verbose_name_plural = _("comments")
2020-01-21 06:35:58 +00:00
class MPTTMeta:
2022-05-14 17:57:27 +00:00
order_insertion_by = ["-time"]
2020-01-21 06:35:58 +00:00
@classmethod
2023-01-24 02:36:44 +00:00
def most_recent(cls, user, n, batch=None, organization=None):
2022-05-14 17:57:27 +00:00
queryset = (
cls.objects.filter(hidden=False)
.select_related("author__user")
.defer("author__about", "body")
.order_by("-id")
)
2020-01-21 06:35:58 +00:00
2023-01-24 02:36:44 +00:00
if organization:
queryset = queryset.filter(author__in=organization.members.all())
2022-05-14 17:57:27 +00:00
problem_access = CacheDict(
lambda code: Problem.objects.get(code=code).is_accessible_by(user)
)
contest_access = CacheDict(
lambda key: Contest.objects.get(key=key).is_accessible_by(user)
)
2020-01-21 06:35:58 +00:00
blog_access = CacheDict(lambda id: BlogPost.objects.get(id=id).can_see(user))
2022-05-14 17:57:27 +00:00
2022-03-21 21:09:16 +00:00
if n == -1:
2022-05-14 17:57:27 +00:00
n = len(queryset)
2020-01-21 06:35:58 +00:00
if user.is_superuser:
return queryset[:n]
if batch is None:
batch = 2 * n
output = []
for i in itertools.count(0):
2022-05-14 17:57:27 +00:00
slice = queryset[i * batch : i * batch + batch]
2020-01-21 06:35:58 +00:00
if not slice:
break
for comment in slice:
2022-05-14 17:57:27 +00:00
if comment.page.startswith("p:") or comment.page.startswith("s:"):
2020-01-21 06:35:58 +00:00
try:
if problem_access[comment.page[2:]]:
output.append(comment)
except Problem.DoesNotExist:
pass
2022-05-14 17:57:27 +00:00
elif comment.page.startswith("c:"):
2020-01-21 06:35:58 +00:00
try:
if contest_access[comment.page[2:]]:
output.append(comment)
except Contest.DoesNotExist:
pass
2022-05-14 17:57:27 +00:00
elif comment.page.startswith("b:"):
2020-01-21 06:35:58 +00:00
try:
if blog_access[comment.page[2:]]:
output.append(comment)
except BlogPost.DoesNotExist:
pass
else:
output.append(comment)
if len(output) >= n:
return output
return output
@cached_property
def link(self):
try:
link = None
2022-05-14 17:57:27 +00:00
if self.page.startswith("p:"):
link = reverse("problem_detail", args=(self.page[2:],))
elif self.page.startswith("c:"):
link = reverse("contest_view", args=(self.page[2:],))
elif self.page.startswith("b:"):
key = "blog_slug:%s" % self.page[2:]
2020-01-21 06:35:58 +00:00
slug = cache.get(key)
if slug is None:
try:
slug = BlogPost.objects.get(id=self.page[2:]).slug
except ObjectDoesNotExist:
2022-05-14 17:57:27 +00:00
slug = ""
2020-01-21 06:35:58 +00:00
cache.set(key, slug, 3600)
2022-05-14 17:57:27 +00:00
link = reverse("blog_post", args=(self.page[2:], slug))
elif self.page.startswith("s:"):
link = reverse("problem_editorial", args=(self.page[2:],))
2020-01-21 06:35:58 +00:00
except Exception:
2022-05-14 17:57:27 +00:00
link = "invalid"
2020-01-21 06:35:58 +00:00
return link
@classmethod
def get_page_title(cls, page):
try:
2022-05-14 17:57:27 +00:00
if page.startswith("p:"):
return Problem.objects.values_list("name", flat=True).get(code=page[2:])
elif page.startswith("c:"):
return Contest.objects.values_list("name", flat=True).get(key=page[2:])
elif page.startswith("b:"):
return BlogPost.objects.values_list("title", flat=True).get(id=page[2:])
elif page.startswith("s:"):
return _("Editorial for %s") % Problem.objects.values_list(
"name", flat=True
).get(code=page[2:])
return "<unknown>"
2020-01-21 06:35:58 +00:00
except ObjectDoesNotExist:
2022-05-14 17:57:27 +00:00
return "<deleted>"
2020-01-21 06:35:58 +00:00
@cached_property
def page_title(self):
return self.get_page_title(self.page)
def get_absolute_url(self):
2022-05-14 17:57:27 +00:00
return "%s#comment-%d" % (self.link, self.id)
2020-01-21 06:35:58 +00:00
2022-05-18 03:34:08 +00:00
@cached_property
def page_object(self):
try:
page = self.page
if page.startswith("p:"):
return Problem.objects.get(code=page[2:])
elif page.startswith("c:"):
return Contest.objects.get(key=page[2:])
elif page.startswith("b:"):
return BlogPost.objects.get(id=page[2:])
elif page.startswith("s:"):
return Solution.objects.get(problem__code=page[2:])
return None
except ObjectDoesNotExist:
return None
2020-01-21 06:35:58 +00:00
def __str__(self):
2022-05-14 17:57:27 +00:00
return "%(page)s by %(user)s" % {
"page": self.page,
"user": self.author.user.username,
}
2020-01-21 06:35:58 +00:00
# Only use this when queried with
# .prefetch_related(Prefetch('votes', queryset=CommentVote.objects.filter(voter_id=profile_id)))
# It's rather stupid to put a query specific property on the model, but the alternative requires
# digging Django internals, and could not be guaranteed to work forever.
# Hence it is left here for when the alternative breaks.
# @property
# def vote_score(self):
# queryset = self.votes.all()
# if not queryset:
# return 0
# return queryset[0].score
class CommentVote(models.Model):
2022-05-14 17:57:27 +00:00
voter = models.ForeignKey(Profile, related_name="voted_comments", on_delete=CASCADE)
comment = models.ForeignKey(Comment, related_name="votes", on_delete=CASCADE)
2020-01-21 06:35:58 +00:00
score = models.IntegerField()
class Meta:
2022-05-14 17:57:27 +00:00
unique_together = ["voter", "comment"]
verbose_name = _("comment vote")
verbose_name_plural = _("comment votes")
2020-01-21 06:35:58 +00:00
class CommentLock(models.Model):
2022-05-14 17:57:27 +00:00
page = models.CharField(
max_length=30,
verbose_name=_("associated page"),
db_index=True,
validators=[comment_validator],
)
2020-01-21 06:35:58 +00:00
class Meta:
2022-05-14 17:57:27 +00:00
permissions = (("override_comment_lock", _("Override comment lock")),)
2020-01-21 06:35:58 +00:00
def __str__(self):
return str(self.page)
2020-07-03 02:50:31 +00:00
class Notification(models.Model):
2022-05-14 17:57:27 +00:00
owner = models.ForeignKey(
Profile,
verbose_name=_("owner"),
related_name="notifications",
on_delete=CASCADE,
)
time = models.DateTimeField(verbose_name=_("posted time"), auto_now_add=True)
comment = models.ForeignKey(
Comment, null=True, verbose_name=_("comment"), on_delete=CASCADE
)
read = models.BooleanField(verbose_name=_("read"), default=False)
category = models.CharField(verbose_name=_("category"), max_length=1000)
2022-05-14 17:57:27 +00:00
html_link = models.TextField(
default="",
verbose_name=_("html link to comments, used for non-comments"),
max_length=1000,
)
author = models.ForeignKey(
Profile,
null=True,
verbose_name=_("who trigger, used for non-comment"),
on_delete=CASCADE,
)