change comment style
This commit is contained in:
parent
2bd0e41653
commit
d7672cf81b
9 changed files with 272 additions and 124 deletions
|
@ -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
|
||||
|
@ -34,6 +40,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(
|
||||
|
@ -97,10 +108,67 @@ 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_comment(request, limit=10):
|
||||
try:
|
||||
comment_id = int(request.GET["id"])
|
||||
page_id = int(request.GET["page"])
|
||||
except ValueError:
|
||||
return HttpResponseBadRequest()
|
||||
else:
|
||||
if comment_id and not Comment.objects.filter(id=comment_id).exists():
|
||||
raise Http404()
|
||||
if not BlogPost.objects.filter(id=page_id).exists():
|
||||
raise Http404()
|
||||
offset = 0
|
||||
if "offset" in request.GET:
|
||||
offset = int(request.GET["offset"])
|
||||
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
|
||||
page_obj = BlogPost.objects.get(pk=page_id)
|
||||
queryset = page_obj.comments
|
||||
replies = len(queryset.filter(parent=comment_obj))
|
||||
queryset = (
|
||||
queryset.filter(parent=comment_obj)[offset:offset+limit].select_related("author__user")
|
||||
.defer("author__about")
|
||||
.annotate(revisions=Count("versions")).annotate(count_replies=Count("replies"))
|
||||
)
|
||||
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),
|
||||
"object": page_obj,
|
||||
"offset": offset + min(len(queryset), limit),
|
||||
"replies": replies,
|
||||
"limit": limit
|
||||
}
|
||||
)
|
||||
|
||||
return HttpResponse(comment_html)
|
||||
|
||||
def get_showmore(request):
|
||||
return get_comment(request)
|
||||
|
||||
def get_reply(request):
|
||||
return get_comment(request)
|
||||
|
||||
class CommentMixin(object):
|
||||
model = Comment
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue