示例#1
0
    def clean_request_params(self, request):
        request_params = request.request_params
        request_filters = {}
        for filter_name in request_params.keys():
            field_name, filter_type = filter_operators. \
                split_to_field_and_filter_type(filter_name)

            cleaned_field_value = self._get_filter_value(
                request, field_name, request_params[filter_name], filter_type)

            attribute_name = self._get_attribute_name(field_name)
            if filter_type is not None:
                attribute_name = filter_operators.OPERATOR_SEPARATOR.join(
                    [attribute_name, filter_type])

            request_filters[attribute_name] = cleaned_field_value

        if 'order_by' in request_filters:
            order_by = filter_operators.transform_to_list(
                request_filters['order_by'])
            request_filters['order_by'] = \
                [self._get_attribute_name(field_name)
                 for field_name in order_by]
        if 'aggregate_by' in request_filters:
            aggregate_by = filter_operators.transform_to_list(
                request_filters['aggregate_by'])
            request_filters['aggregate_by'] = \
                [self._get_attribute_name(field_name)
                 for field_name in aggregate_by]

        return request_filters
    def clean_request_params(self, request):
        request_params = request.request_params
        request_filters = {}
        for filter_name in request_params.keys():
            field_name, filter_type = filter_operators. \
                split_to_field_and_filter_type(filter_name)

            cleaned_field_value = self._get_filter_value(
                request, field_name, request_params[filter_name], filter_type)

            attribute_name = self._get_attribute_name(field_name)
            if filter_type is not None:
                attribute_name = filter_operators.OPERATOR_SEPARATOR.join(
                    [attribute_name, filter_type])

            request_filters[attribute_name] = cleaned_field_value

        if 'order_by' in request_filters:
            order_by = filter_operators.transform_to_list(request_filters[
                'order_by'])
            request_filters['order_by'] = \
                [self._get_attribute_name(field_name)
                 for field_name in order_by]
        if 'aggregate_by' in request_filters:
            aggregate_by = filter_operators.transform_to_list(request_filters[
                'aggregate_by'])
            request_filters['aggregate_by'] = \
                [self._get_attribute_name(field_name)
                 for field_name in aggregate_by]
            
        return request_filters
    def get_fields_to_serialize(self, request):
        schema_fields = self.schema_cls._meta.fields
        if request.context_params.get("crud_action") == CrudActions.READ_LIST:
            return self.schema_cls.list_fields()

        elif request.context_params.get("crud_action") == CrudActions.GET_AGGREGATES:
            aggregate_by_fields = filter_operators.transform_to_list(request.request_params.get("aggregate_by", []))
            return {key: schema_fields[key] for key in aggregate_by_fields}
        return schema_fields
示例#4
0
    def get_fields_to_serialize(self, request):
        schema_fields = self.schema_cls._meta.fields
        if request.context_params.get('crud_action') == CrudActions.READ_LIST:
            return self.schema_cls.list_fields()

        elif request.context_params.get('crud_action') == \
                CrudActions.GET_AGGREGATES:
            aggregate_by_fields = filter_operators.transform_to_list(
                request.request_params.get('aggregate_by', []))
            return {key: schema_fields[key] for key in aggregate_by_fields}
        return schema_fields
    def validate_aggregate_by(self, request_params):
        aggregate_by_params = request_params.get('aggregate_by', [])
        aggregate_by_params = filter_operators.transform_to_list(
            aggregate_by_params)

        validation_errors = {}
        for field_name in aggregate_by_params:
            if field_name not in self.aggregate_by_fields:
                validation_errors.update(
                    {field_name: "Aggregating by this field is not allowed"})
        if validation_errors:
            return validation_errors
        return None
    def validate_aggregate_by(self, request_params):
        aggregate_by_params = request_params.get('aggregate_by', [])
        aggregate_by_params = filter_operators.transform_to_list(
            aggregate_by_params)

        validation_errors = {}
        for field_name in aggregate_by_params:
            if field_name not in self.aggregate_by_fields:
                validation_errors.update(
                    {field_name: "Aggregating by this field is not allowed"})
        if validation_errors:
            return validation_errors
        return None
    def validate_order_by(self, request_params):
        order_by_params = request_params.get('order_by', [])
        order_by_params = filter_operators.transform_to_list(order_by_params)

        validation_errors = {}
        for order_by_field_name in order_by_params:
            field_name, order_by_type = filter_operators. \
                split_to_field_and_order_type(order_by_field_name)
            if field_name not in self.order_by_fields:
                validation_errors.update({field_name: "Ordering not allowed"})
        if validation_errors:
            return validation_errors

        return None
    def validate_order_by(self, request_params):
        order_by_params = request_params.get('order_by', [])
        order_by_params = filter_operators.transform_to_list(order_by_params)

        validation_errors = {}
        for order_by_field_name in order_by_params:
            field_name, order_by_type = filter_operators. \
                split_to_field_and_order_type(order_by_field_name)
            if field_name not in self.order_by_fields:
                validation_errors.update(
                    {field_name: "Ordering not allowed"})
        if validation_errors:
            return validation_errors

        return None