π₯π₯π₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.
TypeScript boardsbug-trackerdjangodockerganttissue-trackerjirajira-alternativekanbanlinearpostgresqlproduct-managementproject-managementproject-planningpythonreactredistypesciptvitework-management
[WEB - 1604]fix: pagination on many to many fields when grouping and sub grouping (#4818)
Nikhil2a740b9cd970
diff --git a/apiserver/plane/utils/paginator.py b/apiserver/plane/utils/paginator.pyindex 8bea4746ae..3ea74bf9be 100644--- a/apiserver/plane/utils/paginator.py+++ b/apiserver/plane/utils/paginator.py@@ -187,11 +187,11 @@ class OffsetPaginator:class GroupedOffsetPaginator(OffsetPaginator):- # Field mappers+ # Field mappers - list m2m fields hereFIELD_MAPPER = {"labels__id": "label_ids","assignees__id": "assignee_ids",- "modules__id": "module_ids",+ "issue_module__module_id": "module_ids",}def __init__(@@ -205,8 +205,12 @@ class GroupedOffsetPaginator(OffsetPaginator):):# Initiate the parent class for all the parameterssuper().__init__(queryset, *args, **kwargs)++ # Set the group by field nameself.group_by_field_name = group_by_field_name+ # Set the group by fieldsself.group_by_fields = group_by_fields+ # Set the count filter - this are extra filters that need to be passed to calculate the counts with the filtersself.count_filter = count_filterdef get_result(self, limit=50, cursor=None):@@ -224,8 +228,11 @@ class GroupedOffsetPaginator(OffsetPaginator):offset = cursor.offset * cursor.valuestop = offset + (cursor.value or limit) + 1+ # Check if the offset is greater than the max offsetif self.max_offset is not None and offset >= self.max_offset:raise BadPaginationError("Pagination offset too large")++ # Check if the offset is less than 0if offset < 0:raise BadPaginationError("Pagination offset cannot be negative")@@ -269,6 +276,8 @@ class GroupedOffsetPaginator(OffsetPaginator):False,queryset.filter(row_number__gte=stop).exists(),)++ # Add previous cursorsprev_cursor = Cursor(limit,page - 1,@@ -305,7 +314,7 @@ class GroupedOffsetPaginator(OffsetPaginator):)def __get_total_queryset(self):- # Get total queryset+ # Get total items for each groupreturn (self.queryset.values(self.group_by_field_name).annotate(@@ -328,7 +337,6 @@ class GroupedOffsetPaginator(OffsetPaginator):)+ (1 if group.get("count") == 0 else group.get("count")))-return total_group_dictdef __get_field_dict(self):@@ -353,7 +361,7 @@ class GroupedOffsetPaginator(OffsetPaginator):# Grouping for m2m valuestotal_group_dict = self.__get_total_dict()- # Preparing a dict to keep track of group IDs associated with each label ID+ # Preparing a dict to keep track of group IDs associated with each entity IDresult_group_mapping = defaultdict(set)# Preparing a dict to group result by group IDgrouped_by_field_name = defaultdict(list)@@ -390,7 +398,7 @@ class GroupedOffsetPaginator(OffsetPaginator):return processed_resultsdef __query_grouper(self, results):- # Grouping for single values+ # Grouping for values that are not m2mprocessed_results = self.__get_field_dict()for result in results:group_value = str(result.get(self.group_by_field_name))@@ -411,10 +419,11 @@ class GroupedOffsetPaginator(OffsetPaginator):class SubGroupedOffsetPaginator(OffsetPaginator):+ # Field mappers this are the fields that are m2mFIELD_MAPPER = {"labels__id": "label_ids","assignees__id": "assignee_ids",- "modules__id": "module_ids",+ "issue_module__module_id": "module_ids",}def __init__(@@ -428,11 +437,18 @@ class SubGroupedOffsetPaginator(OffsetPaginator):*args,**kwargs,):+ # Initiate the parent class for all the parameterssuper().__init__(queryset, *args, **kwargs)++ # Set the group by field nameself.group_by_field_name = group_by_field_nameself.group_by_fields = group_by_fields++ # Set the sub group by field nameself.sub_group_by_field_name = sub_group_by_field_nameself.sub_group_by_fields = sub_group_by_fields++ # Set the count filter - this are extra filters that need to be passed to calculate the counts with the filtersself.count_filter = count_filterdef get_result(self, limit=30, cursor=None):@@ -441,13 +457,19 @@ class SubGroupedOffsetPaginator(OffsetPaginator):if cursor is None:cursor = Cursor(0, 0, 0)+ # get the minimum valuelimit = min(limit, self.max_limit)# Adjust the initial offset and stop based on the cursor and limitqueryset = self.queryset+ # the current pagepage = cursor.offset++ # the offsetoffset = cursor.offset * cursor.value++ # the stopstop = offset + (cursor.value or limit) + 1if self.max_offset is not None and offset >= self.max_offset:@@ -496,6 +518,8 @@ class SubGroupedOffsetPaginator(OffsetPaginator):False,queryset.filter(row_number__gte=stop).exists(),)++ # Add previous cursorsprev_cursor = Cursor(limit,page - 1,@@ -579,19 +603,24 @@ class SubGroupedOffsetPaginator(OffsetPaginator):subgroup = str(item[self.sub_group_by_field_name])count = item["count"]+ # Create a dictionary of group and sub groupif group not in total_sub_group_dict:total_sub_group_dict[str(group)] = {}+ # Create a dictionary of sub groupif subgroup not in total_sub_group_dict[group]:total_sub_group_dict[str(group)][str(subgroup)] = {}+ # Create a nested dictionary of group and sub grouptotal_sub_group_dict[group][subgroup] = countreturn total_group_dict, total_sub_group_dictdef __get_field_dict(self):+ # Create a field dictionarytotal_group_dict, total_sub_group_dict = self.__get_total_dict()+ # Create a dictionary of group and sub groupreturn {str(group): {"results": {@@ -621,7 +650,6 @@ class SubGroupedOffsetPaginator(OffsetPaginator):result_id = result["id"]group_id = result[self.group_by_field_name]result_group_mapping[str(result_id)].add(str(group_id))-# Use the same calculation for the sub groupif self.sub_group_by_field_name in self.FIELD_MAPPER:for result in results:@@ -635,6 +663,9 @@ class SubGroupedOffsetPaginator(OffsetPaginator):group_value = str(result.get(self.group_by_field_name))# Get the sub group valuesub_group_value = str(result.get(self.sub_group_by_field_name))+ # Check if the group value is in the processed results+ result_id = result["id"]+if (group_value in processed_resultsand sub_group_value@@ -647,12 +678,14 @@ class SubGroupedOffsetPaginator(OffsetPaginator):[] if "None" in group_ids else group_ids)if self.sub_group_by_field_name in self.FIELD_MAPPER:- sub_group_ids = list(result_group_mapping[str(result_id)])- # for multi groups- result[self.FIELD_MAPPER.get(self.group_by_field_name)] = (- [] if "None" in sub_group_ids else sub_group_ids+ sub_group_ids = list(+ result_sub_group_mapping[str(result_id)])-+ # for multi groups+ result[+ self.FIELD_MAPPER.get(self.sub_group_by_field_name)+ ] = ([] if "None" in sub_group_ids else sub_group_ids)+ # If a result belongs to multiple groups, add it to each groupprocessed_results[str(group_value)]["results"][str(sub_group_value)]["results"].append(result)@@ -677,8 +710,10 @@ class SubGroupedOffsetPaginator(OffsetPaginator):self.group_by_field_name in self.FIELD_MAPPERor self.sub_group_by_field_name in self.FIELD_MAPPER):+ # if the grouping is done through m2m thenprocessed_results = self.__query_multi_grouper(results=results)else:+ # group it directlyprocessed_results = self.__query_grouper(results=results)else:processed_results = {}