diff --git a/judge/comments.py b/judge/comments.py index 16e6676..5ce4fa7 100644 --- a/judge/comments.py +++ b/judge/comments.py @@ -98,7 +98,6 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): @method_decorator(login_required) def post(self, request, *args, **kwargs): self.object = self.get_object() - page = self.get_comment_page() if self.is_comment_locked(): return HttpResponseForbidden() @@ -116,7 +115,6 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): if form.is_valid(): comment = form.save(commit=False) comment.author = request.profile - comment.page = page comment.linked_object = self.object with LockModel( diff --git a/judge/migrations/drop_comment_page.py b/judge/migrations/drop_comment_page.py new file mode 100644 index 0000000..a03ac09 --- /dev/null +++ b/judge/migrations/drop_comment_page.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.18 on 2023-02-20 23:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("judge", "0152_migrate_comments"), + ] + + operations = [ + migrations.RemoveField( + model_name="comment", + name="page", + ), + migrations.AlterField( + model_name="commentlock", + name="page", + field=models.CharField( + db_index=True, max_length=30, verbose_name="associated page" + ), + ), + ] diff --git a/judge/models/comment.py b/judge/models/comment.py index 24fc007..fc9af4c 100644 --- a/judge/models/comment.py +++ b/judge/models/comment.py @@ -24,10 +24,6 @@ from judge.utils.cachedict import CacheDict __all__ = ["Comment", "CommentLock", "CommentVote", "Notification"] -comment_validator = RegexValidator( - r"^[pcs]:[a-z0-9]+$|^b:\d+$", _(r"Page code must be ^[pcs]:[a-z0-9]+$|^b:\d+$") -) - class VersionRelation(GenericRelation): def __init__(self): @@ -49,12 +45,6 @@ class Comment(MPTTModel): content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.PositiveIntegerField() linked_object = GenericForeignKey("content_type", "object_id") - 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) @@ -170,7 +160,6 @@ class CommentLock(models.Model): max_length=30, verbose_name=_("associated page"), db_index=True, - validators=[comment_validator], ) class Meta: diff --git a/judge/views/blog.py b/judge/views/blog.py index 346466c..3fe0f45 100644 --- a/judge/views/blog.py +++ b/judge/views/blog.py @@ -101,33 +101,12 @@ class PostList(HomeFeedView): queryset = queryset.filter(filter) return queryset - def get_feed_context(self, object_list): - post_comment_counts = { - int(page[2:]): count - for page, count in Comment.objects.filter( - page__in=["b:%d" % post.id for post in object_list], hidden=False - ) - .values_list("page") - .annotate(count=Count("page")) - .order_by() - } - return {"post_comment_counts": post_comment_counts} - def get_context_data(self, **kwargs): context = super(PostList, self).get_context_data(**kwargs) context["title"] = ( self.title or _("Page %d of Posts") % context["page_obj"].number ) context["page_type"] = "blog" - context["post_comment_counts"] = { - int(page[2:]): count - for page, count in Comment.objects.filter( - page__in=["b:%d" % post.id for post in context["posts"]], hidden=False - ) - .values_list("page") - .annotate(count=Count("page")) - .order_by() - } return context def get_comment_page(self, post): diff --git a/judge/views/organization.py b/judge/views/organization.py index 75fc920..c03ee37 100644 --- a/judge/views/organization.py +++ b/judge/views/organization.py @@ -269,18 +269,6 @@ class OrganizationHome(OrganizationHomeView, FeedView): def get_comment_page(self, post): return "b:%s" % post.id - def get_feed_context(self, object_list): - post_comment_counts = { - int(page[2:]): count - for page, count in Comment.objects.filter( - page__in=["b:%d" % post.id for post in object_list], hidden=False - ) - .values_list("page") - .annotate(count=Count("page")) - .order_by() - } - return {"post_comment_counts": post_comment_counts} - def get_context_data(self, **kwargs): context = super(OrganizationHome, self).get_context_data(**kwargs) context["title"] = self.organization.name @@ -292,15 +280,6 @@ class OrganizationHome(OrganizationHomeView, FeedView): + "." + get_current_site(self.request).domain ) - context["post_comment_counts"] = { - int(page[2:]): count - for page, count in Comment.objects.filter( - page__in=["b:%d" % post.id for post in context["posts"]], hidden=False - ) - .values_list("page") - .annotate(count=Count("page")) - .order_by() - } now = timezone.now() visible_contests = ( diff --git a/templates/blog/content.html b/templates/blog/content.html index a20512d..4124729 100644 --- a/templates/blog/content.html +++ b/templates/blog/content.html @@ -29,7 +29,7 @@ - {{- post_comment_counts[post.id] or 0 -}} + {{- post.comments.filter(hidden=False).count() or 0 -}}