From ff6988f29ced561942825391ca55b18b186cce76 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Tue, 5 Dec 2023 20:01:23 -0600 Subject: [PATCH] Add comment revision count field --- judge/comments.py | 2 -- .../migrations/0176_comment_revision_count.py | 30 +++++++++++++++++++ judge/models/comment.py | 5 +--- judge/views/comment.py | 4 +-- templates/comments/content-list.html | 17 +++++------ 5 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 judge/migrations/0176_comment_revision_count.py diff --git a/judge/comments.py b/judge/comments.py index 98434cf..b4de658 100644 --- a/judge/comments.py +++ b/judge/comments.py @@ -169,7 +169,6 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): queryset.select_related("author__user") .filter(hidden=False) .defer("author__about") - .annotate(revisions=Count("versions", distinct=True)) ) else: queryset = self.object.comments @@ -180,7 +179,6 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): .filter(hidden=False) .annotate( count_replies=Count("replies", distinct=True), - revisions=Count("versions", distinct=True), )[:DEFAULT_OFFSET] ) diff --git a/judge/migrations/0176_comment_revision_count.py b/judge/migrations/0176_comment_revision_count.py new file mode 100644 index 0000000..ed2a246 --- /dev/null +++ b/judge/migrations/0176_comment_revision_count.py @@ -0,0 +1,30 @@ +# Generated by Django 3.2.18 on 2023-12-06 01:28 + +from django.db import migrations, models + + +# Run this in shell +def migrate_revision(apps, schema_editor): + Comment = apps.get_model("judge", "Comment") + + for c in Comment.objects.all(): + c.revision_count = c.versions.count() + c.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("judge", "0175_add_profile_index"), + ] + + operations = [ + migrations.AddField( + model_name="comment", + name="revision_count", + field=models.PositiveIntegerField(default=1), + ), + # migrations.RunPython( + # migrate_revision, migrations.RunPython.noop, atomic=True + # ), + ] diff --git a/judge/models/comment.py b/judge/models/comment.py index 2cbe20a..0588dfa 100644 --- a/judge/models/comment.py +++ b/judge/models/comment.py @@ -56,6 +56,7 @@ class Comment(MPTTModel): related_name="replies", on_delete=CASCADE, ) + revision_count = models.PositiveIntegerField(default=1) versions = VersionRelation() @@ -118,10 +119,6 @@ class Comment(MPTTModel): query = Comment.filter(parent=self) return len(query) - @cached_property - def get_revisions(self): - return self.versions.count() - @cached_property def page_title(self): if isinstance(self.linked_object, Problem): diff --git a/judge/views/comment.py b/judge/views/comment.py index caa4eb6..af00190 100644 --- a/judge/views/comment.py +++ b/judge/views/comment.py @@ -147,7 +147,6 @@ def get_comments(request, limit=10): .defer("author__about") .annotate( count_replies=Count("replies", distinct=True), - revisions=Count("versions", distinct=True), )[offset : offset + limit] ) profile = None @@ -241,7 +240,8 @@ class CommentEditAjax(LoginRequiredMixin, CommentMixin, UpdateView): # update notifications comment = form.instance add_mention_notifications(comment) - + comment.revision_count = comment.versions.count() + 1 + comment.save(update_fields=["revision_count"]) with transaction.atomic(), revisions.create_revision(): revisions.set_comment(_("Edited from site")) revisions.set_user(self.request.user) diff --git a/templates/comments/content-list.html b/templates/comments/content-list.html index ebf0c31..1e94cf0 100644 --- a/templates/comments/content-list.html +++ b/templates/comments/content-list.html @@ -1,5 +1,5 @@ {% for node in mptt_tree(comment_list) recursive %} -
  • @@ -33,12 +33,12 @@ {{ relative_time(node.time, abs=_('{time}'), rel=_('{time}')) }} - {% if node.revisions > 1 %} + {% if node.revision_count > 1 %} - {% if node.revisions > 2 %} - {% trans edits=node.revisions - 1 %}edit {{ edits }}{% endtrans %} + {% if node.revision_count > 2 %} + {% trans edits=node.revision_count - 1 %}edit {{ edits }}{% endtrans %} {% else %} {{ _('edited') }} {% endif %} @@ -55,8 +55,7 @@ {% if profile and not comment_lock %} {% set can_edit = node.author.id == profile.id and not profile.mute %} {% if can_edit %} - + {% else %} @@ -69,9 +68,9 @@ {% else %} - + + + {% endif %}