Add comment revision count field

This commit is contained in:
cuom1999 2023-12-05 20:01:23 -06:00
parent b02a30819f
commit ff6988f29c
5 changed files with 41 additions and 17 deletions

View file

@ -169,7 +169,6 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
queryset.select_related("author__user") queryset.select_related("author__user")
.filter(hidden=False) .filter(hidden=False)
.defer("author__about") .defer("author__about")
.annotate(revisions=Count("versions", distinct=True))
) )
else: else:
queryset = self.object.comments queryset = self.object.comments
@ -180,7 +179,6 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
.filter(hidden=False) .filter(hidden=False)
.annotate( .annotate(
count_replies=Count("replies", distinct=True), count_replies=Count("replies", distinct=True),
revisions=Count("versions", distinct=True),
)[:DEFAULT_OFFSET] )[:DEFAULT_OFFSET]
) )

View file

@ -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
# ),
]

View file

@ -56,6 +56,7 @@ class Comment(MPTTModel):
related_name="replies", related_name="replies",
on_delete=CASCADE, on_delete=CASCADE,
) )
revision_count = models.PositiveIntegerField(default=1)
versions = VersionRelation() versions = VersionRelation()
@ -118,10 +119,6 @@ class Comment(MPTTModel):
query = Comment.filter(parent=self) query = Comment.filter(parent=self)
return len(query) return len(query)
@cached_property
def get_revisions(self):
return self.versions.count()
@cached_property @cached_property
def page_title(self): def page_title(self):
if isinstance(self.linked_object, Problem): if isinstance(self.linked_object, Problem):

View file

@ -147,7 +147,6 @@ def get_comments(request, limit=10):
.defer("author__about") .defer("author__about")
.annotate( .annotate(
count_replies=Count("replies", distinct=True), count_replies=Count("replies", distinct=True),
revisions=Count("versions", distinct=True),
)[offset : offset + limit] )[offset : offset + limit]
) )
profile = None profile = None
@ -241,7 +240,8 @@ class CommentEditAjax(LoginRequiredMixin, CommentMixin, UpdateView):
# update notifications # update notifications
comment = form.instance comment = form.instance
add_mention_notifications(comment) add_mention_notifications(comment)
comment.revision_count = comment.versions.count() + 1
comment.save(update_fields=["revision_count"])
with transaction.atomic(), revisions.create_revision(): with transaction.atomic(), revisions.create_revision():
revisions.set_comment(_("Edited from site")) revisions.set_comment(_("Edited from site"))
revisions.set_user(self.request.user) revisions.set_user(self.request.user)

View file

@ -1,5 +1,5 @@
{% for node in mptt_tree(comment_list) recursive %} {% for node in mptt_tree(comment_list) recursive %}
<li id="comment-{{ node.id }}" data-revision="{{ node.revisions - 1 }}" data-max-revision="{{ node.revisions - 1 }}" <li id="comment-{{ node.id }}" data-revision="{{ node.revision_count - 1 }}" data-max-revision="{{ node.revision_count - 1 }}"
data-revision-ajax="{{ url('comment_revision_ajax', node.id) }}" class="comment"> data-revision-ajax="{{ url('comment_revision_ajax', node.id) }}" class="comment">
<div class="comment-display{% if node.score <= vote_hide_threshold %} bad-comment{% endif %}"> <div class="comment-display{% if node.score <= vote_hide_threshold %} bad-comment{% endif %}">
<div class="info"> <div class="info">
@ -33,12 +33,12 @@
{{ relative_time(node.time, abs=_('{time}'), rel=_('{time}')) }} {{ relative_time(node.time, abs=_('{time}'), rel=_('{time}')) }}
<span class="comment-spacer"></span> <span class="comment-spacer"></span>
<span class="comment-operation"> <span class="comment-operation">
{% if node.revisions > 1 %} {% if node.revision_count > 1 %}
<span class="comment-edits"> <span class="comment-edits">
<a href="javascript:show_revision({{ node.id }}, -1)" class="previous-revision">&larr;</a> <a href="javascript:show_revision({{ node.id }}, -1)" class="previous-revision">&larr;</a>
<span class="comment-edit-text"> <span class="comment-edit-text">
{% if node.revisions > 2 %} {% if node.revision_count > 2 %}
{% trans edits=node.revisions - 1 %}edit {{ edits }}{% endtrans %} {% trans edits=node.revision_count - 1 %}edit {{ edits }}{% endtrans %}
{% else %} {% else %}
{{ _('edited') }} {{ _('edited') }}
{% endif %} {% endif %}
@ -55,8 +55,7 @@
{% if profile and not comment_lock %} {% if profile and not comment_lock %}
{% set can_edit = node.author.id == profile.id and not profile.mute %} {% set can_edit = node.author.id == profile.id and not profile.mute %}
{% if can_edit %} {% if can_edit %}
<a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" <a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" href="#" title="{{ _('Edit') }}" class="edit-link">
href="{{ url('comment_edit', node.id) }}" title="{{ _('Edit') }}" class="edit-link">
<i class="fa fa-pencil fa-fw"></i> <i class="fa fa-pencil fa-fw"></i>
</a> </a>
{% else %} {% else %}
@ -69,9 +68,9 @@
<a href="javascript:reply_comment({{ node.id }})" title="{{ _('Reply') }}"><i <a href="javascript:reply_comment({{ node.id }})" title="{{ _('Reply') }}"><i
class="fa fa-reply fa-fw"></i></a> class="fa fa-reply fa-fw"></i></a>
{% else %} {% else %}
<a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" <a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" href="#" title="{{ _('Edit') }}" class="edit-link">
href="{{ url('comment_edit', node.id) }}" title="{{ _('Edit') }}" class="edit-link"><i <i class="fa fa-pencil fa-fw"></i>
class="fa fa-pencil fa-fw"></i></a> </a>
{% endif %} {% endif %}
<a href="javascript:" title="{{ _('Hide') }}" data-id="{{ node.id }}" class="hide-comment"><i <a href="javascript:" title="{{ _('Hide') }}" data-id="{{ node.id }}" class="hide-comment"><i
class="fa fa-trash fa-fw"></i></a> class="fa fa-trash fa-fw"></i></a>