Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MasterDataElementFilterViewSet(ViewSetActionPermissionMixin, viewsets.ModelViewSet):
- permission_classes = [permissions.IsAuthenticated]
- pagination_class = CustomPageNumberPagination
- serializer_class = MasterDataFilterSerializer
- def get_queryset(self):
- active_project_id = self.request.user.active_project_id
- if not active_project_id:
- raise UserHasNoActiveProjectSet(language_code=self.request.user.language.code)
- try:
- parent_id = int(self.request.query_params.get('parent_id'))
- except TypeError:
- parent_id = None
- hierarchy = get_object_or_404(
- MasterDataHierarchy.objects.all(),
- id=self.kwargs.get('pk'),
- project_id=active_project_id
- )
- if parent_id:
- return MasterDataElement.objects.filter(
- level__hierarchy_id=hierarchy.id,
- parent_id=parent_id,
- is_archived=False
- ).annotate(Count('children', distinct=True))
- else:
- try:
- statuses = loads(self.request.query_params.get('statuses', ''))
- except JSONDecodeError:
- statuses = None
- last_hierarchy_level = MasterDataLevel.get_levels_in_hierarchical_order_for_hierarchy(hierarchy.id)
- logger = logging.getLogger(__name__)
- queryset = MasterDataElement.objects.filter(
- level__id=last_hierarchy_level.last().id,
- is_archived=False,
- level__hierarchy_id=hierarchy.id,
- )
- if statuses:
- level_count = range(last_hierarchy_level.count()-1)
- field_query = '__'.join(['children' for _ in level_count]) + '__info__calculated_status_id'
- query_string = Q(**{field_query: statuses[0]})
- for status in statuses[1:]:
- query_string.add(Q(**{field_query: status}), Q.OR)
- queryset = queryset.filter(query_string)
- return queryset.select_related(
- 'info__calculated_status',
- 'info__manual_status',
- ).annotate(
- Count('children', distinct=True)
- ).order_by('-id')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement