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")
.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]
)

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",
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):

View file

@ -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)

View file

@ -1,5 +1,5 @@
{% 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">
<div class="comment-display{% if node.score <= vote_hide_threshold %} bad-comment{% endif %}">
<div class="info">
@ -33,12 +33,12 @@
{{ relative_time(node.time, abs=_('{time}'), rel=_('{time}')) }}
<span class="comment-spacer"></span>
<span class="comment-operation">
{% if node.revisions > 1 %}
{% if node.revision_count > 1 %}
<span class="comment-edits">
<a href="javascript:show_revision({{ node.id }}, -1)" class="previous-revision">&larr;</a>
<span class="comment-edit-text">
{% 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 %}
<a data-featherlight="{{ url('comment_edit_ajax', node.id) }}"
href="{{ url('comment_edit', node.id) }}" title="{{ _('Edit') }}" class="edit-link">
<a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" href="#" title="{{ _('Edit') }}" class="edit-link">
<i class="fa fa-pencil fa-fw"></i>
</a>
{% else %}
@ -69,9 +68,9 @@
<a href="javascript:reply_comment({{ node.id }})" title="{{ _('Reply') }}"><i
class="fa fa-reply fa-fw"></i></a>
{% else %}
<a data-featherlight="{{ url('comment_edit_ajax', node.id) }}"
href="{{ url('comment_edit', node.id) }}" title="{{ _('Edit') }}" class="edit-link"><i
class="fa fa-pencil fa-fw"></i></a>
<a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" href="#" title="{{ _('Edit') }}" class="edit-link">
<i class="fa fa-pencil fa-fw"></i>
</a>
{% endif %}
<a href="javascript:" title="{{ _('Hide') }}" data-id="{{ node.id }}" class="hide-comment"><i
class="fa fa-trash fa-fw"></i></a>