예제 #1
0
def on_valid(valid_content_type, on_invalid=json):
    """Renders as the specified content type only if no errors are found in the provided data object"""
    invalid_kwargs = introspect.generate_accepted_kwargs(
        on_invalid, 'request', 'response')
    invalid_takes_response = introspect.takes_all_arguments(
        on_invalid, 'response')

    def wrapper(function):
        valid_kwargs = introspect.generate_accepted_kwargs(
            function, 'request', 'response')
        valid_takes_response = introspect.takes_all_arguments(
            function, 'response')

        @content_type(valid_content_type)
        @wraps(function)
        def output_content(content, response, **kwargs):
            if type(content) == dict and 'errors' in content:
                response.content_type = on_invalid.content_type
                if invalid_takes_response:
                    kwargs['response'] = response
                return on_invalid(content, **invalid_kwargs(kwargs))

            if valid_takes_response:
                kwargs['response'] = response
            return function(content, **valid_kwargs(kwargs))

        return output_content

    return wrapper
예제 #2
0
 def image_handler(data, **kwargs):
     if hasattr(data, 'read'):
         return data
     elif hasattr(data, 'save'):
         output = stream()
         if introspect.takes_all_arguments(data.save, 'format') or introspect.takes_kwargs(data.save):
             data.save(output, format=image_format.upper())
         else:
             data.save(output)
         output.seek(0)
         return output
     elif hasattr(data, 'render'):
         return data.render()
     elif os.path.isfile(data):
         return open(data, 'rb')
예제 #3
0
 def image_handler(data, **kwargs):
     if hasattr(data, 'read'):
         return data
     elif hasattr(data, 'save'):
         output = stream()
         if introspect.takes_all_arguments(data.save, 'format') or introspect.takes_kwargs(data.save):
             data.save(output, format=image_format.upper())
         else:
             data.save(output)
         output.seek(0)
         return output
     elif hasattr(data, 'render'):
         return data.render()
     elif os.path.isfile(data):
         return open(data, 'rb')
예제 #4
0
    def wrapper(function):
        valid_kwargs = introspect.generate_accepted_kwargs(function, 'request', 'response')
        valid_takes_response = introspect.takes_all_arguments(function, 'response')

        @content_type(valid_content_type)
        @wraps(function)
        def output_content(content, response, **kwargs):
            if type(content) == dict and 'errors' in content:
                response.content_type = on_invalid.content_type
                if invalid_takes_response:
                    kwargs['response'] = response
                return on_invalid(content, **invalid_kwargs(kwargs))

            if valid_takes_response:
                kwargs['response'] = response
            return function(content, **valid_kwargs(kwargs))
        return output_content
예제 #5
0
    def wrapper(function):
        valid_kwargs = introspect.generate_accepted_kwargs(function, 'request', 'response')
        valid_takes_response = introspect.takes_all_arguments(function, 'response')

        @content_type(valid_content_type)
        @wraps(function)
        def output_content(content, response, **kwargs):
            if type(content) == dict and 'errors' in content:
                response.content_type = on_invalid.content_type
                if invalid_takes_response:
                    kwargs['response'] = response
                return on_invalid(content, **invalid_kwargs(kwargs))

            if valid_takes_response:
                kwargs['response'] = response
            return function(content, **valid_kwargs(kwargs))
        return output_content
예제 #6
0
def on_valid(valid_content_type, on_invalid=json):
    """Renders as the specified content type only if no errors are found in the provided data object"""
    invalid_kwargs = introspect.generate_accepted_kwargs(on_invalid, 'request', 'response')
    invalid_takes_response = introspect.takes_all_arguments(on_invalid, 'response')

    def wrapper(function):
        valid_kwargs = introspect.generate_accepted_kwargs(function, 'request', 'response')
        valid_takes_response = introspect.takes_all_arguments(function, 'response')

        @content_type(valid_content_type)
        @wraps(function)
        def output_content(content, response, **kwargs):
            if type(content) == dict and 'errors' in content:
                response.content_type = on_invalid.content_type
                if invalid_takes_response:
                    kwargs['response'] = response
                return on_invalid(content, **invalid_kwargs(kwargs))

            if valid_takes_response:
                kwargs['response'] = response
            return function(content, **valid_kwargs(kwargs))
        return output_content
    return wrapper