<s> from __future__ import unicode_literals <EOL> import re <EOL> from pyramid . httpexceptions import HTTPBadRequest <EOL> from pyramid . path import DottedNameResolver <EOL> MINIMUM_AMOUNT = <NUM_LIT:50> <EOL> STATEMENT_REXP = ( <EOL> '<STR_LIT>' . format ( re . escape ( '''<STR_LIT>''' ) ) <EOL> ) <EOL> def form_errors_to_bad_request ( errors ) : <EOL> """<STR_LIT>""" <EOL> error_params = [ ] <EOL> error_params . append ( '<STR_LIT>' ) <EOL> for param_key , param_errors in errors . iteritems ( ) : <EOL> indent = '<STR_LIT:U+0020>' * <NUM_LIT:4> <EOL> error_params . append ( indent + '<STR_LIT>' ) <EOL> indent = '<STR_LIT:U+0020>' * <NUM_LIT:8> <EOL> error_params . append ( indent + '<STR_LIT>' . format ( param_key ) ) <EOL> for param_error in param_errors : <EOL> indent = '<STR_LIT:U+0020>' * <NUM_LIT:12> <EOL> error_params . append ( indent + '<STR_LIT>' . format ( param_error ) ) <EOL> indent = '<STR_LIT:U+0020>' * <NUM_LIT:8> <EOL> error_params . append ( indent + '<STR_LIT>' ) <EOL> indent = '<STR_LIT:U+0020>' * <NUM_LIT:4> <EOL> error_params . append ( indent + '<STR_LIT>' ) <EOL> error_params . append ( '<STR_LIT>' ) <EOL> error_params = '<STR_LIT:\n>' . join ( error_params ) <EOL> message = "<STR_LIT>" . format ( error_params ) <EOL> return HTTPBadRequest ( message ) <EOL> def validate_form ( form_cls , request ) : <EOL> """<STR_LIT>""" <EOL> form = form_cls ( request . params ) <EOL> form . model_factory = request . model_factory <EOL> validation_result = form . validate ( ) <EOL> if not validation_result : <EOL> raise form_errors_to_bad_request ( form . errors ) <EOL> return form <EOL> class RecordExistValidator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model_cls ) : <EOL> self . model_cls = model_cls <EOL> def __call__ ( self , form , field ) : <EOL> model = self . model_cls ( form . model_factory ) <EOL> if model . get ( field . data ) is None : <EOL> msg = field . gettext ( '<STR_LIT>' <EOL> . format ( self . model_cls . TABLE . __name__ , <EOL> field . data ) ) <EOL> raise ValueError ( msg ) <EOL> def list_by_context ( request , model_cls , context ) : <EOL> """<STR_LIT>""" <EOL> model = model_cls ( request . model_factory ) <EOL> offset = int ( request . params . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> limit = int ( request . params . get ( '<STR_LIT>' , <NUM_LIT:20> ) ) <EOL> kwargs = { } <EOL> if '<STR_LIT>' in request . params : <EOL> kwargs [ '<STR_LIT>' ] = request . params [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in request . params : <EOL> kwargs [ '<STR_LIT>' ] = request . params [ '<STR_LIT>' ] <EOL> items = model . list_by_context ( <EOL> context = context , <EOL> offset = offset , <EOL> limit = limit , <EOL> ** kwargs <EOL> ) <EOL> result = dict ( <EOL> items = list ( items ) , <EOL> offset = offset , <EOL> limit = limit , <EOL> ) <EOL> return result <EOL> def get_processor_factory ( settings ) : <EOL> """<STR_LIT>""" <EOL> resolver = DottedNameResolver ( ) <EOL> processor_factory = settings [ '<STR_LIT>' ] <EOL> processor_factory = resolver . maybe_resolve ( processor_factory ) <EOL> return processor_factory </s>
