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 += (
|
||||
"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",
|
||||
# ...
|
||||
]
|
||||
|
|
|
@ -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<id>\d+)/",
|
||||
include(
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -98,26 +98,19 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{% 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 %}
|
||||
<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>
|
||||
{% if node.count_replies > 1 %}
|
||||
<a href="javascript:comment_get_replies({{ node.id }}, {{ object.id }})" class="show_more_reply"> {{ node.count_replies }} {{
|
||||
_(' Replies ') }} </a>
|
||||
{% elif node.count_replies %}
|
||||
<a href="javascript:comment_get_replies({{ node.id }}, {{ object.id }})" class="show_more_reply"> {{ node.count_replies }} {{
|
||||
_(' Reply ') }} </a>
|
||||
{% endif %}
|
||||
</li>
|
||||
<ul id="comment-{{ node.id }}-reply" class="reply-comment" hidden></ul>
|
||||
<ul id="comment-{{ node.id }}-children" class="comments"> </ul>
|
||||
{% 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">
|
||||
{{ _(' View ') }} {{ replies - offset }} {{ _(' comments more ') }}</a>
|
||||
{{ comment_count - offset }} replies more</a>
|
||||
{% endif %}
|
|
@ -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("<p class='loading'> Loading... </p>");
|
||||
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("<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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue