Example #1
0
def validate_request_data(context: ILocation, request: Request, schema=MappingSchema(), extra_validators=[]):
    """ Validate request data.

    :param context: passed to validator functions
    :param request: passed to validator functions
    :param schema: Schema to validate. Data to validate is extracted from the
                   request.body. For schema nodes with attribute `location` ==
                   `querystring` the data is extracted from the query string.
                   The validated data (dict or list) is stored in the
                   `request.validated` attribute.
                   The `None` value is allowed to disable schema validation.
    :param extra_validators: Functions called after schema validation.
                             The passed arguments are `context` and  `request`.
                             The should append errors to `request.errors` and
                             validated data to `request.validated`.

    :raises HTTPBadRequest: HTTP 400 for bad request data.
    """
    parent = context if request.method == "POST" else context.__parent__
    workflow = _get_workflow(context, request)
    schema_with_binding = schema.bind(
        context=context, request=request, registry=request.registry, workflow=workflow, parent_pool=parent
    )
    body = {}
    if request.content_type == "multipart/form-data":
        body = unflatten_multipart_request(request)
    if request.content_type == "application/json":
        body = _extract_json_body(request)
    validate_user_headers(request)
    qs = _extract_querystring(request)
    validate_body_or_querystring(body, qs, schema_with_binding, context, request)
    _validate_extra_validators(extra_validators, context, request)
    if request.errors:
        request.validated = {}
        raise HTTPBadRequest()
Example #2
0
def add_put_data_subschemas(node: colander.Schema, kw: dict):
    """Add the resource sheet colander schemas that are 'editable'."""
    context = kw.get('context', None)
    request = kw.get('request', None)
    sheets = request.registry.content.get_sheets_edit(context, request)
    if request.content_type == 'multipart/form-data':
        body = unflatten_multipart_request(request)
    else:
        body = request.json_body
    data = body.get('data', {})
    for sheet in sheets:
        name = sheet.meta.isheet.__identifier__
        if name not in data:
            continue
        subschema = sheet.meta.schema_class(name=name)
        node.add(subschema.bind(**kw))
Example #3
0
def add_put_data_subschemas(node: MappingSchema, kw: dict):
    """Add the resource sheet schemas that are 'editable'."""
    context = kw['context']
    request = kw['request']
    sheets = request.registry.content.get_sheets_edit(context, request)
    if request.content_type == 'multipart/form-data':
        body = unflatten_multipart_request(request)
    else:
        body = request.json_body
    data = body.get('data', {})
    for sheet in sheets:
        name = sheet.meta.isheet.__identifier__
        if name not in data:
            continue
        schema = sheet.get_schema_with_bindings()
        node.add(schema)
Example #4
0
def add_put_data_subschemas(node: MappingSchema, kw: dict):
    """Add the resource sheet schemas that are 'editable'."""
    context = kw['context']
    request = kw['request']
    sheets = request.registry.content.get_sheets_edit(context, request)
    if request.content_type == 'multipart/form-data':
        body = unflatten_multipart_request(request)
    else:
        body = request.json_body
    data = body.get('data', {})
    for sheet in sheets:
        name = sheet.meta.isheet.__identifier__
        if name not in data:
            continue
        schema = sheet.get_schema_with_bindings()
        node.add(schema)
Example #5
0
def add_put_data_subschemas(node: colander.Schema, kw: dict):
    """Add the resource sheet colander schemas that are 'editable'."""
    context = kw.get('context', None)
    request = kw.get('request', None)
    sheets = request.registry.content.get_sheets_edit(context, request)
    if request.content_type == 'multipart/form-data':
        body = unflatten_multipart_request(request)
    else:
        body = request.json_body
    data = body.get('data', {})
    for sheet in sheets:
        name = sheet.meta.isheet.__identifier__
        if name not in data:
            continue
        subschema = sheet.meta.schema_class(name=name)
        node.add(subschema.bind(**kw))
Example #6
0
def validate_request_data(context: ILocation,
                          request: Request,
                          schema=MappingSchema(),
                          extra_validators=[]):
    """ Validate request data.

    :param context: passed to validator functions
    :param request: passed to validator functions
    :param schema: Schema to validate. Data to validate is extracted from the
                   request.body. For schema nodes with attribute `location` ==
                   `querystring` the data is extracted from the query string.
                   The validated data (dict or list) is stored in the
                   `request.validated` attribute.
                   The `None` value is allowed to disable schema validation.
    :param extra_validators: Functions called after schema validation.
                             The passed arguments are `context` and  `request`.
                             The should append errors to `request.errors` and
                             validated data to `request.validated`.

    :raises HTTPBadRequest: HTTP 400 for bad request data.
    """
    parent = context if request.method == 'POST' else context.__parent__
    workflow = _get_workflow(context, request)
    schema_with_binding = schema.bind(context=context,
                                      request=request,
                                      registry=request.registry,
                                      workflow=workflow,
                                      parent_pool=parent)
    body = {}
    if request.content_type == 'multipart/form-data':
        body = unflatten_multipart_request(request)
    if request.content_type == 'application/json':
        body = _extract_json_body(request)
    validate_user_headers(request)
    qs = _extract_querystring(request)
    validate_body_or_querystring(body, qs, schema_with_binding, context,
                                 request)
    _validate_extra_validators(extra_validators, context, request)
    if request.errors:
        request.validated = {}
        raise HTTPBadRequest()
Example #7
0
def _validate_request_data(context: IResource, request: IRequest,
                           schema: colander.Schema):
    """Validate request data.

    :param context: passed to validator functions
    :param request: passed to validator functions
    :param schema: Schema to validate. Data to validate is extracted from the
                   request.body. For schema nodes with attribute `location` ==
                   `querystring` the data is extracted from the query string.
                   The validated data (dict or list) is stored in the
                   `request.validated` attribute.
    :raises HTTPBadRequest: HTTP 400 for bad request data.
    """
    body = {}
    if request.content_type == 'multipart/form-data':
        body = unflatten_multipart_request(request)
    if request.content_type == 'application/json':
        body = _extract_json_body(request)
    qs = _extract_querystring(request)
    _validate_body_or_querystring(body, qs, schema, context, request)
    if request.errors:
        request.validated = {}
        raise HTTPBadRequest()
Example #8
0
def _validate_request_data(context: IResource,
                           request: IRequest,
                           schema: colander.Schema):
    """Validate request data.

    :param context: passed to validator functions
    :param request: passed to validator functions
    :param schema: Schema to validate. Data to validate is extracted from the
                   request.body. For schema nodes with attribute `location` ==
                   `querystring` the data is extracted from the query string.
                   The validated data (dict or list) is stored in the
                   `request.validated` attribute.
    :raises HTTPBadRequest: HTTP 400 for bad request data.
    """
    body = {}
    if request.content_type == 'multipart/form-data':
        body = unflatten_multipart_request(request)
    if request.content_type == 'application/json':
        body = _extract_json_body(request)
    qs = _extract_querystring(request)
    _validate_body_or_querystring(body, qs, schema, context, request)
    if request.errors:
        request.validated = {}
        raise HTTPBadRequest()