Adding votes_count sorting to issues and userstories API
parent
411441c84e
commit
0d21f04a87
|
@ -75,7 +75,8 @@ class IssueViewSet(OCCResourceMixin, VotedResourceMixin, HistoryResourceMixin, W
|
||||||
"modified_date",
|
"modified_date",
|
||||||
"owner",
|
"owner",
|
||||||
"assigned_to",
|
"assigned_to",
|
||||||
"subject")
|
"subject",
|
||||||
|
"votes_count")
|
||||||
|
|
||||||
def get_serializer_class(self, *args, **kwargs):
|
def get_serializer_class(self, *args, **kwargs):
|
||||||
if self.action in ["retrieve", "by_ref"]:
|
if self.action in ["retrieve", "by_ref"]:
|
||||||
|
|
|
@ -69,7 +69,8 @@ class UserStoryViewSet(OCCResourceMixin, VotedResourceMixin, HistoryResourceMixi
|
||||||
"status__is_closed"]
|
"status__is_closed"]
|
||||||
order_by_fields = ["backlog_order",
|
order_by_fields = ["backlog_order",
|
||||||
"sprint_order",
|
"sprint_order",
|
||||||
"kanban_order"]
|
"kanban_order",
|
||||||
|
"votes_count"]
|
||||||
|
|
||||||
# Specific filter used for filtering neighbor user stories
|
# Specific filter used for filtering neighbor user stories
|
||||||
_neighbor_tags_filter = filters.TagsFilter('neighbor_tags')
|
_neighbor_tags_filter = filters.TagsFilter('neighbor_tags')
|
||||||
|
|
|
@ -34,10 +34,13 @@ def attach_votes_count_to_queryset(queryset, as_field="votes_count"):
|
||||||
"""
|
"""
|
||||||
model = queryset.model
|
model = queryset.model
|
||||||
type = apps.get_model("contenttypes", "ContentType").objects.get_for_model(model)
|
type = apps.get_model("contenttypes", "ContentType").objects.get_for_model(model)
|
||||||
sql = ("""SELECT coalesce(votes_votes.count, 0)
|
sql = """SELECT coalesce(SUM(votes_count), 0) FROM (
|
||||||
FROM votes_votes
|
SELECT coalesce(votes_votes.count, 0) votes_count
|
||||||
WHERE votes_votes.content_type_id = {type_id}
|
FROM votes_votes
|
||||||
AND votes_votes.object_id = {tbl}.id""")
|
WHERE votes_votes.content_type_id = {type_id}
|
||||||
|
AND votes_votes.object_id = {tbl}.id
|
||||||
|
) as e"""
|
||||||
|
|
||||||
sql = sql.format(type_id=type.id, tbl=model._meta.db_table)
|
sql = sql.format(type_id=type.id, tbl=model._meta.db_table)
|
||||||
qs = queryset.extra(select={as_field: sql})
|
qs = queryset.extra(select={as_field: sql})
|
||||||
return qs
|
return qs
|
||||||
|
|
Loading…
Reference in New Issue