feat: filtering for cycle and module issue and updated grouper function for grouping in modules and cycles (#342)

This commit is contained in:
pablohashescobar 2023-02-27 15:32:15 +05:30 committed by GitHub
parent 3af3bb0fb5
commit ec4332ea6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 7 deletions

View File

@ -16,6 +16,7 @@ from plane.api.serializers import CycleSerializer, CycleIssueSerializer
from plane.api.permissions import ProjectEntityPermission
from plane.db.models import Cycle, CycleIssue, Issue
from plane.bgtasks.issue_activites_task import issue_activity
from plane.utils.grouper import group_results
class CycleViewSet(BaseViewSet):
@ -52,6 +53,11 @@ class CycleIssueViewSet(BaseViewSet):
ProjectEntityPermission,
]
filterset_fields = [
"issue__labels__id",
"issue__assignees__id",
]
def perform_create(self, serializer):
serializer.save(
project_id=self.kwargs.get("project_id"),
@ -80,6 +86,31 @@ class CycleIssueViewSet(BaseViewSet):
.distinct()
)
def list(self, request, slug, project_id, cycle_id):
try:
order_by = request.GET.get("order_by", "issue__created_at")
queryset = self.get_queryset().order_by(order_by)
group_by = request.GET.get("group_by", False)
cycle_issues = CycleIssueSerializer(queryset, many=True).data
if group_by:
return Response(
group_results(cycle_issues, f"issue_detail.{group_by}"),
status=status.HTTP_200_OK,
)
return Response(
cycle_issues,
status=status.HTTP_200_OK,
)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)
def create(self, request, slug, project_id, cycle_id):
try:
issues = request.data.get("issues", [])

View File

@ -27,6 +27,7 @@ from plane.db.models import (
ModuleLink,
)
from plane.bgtasks.issue_activites_task import issue_activity
from plane.utils.grouper import group_results
class ModuleViewSet(BaseViewSet):
@ -103,8 +104,8 @@ class ModuleIssueViewSet(BaseViewSet):
model = ModuleIssue
filterset_fields = [
"issue__id",
"workspace__id",
"issue__labels__id",
"issue__assignees__id",
]
permission_classes = [
@ -140,6 +141,31 @@ class ModuleIssueViewSet(BaseViewSet):
.distinct()
)
def list(self, request, slug, project_id, cycle_id):
try:
order_by = request.GET.get("order_by", "issue__created_at")
queryset = self.get_queryset().order_by(order_by)
group_by = request.GET.get("group_by", False)
module_issues = ModuleIssueSerializer(queryset, many=True).data
if group_by:
return Response(
group_results(module_issues, f"issue_detail.{group_by}"),
status=status.HTTP_200_OK,
)
return Response(
module_issues,
status=status.HTTP_200_OK,
)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)
def create(self, request, slug, project_id, module_id):
try:
issues = request.data.get("issues", [])

View File

@ -1,12 +1,34 @@
def group_results(results_data, group_by):
def resolve_keys(group_keys, value):
"""resolve keys to a key which will be used for
grouping
Args:
group_keys (string): key which will be used for grouping
value (obj): data value
Returns:
string: the key which will be used for
"""
Utility function to group data into a given attribute.
Function can group attributes of string and list type.
keys = group_keys.split(".")
for key in keys:
value = value.get(key, None)
return value
def group_results(results_data, group_by):
"""group results data into certain group_by
Args:
results_data (obj): complete results data
group_by (key): string
Returns:
obj: grouped results
"""
response_dict = dict()
for value in results_data:
group_attribute = value.get(group_by, None)
group_attribute = resolve_keys(group_by, value)
if isinstance(group_attribute, list):
if len(group_attribute):
for attrib in group_attribute:
@ -28,4 +50,4 @@ def group_results(results_data, group_by):
response_dict[str(group_attribute)] = []
response_dict[str(group_attribute)].append(value)
return response_dict
return response_dict