Fix some bugs for new comment
This commit is contained in:
parent
b5816bbcd6
commit
57ded6ff5e
8 changed files with 267 additions and 403 deletions
|
@ -28,6 +28,9 @@ from judge.widgets import HeavyPreviewPageDownWidget
|
|||
from judge.jinja2.reference import get_user_from_text
|
||||
|
||||
|
||||
DEFAULT_OFFSET = 10
|
||||
|
||||
|
||||
def add_mention_notifications(comment):
|
||||
user_referred = get_user_from_text(comment.body).exclude(id=comment.author.id)
|
||||
for user in user_referred:
|
||||
|
@ -152,92 +155,90 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
|
|||
return self.render_to_response(context)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
pre_query = None
|
||||
target_comment = None
|
||||
if "comment-id" in request.GET:
|
||||
comment_id = int(request.GET["comment-id"])
|
||||
try:
|
||||
comment_obj = Comment.objects.get(pk=comment_id)
|
||||
comment_obj = Comment.objects.get(id=comment_id)
|
||||
except Comment.DoesNotExist:
|
||||
raise Http404
|
||||
pre_query = comment_obj
|
||||
while comment_obj is not None:
|
||||
pre_query = comment_obj
|
||||
comment_obj = comment_obj.parent
|
||||
target_comment = comment_obj.get_root()
|
||||
self.object = self.get_object()
|
||||
return self.render_to_response(
|
||||
self.get_context_data(
|
||||
object=self.object,
|
||||
pre_query=pre_query,
|
||||
target_comment=target_comment,
|
||||
comment_form=CommentForm(request, initial={"parent": None}),
|
||||
)
|
||||
)
|
||||
|
||||
def get_context_data(self, pre_query=None, **kwargs):
|
||||
context = super(CommentedDetailView, self).get_context_data(**kwargs)
|
||||
queryset = self.object.comments
|
||||
queryset = queryset.filter(parent=None, hidden=False)
|
||||
queryset_all = None
|
||||
comment_count = len(queryset)
|
||||
context["comment_remove"] = -1
|
||||
if (pre_query != None):
|
||||
comment_remove = pre_query.id
|
||||
queryset_all = pre_query.get_descendants(include_self=True)
|
||||
queryset_all = (
|
||||
queryset_all.select_related("author__user")
|
||||
def _get_queryset(self, target_comment):
|
||||
if target_comment != None:
|
||||
queryset = target_comment.get_descendants(include_self=True)
|
||||
queryset = (
|
||||
queryset.select_related("author__user")
|
||||
.filter(hidden=False)
|
||||
.defer("author__about")
|
||||
.annotate(revisions=Count("versions", distinct=True))
|
||||
.annotate(
|
||||
revisions=Count("versions", distinct=True)
|
||||
)
|
||||
)
|
||||
context["comment_remove"] = comment_remove
|
||||
else:
|
||||
queryset = self.object.comments
|
||||
queryset = queryset.filter(parent=None, hidden=False)
|
||||
queryset = (
|
||||
queryset.select_related("author__user")
|
||||
.defer("author__about")
|
||||
.filter(hidden=False)
|
||||
.annotate(
|
||||
count_replies=Count("replies", distinct=True),
|
||||
count_replies=Count("replies", distinct=True),
|
||||
revisions=Count("versions", distinct=True),
|
||||
)[:10]
|
||||
)[:DEFAULT_OFFSET]
|
||||
)
|
||||
|
||||
|
||||
if self.request.user.is_authenticated:
|
||||
profile = self.request.profile
|
||||
if (pre_query != None):
|
||||
queryset_all = queryset_all.annotate(
|
||||
my_vote=FilteredRelation(
|
||||
"votes", condition=Q(votes__voter_id=profile.id)
|
||||
),
|
||||
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
|
||||
else:
|
||||
queryset = queryset.annotate(
|
||||
my_vote=FilteredRelation(
|
||||
"votes", condition=Q(votes__voter_id=profile.id)
|
||||
),
|
||||
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
|
||||
queryset = queryset.annotate(
|
||||
my_vote=FilteredRelation(
|
||||
"votes", condition=Q(votes__voter_id=profile.id)
|
||||
),
|
||||
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
|
||||
|
||||
return queryset
|
||||
|
||||
def get_context_data(self, target_comment=None, **kwargs):
|
||||
context = super(CommentedDetailView, self).get_context_data(**kwargs)
|
||||
queryset = self._get_queryset(target_comment)
|
||||
comment_count = self.object.comments.filter(parent=None, hidden=False).count()
|
||||
context["target_comment"] = -1
|
||||
if (target_comment != None):
|
||||
context["target_comment"] = target_comment.id
|
||||
|
||||
if self.request.user.is_authenticated:
|
||||
context["is_new_user"] = (
|
||||
not self.request.user.is_staff
|
||||
and not profile.submission_set.filter(
|
||||
points=F("problem__points")
|
||||
).exists()
|
||||
)
|
||||
|
||||
|
||||
context["has_comments"] = queryset.exists()
|
||||
context["comment_lock"] = self.is_comment_locked()
|
||||
context["comment_list"] = queryset
|
||||
context["comment_all_list"] = queryset_all
|
||||
|
||||
context["vote_hide_threshold"] = settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD
|
||||
if queryset.exists():
|
||||
context["comment_root_id"] = queryset[0].id
|
||||
else:
|
||||
context["comment_root_id"] = 0
|
||||
context["comment_parrent_none"] = 1
|
||||
if (pre_query != None):
|
||||
context["offset"] = 1
|
||||
context["comment_parent_none"] = 1
|
||||
if target_comment != None:
|
||||
context["offset"] = 0
|
||||
context["comment_more"] = comment_count - 1
|
||||
else:
|
||||
context["offset"] = 10
|
||||
context["offset"] = DEFAULT_OFFSET
|
||||
context["comment_more"] = comment_count - DEFAULT_OFFSET
|
||||
|
||||
context["limit"] = 10
|
||||
context["limit"] = DEFAULT_OFFSET
|
||||
context["comment_count"] = comment_count
|
||||
return context
|
||||
|
|
|
@ -110,46 +110,52 @@ 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"])
|
||||
parent_none = int(request.GET["parent_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"])
|
||||
|
||||
target_comment = -1
|
||||
if "target_comment" in request.GET:
|
||||
target_comment = int(request.GET["target_comment"])
|
||||
|
||||
comment_root_id = 0
|
||||
if (comment_id):
|
||||
|
||||
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:
|
||||
if parent_none:
|
||||
queryset = queryset.filter(parent=None, hidden=False)
|
||||
if (comment_remove != -1):
|
||||
queryset.get(pk=comment_remove).delete()
|
||||
queryset = queryset.exclude(pk=target_comment)
|
||||
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]
|
||||
)
|
||||
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(
|
||||
|
@ -158,6 +164,8 @@ def get_comments(request, limit=10):
|
|||
),
|
||||
).annotate(vote_score=Coalesce(F("my_vote__score"), Value(0)))
|
||||
|
||||
new_offset = offset + min(len(queryset), limit)
|
||||
|
||||
comment_html = loader.render_to_string(
|
||||
"comments/content-list.html",
|
||||
{
|
||||
|
@ -166,22 +174,26 @@ def get_comments(request, limit=10):
|
|||
"comment_list": queryset,
|
||||
"vote_hide_threshold" : settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD,
|
||||
"perms": PermWrapper(request.user),
|
||||
"offset": offset + min(len(queryset), limit),
|
||||
"offset": new_offset,
|
||||
"limit": limit,
|
||||
"comment_count": comment_count,
|
||||
"comment_parrent_none": parrent_none,
|
||||
"comment_remove": comment_remove,
|
||||
"comment_parent_none": parent_none,
|
||||
"target_comment": target_comment,
|
||||
"comment_more": comment_count - new_offset
|
||||
}
|
||||
)
|
||||
|
||||
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
|
||||
pk_url_kwarg = "id"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue