示例#1
0
文件: input.py 项目: hjalves/ines
    def __init__(self,
                 route_name,
                 request_method,
                 schema=None,
                 use_fields=False,
                 auto_camelcase=True):
        self.route_name = route_name
        self.request_method = request_method

        self.schema = schema
        self.fields_schema = None
        if use_fields:
            self.fields_schema = SearchFields()
        self.auto_camelcase = auto_camelcase
示例#2
0
文件: input.py 项目: hjalves/ines
    def __init__(
            self, route_name, request_method, schema=None, use_fields=False,
            auto_camelcase=True):
        self.route_name = route_name
        self.request_method = request_method

        self.schema = schema
        self.fields_schema = None
        if use_fields:
            self.fields_schema = SearchFields()
        self.auto_camelcase = auto_camelcase
示例#3
0
文件: input.py 项目: hjalves/ines
class InputSchemaView(object):
    schema_type = 'request'

    def __init__(
            self, route_name, request_method, schema=None, use_fields=False,
            auto_camelcase=True):
        self.route_name = route_name
        self.request_method = request_method

        self.schema = schema
        self.fields_schema = None
        if use_fields:
            self.fields_schema = SearchFields()
        self.auto_camelcase = auto_camelcase

    def __call__(self, wrapped):
        def decorator(context, request):
            context.input_schema = self.schema

            context.structure = {}
            if self.schema:
                structure = self.get_structure(request, self.schema)
                context.structure = self.schema.deserialize(structure) or {}

            # Construct output fields
            context.include_fields = set()
            context.exclude_fields = set()
            if self.fields_schema:
                fields_structure = self.get_structure(request, self.fields_schema)
                fields = self.fields_schema.deserialize(fields_structure)
                if fields:
                    context.include_fields.update(
                        self.decode_key(f) for f in fields.get('field', []))
                    context.include_fields.update(
                        self.decode_key(f) for f in fields.get('fields', []))
                    context.exclude_fields.update(
                        self.decode_key(f) for f in fields.get('exclude_field', []))
                    context.exclude_fields.update(
                        self.decode_key(f) for f in fields.get('exclude_fields', []))

            return wrapped(context, request)
        return decorator

    def decode_key(self, key):
        return uncamelcase(key) if self.auto_camelcase else key

    def encode_key(self, key):
        return camelcase(key) if self.auto_camelcase else key

    def get_structure(self, request, schema):
        method = request.method.upper()
        if method in ('PUT', 'POST'):
            request_values = request.POST.dict_of_lists()
        elif method == 'DELETE':
            request_values = request.DELETE.dict_of_lists()
        else:
            request_values = request.GET.dict_of_lists()

        return self.construct_structure(schema, request_values)

    def construct_structure(self, schema, values, padding=None):
        name = self.encode_key(schema.name)
        if padding and name:
            name = '%s.%s' % (padding, name)

        if isinstance(schema.typ, Sequence):
            child = schema.children[0]
            find_exact_name = not isinstance(child.typ, (Sequence, Tuple, Mapping))

            result = []
            for values in construct_sequence_items(name, values):
                value = self.construct_structure(child, values, padding=name)
                if value is not None:
                    result.append(value)

                if find_exact_name:
                    exact_value = values.get(name)
                    if exact_value:
                        result.append(exact_value)

            return result

        elif isinstance(schema.typ, Tuple):
            raise NotImplementedError('Tuple type need to be implemented')

        elif isinstance(schema.typ, Mapping):
            result = {}
            for child in schema.children:
                value = self.construct_structure(child, values, padding=name)
                if value is not None:
                    result[child.name] = value
            return result

        else:
            for key, value in values.items():
                if value and key == name:
                    if is_nonstr_iter(value):
                        return value.pop(0)
                    else:
                        return value
示例#4
0
文件: input.py 项目: hjalves/ines
class InputSchemaView(object):
    schema_type = 'request'

    def __init__(self,
                 route_name,
                 request_method,
                 schema=None,
                 use_fields=False,
                 auto_camelcase=True):
        self.route_name = route_name
        self.request_method = request_method

        self.schema = schema
        self.fields_schema = None
        if use_fields:
            self.fields_schema = SearchFields()
        self.auto_camelcase = auto_camelcase

    def __call__(self, wrapped):
        def decorator(context, request):
            context.input_schema = self.schema

            context.structure = {}
            if self.schema:
                structure = self.get_structure(request, self.schema)
                context.structure = self.schema.deserialize(structure) or {}

            # Construct output fields
            context.include_fields = set()
            context.exclude_fields = set()
            if self.fields_schema:
                fields_structure = self.get_structure(request,
                                                      self.fields_schema)
                fields = self.fields_schema.deserialize(fields_structure)
                if fields:
                    context.include_fields.update(
                        self.decode_key(f) for f in fields.get('field', []))
                    context.include_fields.update(
                        self.decode_key(f) for f in fields.get('fields', []))
                    context.exclude_fields.update(
                        self.decode_key(f)
                        for f in fields.get('exclude_field', []))
                    context.exclude_fields.update(
                        self.decode_key(f)
                        for f in fields.get('exclude_fields', []))

            return wrapped(context, request)

        return decorator

    def decode_key(self, key):
        return uncamelcase(key) if self.auto_camelcase else key

    def encode_key(self, key):
        return camelcase(key) if self.auto_camelcase else key

    def get_structure(self, request, schema):
        method = request.method.upper()
        if method in ('PUT', 'POST'):
            request_values = request.POST.dict_of_lists()
        elif method == 'DELETE':
            request_values = request.DELETE.dict_of_lists()
        else:
            request_values = request.GET.dict_of_lists()

        return self.construct_structure(schema, request_values)

    def construct_structure(self, schema, values, padding=None):
        name = self.encode_key(schema.name)
        if padding and name:
            name = '%s.%s' % (padding, name)

        if isinstance(schema.typ, Sequence):
            child = schema.children[0]
            find_exact_name = not isinstance(child.typ,
                                             (Sequence, Tuple, Mapping))

            result = []
            for values in construct_sequence_items(name, values):
                value = self.construct_structure(child, values, padding=name)
                if value is not None:
                    result.append(value)

                if find_exact_name:
                    exact_value = values.get(name)
                    if exact_value:
                        result.append(exact_value)

            return result

        elif isinstance(schema.typ, Tuple):
            raise NotImplementedError('Tuple type need to be implemented')

        elif isinstance(schema.typ, Mapping):
            result = {}
            for child in schema.children:
                value = self.construct_structure(child, values, padding=name)
                if value is not None:
                    result[child.name] = value
            return result

        else:
            for key, value in values.items():
                if value and key == name:
                    if is_nonstr_iter(value):
                        return value.pop(0)
                    else:
                        return value