Ejemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        params = request.query_params

        aggregations = params.get('aggregations', None)
        groupings = params.get('group_by', None)
        orderings = params.get('order_by', None)

        if not groupings:
            return Response({
                'error_message':
                "Invalid value for mandatory field 'group_by'"
            })
        elif not aggregations:
            return Response({
                'error_message':
                "Invalid value for mandatory field 'aggregations'"
            })

        aggregations = aggregations.split(',')
        groupings = groupings.split(',')
        if orderings:
            orderings = orderings.split(',')

        selected_groupings = list(
            filter(lambda x: x.query_param in groupings,
                   self.allowed_groupings))

        selected_aggregations = list(
            filter(lambda x: x.query_param in aggregations,
                   self.allowed_aggregations))

        selected_orderings = orderings

        result = aggregate(
            queryset,
            request,
            selected_groupings,
            selected_aggregations,
            selected_orderings,
        )

        page_size = params.get('page_size', None)
        page = params.get('page', None)

        result['results'] = \
            self.apply_limit_offset_filters(result['results'], page_size, page)

        # prevent on the Response
        # so can not direct to Response(result) if format apa oor None
        if self.request.GET.get('format', None) in ['api', None]:
            return Response({
                'count': result['count'],
                'result': result['results']
            })

        return Response(result)
Ejemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        params = request.query_params

        aggregations = params.get('aggregations', None)
        groupings = params.get('group_by', None)
        orderings = params.get('order_by', None)

        if not groupings:
            return Response({
                'error_message':
                    "Invalid value for mandatory field 'group_by'"})
        elif not aggregations:
            return Response({
                'error_message':
                    "Invalid value for mandatory field 'aggregations'"})

        aggregations = aggregations.split(',')
        groupings = groupings.split(',')
        if orderings:
            orderings = orderings.split(',')

        selected_groupings = list(filter(
            lambda x: x.query_param in groupings,
            self.allowed_groupings
        ))

        selected_aggregations = list(filter(
            lambda x: x.query_param in aggregations,
            self.allowed_aggregations
        ))

        selected_orderings = orderings

        result = aggregate(
            queryset,
            request,
            selected_groupings,
            selected_aggregations,
            selected_orderings,
        )

        page_size = params.get('page_size', None)
        page = params.get('page', None)

        result['results'] = \
            self.apply_limit_offset_filters(result['results'], page_size, page)

        # prevent on the Response
        # so can not direct to Response(result) if format apa oor None
        if self.request.GET.get('format', None) in ['api', None]:
            return Response(
                {'count': result['count'], 'result': result['results']}
            )

        return Response(result)
Ejemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        params = request.query_params
        #temp_params = request.query_params
        #params = temp_params.copy()
        #params.pop('indicator', None)
        #params.pop(' technology', None)

        aggregations = filter(None, params.get('aggregations', "").split(','))
        groupings = filter(None, params.get('group_by', "").split(','))
        orderings = filter(None, params.get('order_by', "").split(','))

        if not len(groupings):
            return Response({
                'error_message':
                "Invalid value for mandatory field 'group_by'"
            })
        elif not len(aggregations):
            return Response({
                'error_message':
                "Invalid value for mandatory field 'aggregations'"
            })

        selected_groupings = filter(lambda x: x.query_param in groupings,
                                    self.allowed_groupings)

        selected_aggregations = filter(lambda x: x.query_param in aggregations,
                                       self.allowed_aggregations)

        # selected_orderings = filter(
        #     lambda x: x.query_param in orderings or '-' + x.query_param in orderings,
        #     self.allowed_groupings + self.allowed_aggregations
        # )
        selected_orderings = orderings

        result = aggregate(
            queryset,
            request,
            selected_groupings,
            selected_aggregations,
            selected_orderings,
        )

        page_size = params.get('page_size', None)
        page = params.get('page', None)

        result['results'] = self.apply_limit_offset_filters(
            result['results'], page_size, page)
        return Response(result)