From 495ac0ca0079a72aefbbe0e3f2edc4c07f3320be Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Sat, 18 Feb 2023 15:43:47 +0530 Subject: [PATCH] feat: improved grouper with grouping function --- apiserver/plane/api/views/issue.py | 16 +++++---------- apiserver/plane/utils/grouper.py | 31 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 apiserver/plane/utils/grouper.py diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index 3c621319b..9f35aeb69 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -42,6 +42,7 @@ from plane.db.models import ( IssueLink, ) from plane.bgtasks.issue_activites_task import issue_activity +from plane.utils.grouper import group_results class IssueViewSet(BaseViewSet): @@ -160,20 +161,13 @@ class IssueViewSet(BaseViewSet): ) issues = IssueSerializer(issue_queryset, many=True).data + ## Grouping the results group_by = request.GET.get("group_by", False) - if group_by: - issue_dict = dict() - for key, value in groupby( - 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( + group_results(issues, group_by), status=status.HTTP_200_OK + ) return Response(issues, status=status.HTTP_200_OK) diff --git a/apiserver/plane/utils/grouper.py b/apiserver/plane/utils/grouper.py new file mode 100644 index 000000000..51c1f61c2 --- /dev/null +++ b/apiserver/plane/utils/grouper.py @@ -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 \ No newline at end of file