diff --git a/taiga/projects/issues/api.py b/taiga/projects/issues/api.py index 5035418b..e1259e10 100644 --- a/taiga/projects/issues/api.py +++ b/taiga/projects/issues/api.py @@ -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"]: diff --git a/taiga/projects/userstories/api.py b/taiga/projects/userstories/api.py index 9869159a..4c4ebf59 100644 --- a/taiga/projects/userstories/api.py +++ b/taiga/projects/userstories/api.py @@ -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') diff --git a/taiga/projects/votes/utils.py b/taiga/projects/votes/utils.py index bff72a6a..f82b17b0 100644 --- a/taiga/projects/votes/utils.py +++ b/taiga/projects/votes/utils.py @@ -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) - FROM votes_votes - WHERE votes_votes.content_type_id = {type_id} - AND votes_votes.object_id = {tbl}.id""") + 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 + ) as e""" + sql = sql.format(type_id=type.id, tbl=model._meta.db_table) qs = queryset.extra(select={as_field: sql}) return qs