diff --git a/judge/comments.py b/judge/comments.py index 6bb0e9c..405b2ce 100755 --- a/judge/comments.py +++ b/judge/comments.py @@ -151,46 +151,86 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): return self.render_to_response(context) def get(self, request, *args, **kwargs): + pre_query = None + if "comment-id" in request.GET: + comment_id = int(request.GET["comment-id"]) + comment_obj = Comment.objects.get(pk=comment_id) + pre_query = comment_obj + while comment_obj is not None: + pre_query = comment_obj + comment_obj = comment_obj.parent self.object = self.get_object() return self.render_to_response( self.get_context_data( object=self.object, + pre_query=pre_query, comment_form=CommentForm(request, initial={"parent": None}), ) ) - def get_context_data(self, **kwargs): + 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) - queryset = ( - queryset.select_related("author__user") - .defer("author__about") - .annotate( - count_replies=Count("replies", distinct=True), - revisions=Count("versions", distinct=True), - )[:10] - ) - context["has_comments"] = queryset.exists() - context["comment_lock"] = self.is_comment_locked() + if (pre_query != None): + queryset_all = pre_query.get_descendants(include_self=True) + queryset_all = ( + queryset_all.select_related("author__user") + .filter(hidden=False) + .defer("author__about") + .annotate(revisions=Count("versions", distinct=True)) + ) + else: + queryset = ( + queryset.select_related("author__user") + .defer("author__about") + .filter(hidden=False) + .annotate( + count_replies=Count("replies", distinct=True), + revisions=Count("versions", distinct=True), + )[:10] + ) + if self.request.user.is_authenticated: profile = self.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))) + 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))) + 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 - context["comment_root_id"] = 0 - context["offset"] = 10 + 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"] = 0 + else: + context["offset"] = 10 + context["limit"] = 10 context["comment_count"] = comment_count return context diff --git a/judge/models/comment.py b/judge/models/comment.py index adeeffd..335d567 100755 --- a/judge/models/comment.py +++ b/judge/models/comment.py @@ -151,7 +151,7 @@ class Comment(MPTTModel): ) def get_absolute_url(self): - return "%s#comment-%d" % (self.link, self.id) + return "%s?comment-id=%d#comment-%d" % (self.link, self.id, self.id) class CommentVote(models.Model): diff --git a/judge/views/comment.py b/judge/views/comment.py index 87efc0c..976abd2 100755 --- a/judge/views/comment.py +++ b/judge/views/comment.py @@ -116,14 +116,12 @@ def downvote_comment(request): def get_comments(request, limit=10): try: comment_id = int(request.GET["id"]) - page_id = int(request.GET["page"]) + 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() - if not BlogPost.objects.filter(id=page_id).exists(): - raise Http404() offset = 0 if "offset" in request.GET: offset = int(request.GET["offset"]) @@ -133,13 +131,14 @@ def get_comments(request, limit=10): comment_root_id = comment_obj.id else: comment_obj = None - page_obj = BlogPost.objects.get(pk=page_id) - queryset = page_obj.comments - queryset = queryset.filter(parent=comment_obj, hidden=False) + queryset = comment_obj.linked_object.comments + if parrent_none: + queryset = queryset.filter(parent=None, hidden=False) + else: + queryset = queryset.filter(parent=comment_obj, hidden=False) comment_count = len(queryset) queryset = ( - queryset - .select_related("author__user") + queryset.select_related("author__user") .defer("author__about") .annotate( count_replies=Count("replies", distinct=True), @@ -153,7 +152,7 @@ def get_comments(request, limit=10): "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", { @@ -162,13 +161,13 @@ def get_comments(request, limit=10): "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), "limit": limit, - "comment_count": comment_count + "comment_count": comment_count, + "comment_parrent_none": parrent_none, } ) - + return HttpResponse(comment_html) def get_show_more(request): diff --git a/resources/comments.scss b/resources/comments.scss index 420c6a1..da828b4 100755 --- a/resources/comments.scss +++ b/resources/comments.scss @@ -129,14 +129,24 @@ a { .show_more_reply { font-weight: bold; - display: block; + display: flex; margin: 16px 40px; + align-items: center; + + svg { + margin-right: 4px; + } } .show_more_comment { font-weight: bold; - display: block; + display: flex; margin: 16px -40px; + align-items: center; + + svg { + margin-right: 4px; + } } } diff --git a/templates/actionbar/list.html b/templates/actionbar/list.html index 0defe94..bbed0bc 100755 --- a/templates/actionbar/list.html +++ b/templates/actionbar/list.html @@ -27,9 +27,9 @@ - ({{replies}}) + ({{ comment_count }}) {% endif %} diff --git a/templates/comments/content-list.html b/templates/comments/content-list.html index c680db1..711e632 100755 --- a/templates/comments/content-list.html +++ b/templates/comments/content-list.html @@ -1,5 +1,3 @@ -{% set logged_in = request.user.is_authenticated %} -{% set profile = request.profile if logged_in else None %} {% for node in mptt_tree(comment_list) recursive %}
- {% if replies %} + {% if comment_count %}Loading...
"); - ajax_get_reply('{{ url('comment_get_replies') }}', id, page); + ajax_get_reply('{{ url('comment_get_replies') }}', id, parrent_none); } - function ajax_get_reply(url, id, page) { + function ajax_get_reply(url, id, parrent_none) { return $.ajax({ url: url, type: 'GET', data: { id: id, - page: page, + parrent_none: parrent_none, }, success: function(data) { var $comment_loading = $("#comment-" + id + "-children .loading"); @@ -145,11 +145,12 @@ }) } - window.comment_show_more = function (id, page, offset) { - if (id == 0) { - var $comment_show_btn = $("#comment-" + id + " .show_more_comment"); + window.comment_show_more = function (id, parrent_none, offset) { + console.log(parrent_none) + if (parrent_none == 1) { + var $comment_show_btn = $("#comment-0" + " .show_more_comment"); $comment_show_btn.hide(); - var $comment = $("#comment-" + id); + var $comment = $("#comment-0"); $comment.append("Loading...
"); } else { var $comment_show_btn = $("#comment-" + id + "-children" + " .show_more_comment"); @@ -157,23 +158,23 @@ var $comment = $("#comment-" + id + "-children"); $comment.append("Loading...
"); } - ajax_comment_show_more('{{ url('comment_show_more') }}', id, page, offset); + ajax_comment_show_more('{{ url('comment_show_more') }}', id, parrent_none, offset); } - function ajax_comment_show_more(url, id, page, offset) { + function ajax_comment_show_more(url, id, parrent_none, offset) { return $.ajax({ url: url, type: 'GET', data: { id: id, - page: page, + parrent_none: parrent_none, offset: offset, }, success: function(data) { - if (id == 0) { - var $comment_loading = $("#comment-" + id + " .loading"); + if (parrent_none == 1) { + var $comment_loading = $("#comment-0" + " .loading"); $comment_loading.hide(); - var $comment = $("#comment-" + id); + var $comment = $("#comment-0"); $comment.append(data); } else { var $comment_loading = $("#comment-" + id + "-children .loading");
+
+ {% trans id=node.id %}This comment is hidden due to too much negative feedback. Click here to view it.{% endtrans %} +
+