fix annotate query
This commit is contained in:
parent
c3b7d465b1
commit
dfc614ac1e
7 changed files with 70 additions and 51 deletions
|
@ -219,6 +219,7 @@ else:
|
||||||
}
|
}
|
||||||
|
|
||||||
INSTALLED_APPS += (
|
INSTALLED_APPS += (
|
||||||
|
"debug_toolbar",
|
||||||
"django.contrib.admin",
|
"django.contrib.admin",
|
||||||
"judge",
|
"judge",
|
||||||
"django.contrib.auth",
|
"django.contrib.auth",
|
||||||
|
@ -248,6 +249,7 @@ INSTALLED_APPS += (
|
||||||
)
|
)
|
||||||
|
|
||||||
MIDDLEWARE = (
|
MIDDLEWARE = (
|
||||||
|
"debug_toolbar.middleware.DebugToolbarMiddleware",
|
||||||
"judge.middleware.SlowRequestMiddleware",
|
"judge.middleware.SlowRequestMiddleware",
|
||||||
"judge.middleware.ShortCircuitMiddleware",
|
"judge.middleware.ShortCircuitMiddleware",
|
||||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||||
|
@ -424,11 +426,7 @@ STATIC_URL = "/static/"
|
||||||
CACHES = {
|
CACHES = {
|
||||||
"default": {
|
"default": {
|
||||||
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
|
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
|
||||||
"LOCATION": [
|
"LOCATION": "127.0.0.1:11211",
|
||||||
"172.19.26.240:11211",
|
|
||||||
"172.19.26.242:11212",
|
|
||||||
"172.19.26.244:11213",
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -490,3 +488,9 @@ except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
|
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
|
||||||
|
|
||||||
|
INTERNAL_IPS = [
|
||||||
|
# ...
|
||||||
|
"127.0.0.1",
|
||||||
|
# ...
|
||||||
|
]
|
||||||
|
|
|
@ -207,6 +207,7 @@ def paged_list_view(view, name, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
path('__debug__/', include('debug_toolbar.urls')),
|
||||||
url("", include("pagedown.urls")),
|
url("", include("pagedown.urls")),
|
||||||
url(
|
url(
|
||||||
r"^$",
|
r"^$",
|
||||||
|
@ -469,8 +470,8 @@ urlpatterns = [
|
||||||
url(r"^comments/upvote/$", comment.upvote_comment, name="comment_upvote"),
|
url(r"^comments/upvote/$", comment.upvote_comment, name="comment_upvote"),
|
||||||
url(r"^comments/downvote/$", comment.downvote_comment, name="comment_downvote"),
|
url(r"^comments/downvote/$", comment.downvote_comment, name="comment_downvote"),
|
||||||
url(r"^comments/hide/$", comment.comment_hide, name="comment_hide"),
|
url(r"^comments/hide/$", comment.comment_hide, name="comment_hide"),
|
||||||
url(r"^comments/reply/$", comment.get_reply, name="comment_reply"),
|
url(r"^comments/get_replies/$", comment.get_replies, name="comment_get_replies"),
|
||||||
url(r"^comments/showmore/$", comment.get_showmore, name="comment_show_more"),
|
url(r"^comments/show_more/$", comment.get_show_more, name="comment_show_more"),
|
||||||
url(
|
url(
|
||||||
r"^comments/(?P<id>\d+)/",
|
r"^comments/(?P<id>\d+)/",
|
||||||
include(
|
include(
|
||||||
|
|
|
@ -162,13 +162,15 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(CommentedDetailView, self).get_context_data(**kwargs)
|
context = super(CommentedDetailView, self).get_context_data(**kwargs)
|
||||||
queryset = self.object.comments
|
queryset = self.object.comments
|
||||||
context["replies"] = len(queryset.filter(parent=None))
|
queryset = queryset.filter(parent=None, hidden=False)
|
||||||
|
comment_count = len(queryset)
|
||||||
queryset = (
|
queryset = (
|
||||||
queryset.filter(parent=None, hidden=False)[:10]
|
queryset.select_related("author__user")
|
||||||
.select_related("author__user")
|
|
||||||
.defer("author__about")
|
.defer("author__about")
|
||||||
# .annotate(count_replies=Count("replies"))
|
.annotate(
|
||||||
# .annotate(revisions=Count("versions"))
|
count_replies=Count("replies", distinct=True),
|
||||||
|
revisions=Count("versions", distinct=True),
|
||||||
|
)[:10]
|
||||||
)
|
)
|
||||||
context["has_comments"] = queryset.exists()
|
context["has_comments"] = queryset.exists()
|
||||||
context["comment_lock"] = self.is_comment_locked()
|
context["comment_lock"] = self.is_comment_locked()
|
||||||
|
@ -190,5 +192,5 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
|
||||||
context["comment_root_id"] = 0
|
context["comment_root_id"] = 0
|
||||||
context["offset"] = 10
|
context["offset"] = 10
|
||||||
context["limit"] = 10
|
context["limit"] = 10
|
||||||
|
context["comment_count"] = comment_count
|
||||||
return context
|
return context
|
||||||
|
|
|
@ -114,12 +114,13 @@ class Comment(MPTTModel):
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def get_replies(self, ):
|
def get_replies(self):
|
||||||
return self.replies
|
query = Comment.filter(parent=self)
|
||||||
|
return len(query)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def get_revisions(self):
|
def get_revisions(self):
|
||||||
return self.versions
|
return self.versions.count()
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def page_title(self):
|
def page_title(self):
|
||||||
|
|
|
@ -113,7 +113,7 @@ def upvote_comment(request):
|
||||||
def downvote_comment(request):
|
def downvote_comment(request):
|
||||||
return vote_comment(request, -1)
|
return vote_comment(request, -1)
|
||||||
|
|
||||||
def get_comment(request, limit=10):
|
def get_comments(request, limit=10):
|
||||||
try:
|
try:
|
||||||
comment_id = int(request.GET["id"])
|
comment_id = int(request.GET["id"])
|
||||||
page_id = int(request.GET["page"])
|
page_id = int(request.GET["page"])
|
||||||
|
@ -135,12 +135,16 @@ def get_comment(request, limit=10):
|
||||||
comment_obj = None
|
comment_obj = None
|
||||||
page_obj = BlogPost.objects.get(pk=page_id)
|
page_obj = BlogPost.objects.get(pk=page_id)
|
||||||
queryset = page_obj.comments
|
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 = (
|
||||||
queryset.filter(parent=comment_obj, hidden=False)
|
queryset
|
||||||
.select_related("author__user")
|
.select_related("author__user")
|
||||||
.defer("author__about")[offset:offset+limit]
|
.defer("author__about")
|
||||||
# .annotate(revisions=Count("versions"), count_replies=Count("replies"))
|
.annotate(
|
||||||
|
count_replies=Count("replies", distinct=True),
|
||||||
|
revisions=Count("versions", distinct=True),
|
||||||
|
)[offset:offset+limit]
|
||||||
)
|
)
|
||||||
if request.user.is_authenticated:
|
if request.user.is_authenticated:
|
||||||
profile = request.profile
|
profile = request.profile
|
||||||
|
@ -160,18 +164,18 @@ def get_comment(request, limit=10):
|
||||||
"perms": PermWrapper(request.user),
|
"perms": PermWrapper(request.user),
|
||||||
"object": page_obj,
|
"object": page_obj,
|
||||||
"offset": offset + min(len(queryset), limit),
|
"offset": offset + min(len(queryset), limit),
|
||||||
"replies": replies,
|
"limit": limit,
|
||||||
"limit": limit
|
"comment_count": comment_count
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
return HttpResponse(comment_html)
|
return HttpResponse(comment_html)
|
||||||
|
|
||||||
def get_showmore(request):
|
def get_show_more(request):
|
||||||
return get_comment(request)
|
return get_comments(request)
|
||||||
|
|
||||||
def get_reply(request):
|
def get_replies(request):
|
||||||
return get_comment(request)
|
return get_comments(request)
|
||||||
|
|
||||||
class CommentMixin(object):
|
class CommentMixin(object):
|
||||||
model = Comment
|
model = Comment
|
||||||
|
|
|
@ -98,26 +98,19 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if node.revisions == 1 %}
|
{% if node.count_replies > 1 %}
|
||||||
{% set real_replies = node.count_replies - node.revisions + 1 %}
|
<a href="javascript:comment_get_replies({{ node.id }}, {{ object.id }})" class="show_more_reply"> {{ node.count_replies }} {{
|
||||||
{% else %}
|
_(' Replies ') }} </a>
|
||||||
{% set real_replies = node.count_replies - node.revisions + 2 %}
|
{% elif node.count_replies %}
|
||||||
{% endif %}
|
<a href="javascript:comment_get_replies({{ node.id }}, {{ object.id }})" class="show_more_reply"> {{ node.count_replies }} {{
|
||||||
{% if real_replies > 1 %}
|
_(' Reply ') }} </a>
|
||||||
<a href="javascript:comment_reply({{ node.id }}, {{ object.id }})" class="show_more_reply">
|
|
||||||
{{ _(' View ') }} {{ real_replies }} {{ _(' replies ') }}
|
|
||||||
</a>
|
|
||||||
{% elif real_replies %}
|
|
||||||
<a href="javascript:comment_reply({{ node.id }}, {{ object.id }})" class="show_more_reply">
|
|
||||||
{{ _(' View ') }} {{ real_replies }} {{_(' reply ') }}
|
|
||||||
</a>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</li>
|
</li>
|
||||||
<ul id="comment-{{ node.id }}-reply" class="reply-comment" hidden></ul>
|
<ul id="comment-{{ node.id }}-reply" class="reply-comment" hidden></ul>
|
||||||
<ul id="comment-{{ node.id }}-children" class="comments"> </ul>
|
<ul id="comment-{{ node.id }}-children" class="comments"> </ul>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
{% if replies - offset > 0 %}
|
{% if comment_count - offset > 0 %}
|
||||||
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ object.id }}, {{ offset }} )" class="show_more_comment">
|
<a href="javascript:comment_show_more({{ comment_root_id }}, {{ object.id }}, {{ offset }} )" class="show_more_comment">
|
||||||
{{ _(' View ') }} {{ replies - offset }} {{ _(' comments more ') }}</a>
|
{{ comment_count - offset }} replies more</a>
|
||||||
{% endif %}
|
{% endif %}
|
|
@ -120,8 +120,12 @@
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
window.comment_reply = function (id, page) {
|
window.comment_get_replies = function (id, page) {
|
||||||
ajax_get_reply('{{ url('comment_reply') }}', id, page);
|
var $comment_show_btn = $("#comment-" + id + " .show_more_reply");
|
||||||
|
$comment_show_btn.hide();
|
||||||
|
var $comment = $("#comment-" + id + "-children");
|
||||||
|
$comment.append("<p class='loading'> Loading... </p>");
|
||||||
|
ajax_get_reply('{{ url('comment_get_replies') }}', id, page);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ajax_get_reply(url, id, page) {
|
function ajax_get_reply(url, id, page) {
|
||||||
|
@ -133,16 +137,26 @@
|
||||||
page: page,
|
page: page,
|
||||||
},
|
},
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
|
var $comment_loading = $("#comment-" + id + "-children .loading");
|
||||||
|
$comment_loading.hide();
|
||||||
var $comment = $("#comment-" + id + "-children");
|
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);
|
$comment.append(data);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
window.comment_show_more = function (id, page, offset) {
|
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("<p class='loading'> Loading... </p>");
|
||||||
|
} else {
|
||||||
|
var $comment_show_btn = $("#comment-" + id + "-children" + " .show_more_comment");
|
||||||
|
$comment_show_btn.hide();
|
||||||
|
var $comment = $("#comment-" + id + "-children");
|
||||||
|
$comment.append("<p class='loading'> Loading... </p>");
|
||||||
|
}
|
||||||
ajax_comment_show_more('{{ url('comment_show_more') }}', id, page, offset);
|
ajax_comment_show_more('{{ url('comment_show_more') }}', id, page, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,14 +171,14 @@
|
||||||
},
|
},
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
|
var $comment_loading = $("#comment-" + id + " .loading");
|
||||||
|
$comment_loading.hide();
|
||||||
var $comment = $("#comment-" + id);
|
var $comment = $("#comment-" + id);
|
||||||
var $comment_show_btn = $("#comment-" + id + " .show_more_comment");
|
|
||||||
$comment_show_btn.hide();
|
|
||||||
$comment.append(data);
|
$comment.append(data);
|
||||||
} else {
|
} else {
|
||||||
|
var $comment_loading = $("#comment-" + id + "-children .loading");
|
||||||
|
$comment_loading.hide();
|
||||||
var $comment = $("#comment-" + id + "-children");
|
var $comment = $("#comment-" + id + "-children");
|
||||||
var $comment_show_btn = $("#comment-" + id + "-children" + " .show_more_comment");
|
|
||||||
$comment_show_btn.hide();
|
|
||||||
$comment.append(data);
|
$comment.append(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue