Esempio n. 1
0
    def group(self, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        x_column = self.request.get_argument('_x_column')
        x_lookup_name = self.request.get_argument('_x_lookup', None)
        y_func = self.request.get_argument('_y_func').lower()
        y_column = self.request.get_argument('_y_column', self.lookup_field)

        model_serializer = self.get_serializer()

        # x_serializers = list(filter(lambda x: x.field_name == x_column, model_serializer.fields))
        # x_serializer = x_serializers[0]

        # y_serializers = list(filter(lambda x: x.field_name == y_column, model_serializer.fields))
        # y_serializer = y_serializers[0]

        filter_instance = ModelGroupFilter()
        filter_instance.model = self.model
        queryset = filter_instance.filter(
            queryset, {
                'x_column': x_column,
                'x_lookup': x_lookup_name,
                'y_func': y_func,
                'y_column': y_column
            })
        serializer = ModelGroupSerializer(
            instance=queryset,
            many=True,
            # TODO: Refactor serializer
            # group_serializer=x_serializer,
            # y_func_serializer=y_serializer
        )

        return JSONResponse(serializer.representation_data)
Esempio n. 2
0
    def group(self, request, *args, **kwargs):
        queryset = self.filter_queryset(request, self.get_queryset(request))

        x_column = request.get_argument('_x_column')
        x_lookup_name = request.get_argument('_x_lookup', None)
        y_func = request.get_argument('_y_func').lower()
        y_column = request.get_argument('_y_column', self.lookup_field)

        model_serializer = self.get_serializer(request)

        # x_serializers = list(filter(lambda x: x.field_name == x_column, model_serializer.fields))
        # x_serializer = x_serializers[0]

        # y_serializers = list(filter(lambda x: x.field_name == y_column, model_serializer.fields))
        # y_serializer = y_serializers[0]

        filter_instance = ModelGroupFilter()
        filter_instance.model = self.get_model(request)
        queryset = filter_instance.filter(
            queryset, {
                'x_column': x_column,
                'x_lookup': x_lookup_name,
                'y_func': y_func,
                'y_column': y_column
            })

        try:
            instance = list(queryset)
        except SQLAlchemyError:
            queryset.session.rollback()
            raise

        serializer = ModelGroupSerializer(
            instance=instance,
            many=True,
            # TODO: Refactor serializer
            # group_serializer=x_serializer,
            # y_func_serializer=y_serializer
        )

        return JSONResponse(serializer.representation_data)