diff --git a/dmoj/settings.py b/dmoj/settings.py index 2f206d3..80c883a 100755 --- a/dmoj/settings.py +++ b/dmoj/settings.py @@ -219,6 +219,7 @@ else: } INSTALLED_APPS += ( + "debug_toolbar", "django.contrib.admin", "judge", "django.contrib.auth", @@ -248,6 +249,7 @@ INSTALLED_APPS += ( ) MIDDLEWARE = ( + "debug_toolbar.middleware.DebugToolbarMiddleware", "judge.middleware.SlowRequestMiddleware", "judge.middleware.ShortCircuitMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", @@ -424,11 +426,7 @@ STATIC_URL = "/static/" CACHES = { "default": { "BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache", - "LOCATION": [ - "172.19.26.240:11211", - "172.19.26.242:11212", - "172.19.26.244:11213", - ], + "LOCATION": "127.0.0.1:11211", } } @@ -490,3 +488,9 @@ except IOError: pass DEFAULT_AUTO_FIELD = "django.db.models.AutoField" + +INTERNAL_IPS = [ + # ... + "127.0.0.1", + # ... +] diff --git a/dmoj/urls.py b/dmoj/urls.py index 913e63f..0de5356 100755 --- a/dmoj/urls.py +++ b/dmoj/urls.py @@ -207,6 +207,7 @@ def paged_list_view(view, name, **kwargs): urlpatterns = [ + path('__debug__/', include('debug_toolbar.urls')), url("", include("pagedown.urls")), url( r"^$", @@ -469,8 +470,8 @@ urlpatterns = [ url(r"^comments/upvote/$", comment.upvote_comment, name="comment_upvote"), url(r"^comments/downvote/$", comment.downvote_comment, name="comment_downvote"), url(r"^comments/hide/$", comment.comment_hide, name="comment_hide"), - url(r"^comments/reply/$", comment.get_reply, name="comment_reply"), - url(r"^comments/showmore/$", comment.get_showmore, name="comment_show_more"), + url(r"^comments/get_replies/$", comment.get_replies, name="comment_get_replies"), + url(r"^comments/show_more/$", comment.get_show_more, name="comment_show_more"), url( r"^comments/(?P\d+)/", include( diff --git a/judge/comments.py b/judge/comments.py index 96a5904..6bb0e9c 100755 --- a/judge/comments.py +++ b/judge/comments.py @@ -162,13 +162,15 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): def get_context_data(self, **kwargs): context = super(CommentedDetailView, self).get_context_data(**kwargs) queryset = self.object.comments - context["replies"] = len(queryset.filter(parent=None)) + queryset = queryset.filter(parent=None, hidden=False) + comment_count = len(queryset) queryset = ( - queryset.filter(parent=None, hidden=False)[:10] - .select_related("author__user") + queryset.select_related("author__user") .defer("author__about") - # .annotate(count_replies=Count("replies")) - # .annotate(revisions=Count("versions")) + .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() @@ -190,5 +192,5 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View): context["comment_root_id"] = 0 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 f4b6f01..adeeffd 100755 --- a/judge/models/comment.py +++ b/judge/models/comment.py @@ -114,12 +114,13 @@ class Comment(MPTTModel): return output @cached_property - def get_replies(self, ): - return self.replies + def get_replies(self): + query = Comment.filter(parent=self) + return len(query) @cached_property def get_revisions(self): - return self.versions + return self.versions.count() @cached_property def page_title(self): diff --git a/judge/views/comment.py b/judge/views/comment.py index 321a058..87efc0c 100755 --- a/judge/views/comment.py +++ b/judge/views/comment.py @@ -113,7 +113,7 @@ def upvote_comment(request): def downvote_comment(request): return vote_comment(request, -1) -def get_comment(request, limit=10): +def get_comments(request, limit=10): try: comment_id = int(request.GET["id"]) page_id = int(request.GET["page"]) @@ -135,12 +135,16 @@ def get_comment(request, limit=10): 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, hidden=False) + comment_count = len(queryset) queryset = ( - queryset.filter(parent=comment_obj, hidden=False) + queryset .select_related("author__user") - .defer("author__about")[offset:offset+limit] - # .annotate(revisions=Count("versions"), count_replies=Count("replies")) + .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 @@ -160,18 +164,18 @@ def get_comment(request, limit=10): "perms": PermWrapper(request.user), "object": page_obj, "offset": offset + min(len(queryset), limit), - "replies": replies, - "limit": limit + "limit": limit, + "comment_count": comment_count } ) return HttpResponse(comment_html) -def get_showmore(request): - return get_comment(request) +def get_show_more(request): + return get_comments(request) -def get_reply(request): - return get_comment(request) +def get_replies(request): + return get_comments(request) class CommentMixin(object): model = Comment diff --git a/templates/comments/content-list.html b/templates/comments/content-list.html index ede2e0d..c680db1 100755 --- a/templates/comments/content-list.html +++ b/templates/comments/content-list.html @@ -98,26 +98,19 @@ - {% if node.revisions == 1 %} - {% set real_replies = node.count_replies - node.revisions + 1 %} - {% else %} - {% set real_replies = node.count_replies - node.revisions + 2 %} - {% endif %} - {% if real_replies > 1 %} - - {{ _(' View ') }} {{ real_replies }} {{ _(' replies ') }} - - {% elif real_replies %} - - {{ _(' View ') }} {{ real_replies }} {{_(' reply ') }} - + {% if node.count_replies > 1 %} + {{ node.count_replies }} {{ + _(' Replies ') }} + {% elif node.count_replies %} + {{ node.count_replies }} {{ + _(' Reply ') }} {% endif %} {% endfor %} -{% if replies - offset > 0 %} +{% if comment_count - offset > 0 %} - {{ _(' View ') }} {{ replies - offset }} {{ _(' comments more ') }} + {{ comment_count - offset }} replies more {% endif %} \ No newline at end of file diff --git a/templates/comments/media-js.html b/templates/comments/media-js.html index c825b26..8604785 100755 --- a/templates/comments/media-js.html +++ b/templates/comments/media-js.html @@ -120,8 +120,12 @@ }); } - window.comment_reply = function (id, page) { - ajax_get_reply('{{ url('comment_reply') }}', id, page); + window.comment_get_replies = function (id, page) { + var $comment_show_btn = $("#comment-" + id + " .show_more_reply"); + $comment_show_btn.hide(); + var $comment = $("#comment-" + id + "-children"); + $comment.append("

Loading...

"); + ajax_get_reply('{{ url('comment_get_replies') }}', id, page); } function ajax_get_reply(url, id, page) { @@ -133,16 +137,26 @@ page: page, }, success: function(data) { + var $comment_loading = $("#comment-" + id + "-children .loading"); + $comment_loading.hide(); var $comment = $("#comment-" + id + "-children"); - var $comment_show_btn = $("#comment-" + id + " .show_more_reply"); - console.log($comment_show_btn); - $comment_show_btn.hide(); $comment.append(data); } }) } window.comment_show_more = function (id, page, offset) { + if (id == 0) { + var $comment_show_btn = $("#comment-" + id + " .show_more_comment"); + $comment_show_btn.hide(); + var $comment = $("#comment-" + id); + $comment.append("

Loading...

"); + } else { + var $comment_show_btn = $("#comment-" + id + "-children" + " .show_more_comment"); + $comment_show_btn.hide(); + var $comment = $("#comment-" + id + "-children"); + $comment.append("

Loading...

"); + } ajax_comment_show_more('{{ url('comment_show_more') }}', id, page, offset); } @@ -157,14 +171,14 @@ }, success: function(data) { if (id == 0) { + var $comment_loading = $("#comment-" + id + " .loading"); + $comment_loading.hide(); var $comment = $("#comment-" + id); - var $comment_show_btn = $("#comment-" + id + " .show_more_comment"); - $comment_show_btn.hide(); $comment.append(data); } else { + var $comment_loading = $("#comment-" + id + "-children .loading"); + $comment_loading.hide(); var $comment = $("#comment-" + id + "-children"); - var $comment_show_btn = $("#comment-" + id + "-children" + " .show_more_comment"); - $comment_show_btn.hide(); $comment.append(data); } }