feat: improved grouper with grouping function

This commit is contained in:
pablohashescobar 2023-02-18 15:43:47 +05:30
parent 236c660cc7
commit 495ac0ca00
2 changed files with 36 additions and 11 deletions

View File

@ -42,6 +42,7 @@ from plane.db.models import (
IssueLink, IssueLink,
) )
from plane.bgtasks.issue_activites_task import issue_activity from plane.bgtasks.issue_activites_task import issue_activity
from plane.utils.grouper import group_results
class IssueViewSet(BaseViewSet): class IssueViewSet(BaseViewSet):
@ -160,20 +161,13 @@ class IssueViewSet(BaseViewSet):
) )
issues = IssueSerializer(issue_queryset, many=True).data issues = IssueSerializer(issue_queryset, many=True).data
## Grouping the results ## Grouping the results
group_by = request.GET.get("group_by", False) group_by = request.GET.get("group_by", False)
if group_by: if group_by:
issue_dict = dict() return Response(
for key, value in groupby( group_results(issues, group_by), status=status.HTTP_200_OK
sorted( )
issues,
key=lambda issue: str(issue.get(group_by)),
),
key=lambda issue: str(issue.get(group_by)),
):
issue_dict[str(key)] = list(value)
return Response(issue_dict, status=status.HTTP_200_OK)
return Response(issues, status=status.HTTP_200_OK) return Response(issues, status=status.HTTP_200_OK)

View File

@ -0,0 +1,31 @@
def group_results(results_data, group_by):
"""
Utility function to group data into a given attribute.
Function can group attributes of string and list type.
"""
response_dict = dict()
for value in results_data:
group_attribute = value.get(group_by, None)
if isinstance(group_attribute, list):
if len(group_attribute):
for attrib in group_attribute:
if str(attrib) in response_dict:
response_dict[str(attrib)].append(value)
else:
response_dict[str(attrib)] = []
response_dict[str(attrib)].append(value)
else:
if str(None) in response_dict:
response_dict[str(None)].append(value)
else:
response_dict[str(None)] = []
response_dict[str(None)].append(value)
else:
if str(group_attribute) in response_dict:
response_dict[str(group_attribute)].append(value)
else:
response_dict[str(group_attribute)] = []
response_dict[str(group_attribute)].append(value)
return response_dict