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",
|
||||
"owner",
|
||||
"assigned_to",
|
||||
"subject")
|
||||
"subject",
|
||||
"votes_count")
|
||||
|
||||
def get_serializer_class(self, *args, **kwargs):
|
||||
if self.action in ["retrieve", "by_ref"]:
|
||||
|
|
|
@ -69,7 +69,8 @@ class UserStoryViewSet(OCCResourceMixin, VotedResourceMixin, HistoryResourceMixi
|
|||
"status__is_closed"]
|
||||
order_by_fields = ["backlog_order",
|
||||
"sprint_order",
|
||||
"kanban_order"]
|
||||
"kanban_order",
|
||||
"votes_count"]
|
||||
|
||||
# Specific filter used for filtering neighbor user stories
|
||||
_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
|
||||
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 (
|
||||
SELECT coalesce(votes_votes.count, 0) votes_count
|
||||
FROM votes_votes
|
||||
WHERE votes_votes.content_type_id = {type_id}
|
||||
AND votes_votes.object_id = {tbl}.id""")
|
||||
AND votes_votes.object_id = {tbl}.id
|
||||
) as e"""
|
||||
|
||||
sql = sql.format(type_id=type.id, tbl=model._meta.db_table)
|
||||
qs = queryset.extra(select={as_field: sql})
|
||||
return qs
|
||||
|
|
Loading…
Reference in New Issue