Wednesday, March 16, 2011

validate decorator for some service call

A validate decorator modified from formencode validate which returns input form if failed, it returns error message string. Used in some service call.

'''
Created on Mar 16, 2011

@author: eryx lee
'''
import logging

import formencode

from decorator import decorator
from pylons.decorators import PylonsFormEncodeState

log = logging.getLogger(__name__)

def validate_api(schema=None, validators=None, error_msg_template=None,
             variable_decode=False, dict_char='.', list_char='-', 
             post_only=False, state=None, **htmlfill_kwargs):
    """Validate input either for a FormEncode schema, or individual
    validators

    Given a form schema or dict of validators, validate will attempt to
    validate the schema or validator list.

    If validation was successful, the valid result dict will be saved
    as ``self.form_result``. Otherwise, the action will return a error
    message formatted by ``error_msg_template``

    ``schema``
        Refers to a FormEncode Schema object to use during validation.
    ``variable_decode``
        Boolean to indicate whether FormEncode's variable decode
        function should be run on the form input before validation.
    ``error_msg_template``
        String template to format error message.
    ``dict_char``
        Passed through to FormEncode. Toggles the form field naming 
        scheme used to determine what is used to represent a dict. This
        option is only applicable when used with variable_decode=True.
    ``list_char``
        Passed through to FormEncode. Toggles the form field naming
        scheme used to determine what is used to represent a list. This
        option is only applicable when used with variable_decode=True.
    ``post_only``
        Boolean that indicates whether or not GET (query) variables
        should be included during validation.
        
        .. warning::
            ``post_only`` applies to *where* the arguments to be
            validated come from. It does *not* restrict the form to
            only working with post, merely only checking POST vars.
    ``state``
        Passed through to FormEncode for use in validators that utilize
        a state object.

    Example::

        class SomeController(BaseController):

            def create(self, id):
                return render('/myform.mako')

            @validate(schema=model.forms.myshema())
            def update(self, id):
                # Do something with self.form_result
                pass

    """
    if state is None:
        state = PylonsFormEncodeState
    def wrapper(func, self, *args, **kwargs):
        """Decorator Wrapper function"""
        request = self._py_object.request
        errors = {}
        
        # If they want post args only, use just the post args
        if post_only:
            params = request.POST
        else:
            params = request.params
        
        params = params.mixed()
        if variable_decode:
            log.debug("Running variable_decode on params")
            decoded = formencode.variabledecode.variable_decode(params, dict_char,
                                                     list_char)
        else:
            decoded = params

        if schema:
            log.debug("Validating against a schema")
            try:
                self.form_result = schema.to_python(decoded, state)
            except formencode.Invalid, e:
                errors = e.unpack_errors(variable_decode, dict_char, list_char)
        if validators:
            log.debug("Validating against provided validators")
            if isinstance(validators, dict):
                if not hasattr(self, 'form_result'):
                    self.form_result = {}
                for field, validator in validators.iteritems():
                    try:
                        self.form_result[field] = \
                            validator.to_python(decoded.get(field), state)
                    except formencode.Invalid, error:
                        errors[field] = error
        if errors:
            log.debug("Errors found in validation, form error message.")
            request.environ['REQUEST_METHOD'] = 'GET'
            self._py_object.c.form_errors = errors
            
            form_content = ', '.join(["%s: %s" % itm for itm in errors.items()])
            if error_msg_template:
                form_content = error_msg_template % form_content

            return form_content
        return func(self, *args, **kwargs)
    return decorator(wrapper)

No comments:

Post a Comment