Change comment style (#70)

This commit is contained in:
Dung T.Bui 2023-05-22 20:52:18 +07:00 committed by GitHub
parent d80ec962a5
commit 1056a470b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 680 additions and 306 deletions

View file

@ -1,8 +1,13 @@
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.contrib.auth.context_processors import PermWrapper
from django.core.exceptions import PermissionDenied
from django.db import IntegrityError, transaction
from django.db.models import F
from django.db.models import Q, F, Count, FilteredRelation
from django.db.models.functions import Coalesce
from django.db.models.expressions import F, Value
from django.forms.models import ModelForm
from django.http import (
Http404,
@ -15,11 +20,12 @@ from django.utils.translation import gettext as _
from django.views.decorators.http import require_POST
from django.views.generic import DetailView, UpdateView
from django.urls import reverse_lazy
from django.template import loader
from reversion import revisions
from reversion.models import Version
from judge.dblock import LockModel
from judge.models import Comment, CommentVote, Notification
from judge.models import Comment, CommentVote, Notification, BlogPost
from judge.utils.views import TitleMixin
from judge.widgets import MathJaxPagedownWidget, HeavyPreviewPageDownWidget
from judge.comments import add_mention_notifications, del_mention_notifications
@ -36,6 +42,11 @@ __all__ = [
@login_required
# def get_more_reply(request, id):
# queryset = Comment.get_pk(id)
def vote_comment(request, delta):
if abs(delta) != 1:
return HttpResponseBadRequest(
@ -99,10 +110,77 @@ def vote_comment(request, delta):
def upvote_comment(request):
return vote_comment(request, 1)
def downvote_comment(request):
return vote_comment(request, -1)
def get_comments(request, limit=10):
try:
comment_id = int(request.GET["id"])
parrent_none = int(request.GET["parrent_none"])
except ValueError:
return HttpResponseBadRequest()
else:
if comment_id and not Comment.objects.filter(id=comment_id).exists():
raise Http404()
offset = 0
if "offset" in request.GET:
offset = int(request.GET["offset"])
comment_remove = -1
if "comment_remove" in request.GET:
comment_remove = int(request.GET["comment_remove"])
comment_root_id = 0
if (comment_id):
comment_obj = Comment.objects.get(pk=comment_id)
comment_root_id = comment_obj.id
else:
comment_obj = None
queryset = comment_obj.linked_object.comments
if parrent_none:
queryset = queryset.filter(parent=None, hidden=False)
if (comment_remove != -1):
queryset.get(pk=comment_remove).delete()
else:
queryset = queryset.filter(parent=comment_obj, hidden=False)
comment_count = len(queryset)
queryset = (
queryset.select_related("author__user")
.defer("author__about")
.annotate(
count_replies=Count("replies", distinct=True),
revisions=Count("versions", distinct=True),
)[offset:offset+limit]
)
if request.user.is_authenticated:
profile = request.profile
queryset = queryset.annotate(
my_vote=FilteredRelation(
"votes", condition=Q(votes__voter_id=profile.id)
),
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
comment_html = loader.render_to_string(
"comments/content-list.html",
{
"request": request,
"comment_root_id": comment_root_id,
"comment_list": queryset,
"vote_hide_threshold" : settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD,
"perms": PermWrapper(request.user),
"offset": offset + min(len(queryset), limit),
"limit": limit,
"comment_count": comment_count,
"comment_parrent_none": parrent_none,
"comment_remove": comment_remove,
}
)
return HttpResponse(comment_html)
def get_show_more(request):
return get_comments(request)
def get_replies(request):
return get_comments(request)
class CommentMixin(object):
model = Comment