<s> import cgi <EOL> from wtforms import Form <EOL> from wtforms import validators <EOL> from pyramid . i18n import get_localizer <EOL> from pyramid . renderers import render <EOL> from pyramid . threadlocal import get_current_request <EOL> from apex . lib . db import merge_session_with_post <EOL> from apex . lib . i18n import Translator <EOL> class ExtendedForm ( Form ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , formdata = None , obj = None , prefix = '<STR_LIT>' , ** kwargs ) : <EOL> self . request = kwargs . pop ( '<STR_LIT>' , get_current_request ( ) ) <EOL> super ( Form , self ) . __init__ ( self . _unbound_fields , prefix = prefix ) <EOL> self . is_multipart = False <EOL> for name , field in self . _fields . iteritems ( ) : <EOL> if field . type == '<STR_LIT>' : <EOL> self . is_multipart = True <EOL> setattr ( self , name , field ) <EOL> self . process ( formdata , obj , ** kwargs ) <EOL> def hidden_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _fields [ name ] for name , field in self . _unbound_fields <EOL> if self . _fields . has_key ( name ) and self . _fields [ name ] . type == '<STR_LIT>' ] <EOL> def visible_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _fields [ name ] for name , field in self . _unbound_fields <EOL> if self . _fields . has_key ( name ) and not self . _fields [ name ] . type == '<STR_LIT>' ] <EOL> def _get_translations ( self ) : <EOL> if self . request : <EOL> localizer = get_localizer ( self . request ) <EOL> return Translator ( localizer ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def validate ( self ) : <EOL> if not super ( ExtendedForm , self ) . validate ( ) : <EOL> return False <EOL> errors = self . clean ( ) <EOL> if errors : <EOL> self . _errors = { '<STR_LIT>' : errors } <EOL> return False <EOL> return True <EOL> def render ( self , ** kwargs ) : <EOL> action = kwargs . pop ( '<STR_LIT:action>' , '<STR_LIT>' ) <EOL> submit_text = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> template = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if not template : <EOL> settings = self . request . registry . settings <EOL> template = settings . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return render ( template , { <EOL> '<STR_LIT>' : self , <EOL> '<STR_LIT:action>' : action , <EOL> '<STR_LIT>' : submit_text , <EOL> '<STR_LIT:args>' : kwargs , <EOL> } , request = self . request ) <EOL> class StyledWidget ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , widget = None , ** kwargs ) : <EOL> self . widget = widget <EOL> self . kw = kwargs <EOL> def __call__ ( self , field , ** kwargs ) : <EOL> if not self . widget : <EOL> self . widget = field . __class__ . widget <EOL> return self . widget ( field , ** dict ( self . kw , ** kwargs ) ) <EOL> class FileRequired ( validators . Required ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , form , field ) : <EOL> if not isinstance ( field . data , cgi . FieldStorage ) : <EOL> if self . message is None : <EOL> self . message = field . gettext ( u'<STR_LIT>' ) <EOL> field . errors [ : ] = [ ] <EOL> raise validators . StopValidation ( self . message ) <EOL> class ModelForm ( ExtendedForm ) : <EOL> """<STR_LIT>""" <EOL> def save ( self , session , model , commit = True ) : <EOL> record = model ( ) <EOL> record = merge_session_with_post ( record , self . data . items ( ) ) <EOL> if commit : <EOL> session . add ( record ) <EOL> session . flush ( ) <EOL> return record </s>
