<s> """<STR_LIT>""" <EOL> __all__ = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ) <EOL> from gluon import * <EOL> from s3rest import S3Method <EOL> from s3utils import s3_unicode <EOL> class S3XForms ( S3Method ) : <EOL> """<STR_LIT>""" <EOL> def apply_method ( self , r , ** attr ) : <EOL> """<STR_LIT>""" <EOL> if r . http == "<STR_LIT:GET>" : <EOL> return self . form ( r , ** attr ) <EOL> else : <EOL> r . error ( <NUM_LIT> , current . ERROR . BAD_METHOD ) <EOL> def form ( self , r , ** attr ) : <EOL> """<STR_LIT>""" <EOL> resource = self . resource <EOL> form = S3XFormsForm ( resource . table ) <EOL> response = current . response <EOL> response . headers [ "<STR_LIT:Content-Type>" ] = "<STR_LIT>" <EOL> return form <EOL> @ staticmethod <EOL> def formlist ( ) : <EOL> """<STR_LIT>""" <EOL> resources = current . deployment_settings . get_xforms_resources ( ) <EOL> xforms = [ ] <EOL> if resources : <EOL> s3db = current . s3db <EOL> for item in resources : <EOL> options = { } <EOL> if isinstance ( item , ( tuple , list ) ) : <EOL> if len ( item ) == <NUM_LIT:2> : <EOL> title , tablename = item <EOL> if isinstance ( tablename , dict ) : <EOL> tablename , options = title , tablename <EOL> title = None <EOL> elif len ( item ) == <NUM_LIT:3> : <EOL> title , tablename , options = item <EOL> else : <EOL> continue <EOL> else : <EOL> title , tablename = None , item <EOL> try : <EOL> resource = s3db . resource ( tablename ) <EOL> except AttributeError : <EOL> current . log . warning ( "<STR_LIT>" % tablename ) <EOL> continue <EOL> if title is None : <EOL> title = "<STR_LIT:U+0020>" . join ( w . capitalize ( ) for w in resource . name . split ( "<STR_LIT:_>" ) ) <EOL> c = options . get ( "<STR_LIT:c>" , "<STR_LIT>" ) <EOL> f = options . get ( "<STR_LIT:f>" , "<STR_LIT>" ) <EOL> url_vars = options . get ( "<STR_LIT>" , { } ) <EOL> config = resource . get_config ( "<STR_LIT>" ) <EOL> if config : <EOL> if isinstance ( config , dict ) : <EOL> collection = config . get ( "<STR_LIT>" ) <EOL> if not collection : <EOL> continue <EOL> table = resource . table <EOL> DELETED = current . xml . DELETED <EOL> if DELETED in table : <EOL> query = table [ DELETED ] != True <EOL> else : <EOL> query = table . id > <NUM_LIT:0> <EOL> public = options . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if public in table : <EOL> query &= table [ public ] == True <EOL> title_field = options . get ( "<STR_LIT:title>" , "<STR_LIT:name>" ) <EOL> rows = current . db ( query ) . select ( table . _id , <EOL> table [ title_field ] , <EOL> ) <EOL> native = c == "<STR_LIT>" <EOL> for row in rows : <EOL> if native : <EOL> args = [ tablename , row [ table . _id ] ] <EOL> else : <EOL> args = [ row [ table . _id ] , "<STR_LIT>" ] <EOL> url = URL ( c = c , <EOL> f = f , <EOL> args = args , <EOL> vars = url_vars , <EOL> host = True , <EOL> extension = "<STR_LIT>" , <EOL> ) <EOL> xforms . append ( ( url , row [ title_field ] ) ) <EOL> else : <EOL> continue <EOL> else : <EOL> if c == "<STR_LIT>" : <EOL> args = [ tablename ] <EOL> else : <EOL> args = [ "<STR_LIT>" ] <EOL> url = URL ( c = c , <EOL> f = f , <EOL> args = args , <EOL> vars = url_vars , <EOL> host = True , <EOL> extension = "<STR_LIT>" , <EOL> ) <EOL> xforms . append ( ( url , title ) ) <EOL> return xforms <EOL> class S3XFormsWidget ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , translate = True ) : <EOL> """<STR_LIT>""" <EOL> self . translate = translate <EOL> self . _strings = { } <EOL> def __call__ ( self , field , label , ref ) : <EOL> """<STR_LIT>""" <EOL> if not field : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> if not ref : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> self . ref = ref <EOL> attr = { "<STR_LIT>" : ref } <EOL> self . setstr ( "<STR_LIT:label>" , label ) <EOL> comment = field . comment <EOL> if comment and isinstance ( comment , basestring ) : <EOL> self . setstr ( "<STR_LIT>" , comment ) <EOL> return self . widget ( field , attr ) , self . _strings <EOL> def widget ( self , field , attr ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def hint ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getstr ( "<STR_LIT>" , "<STR_LIT>" , default = TAG [ "<STR_LIT>" ] ( ) ) <EOL> def label ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getstr ( "<STR_LIT:label>" , "<STR_LIT:label>" ) <EOL> def setstr ( self , key , string = None ) : <EOL> """<STR_LIT>""" <EOL> ref = "<STR_LIT>" % ( self . ref , key ) <EOL> strings = self . _strings <EOL> if string : <EOL> if hasattr ( string , "<STR_LIT>" ) : <EOL> string = string . flatten ( ) <EOL> strings [ ref ] = string <EOL> elif key in strings : <EOL> del strings [ ref ] <EOL> return <EOL> def getstr ( self , tag , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> empty = False <EOL> ref = "<STR_LIT>" % ( self . ref , key ) <EOL> translations = self . _strings <EOL> if ref in translations : <EOL> string = translations [ ref ] <EOL> elif default is not None : <EOL> return default <EOL> else : <EOL> ref = None <EOL> string = "<STR_LIT>" <EOL> widget = TAG [ str ( tag ) ] <EOL> if self . translate and ref : <EOL> return widget ( _ref = "<STR_LIT>" % ref ) <EOL> else : <EOL> return widget ( string ) <EOL> class S3XFormsStringWidget ( S3XFormsWidget ) : <EOL> """<STR_LIT>""" <EOL> def widget ( self , field , attr ) : <EOL> """<STR_LIT>""" <EOL> return TAG [ "<STR_LIT:input>" ] ( self . label ( ) , self . hint ( ) , ** attr ) <EOL> class S3XFormsTextWidget ( S3XFormsWidget ) : <EOL> """<STR_LIT>""" <EOL> def widget ( self , field , attr ) : <EOL> """<STR_LIT>""" <EOL> return TAG [ "<STR_LIT:input>" ] ( self . label ( ) , self . hint ( ) , ** attr ) <EOL> class S3XFormsReadonlyWidget ( S3XFormsWidget ) : <EOL> """<STR_LIT>""" <EOL> def widget ( self , field , attr ) : <EOL> """<STR_LIT>""" <EOL> attr [ "<STR_LIT>" ] = "<STR_LIT:true>" <EOL> attr [ "<STR_LIT>" ] = s3_unicode ( field . default ) <EOL> return TAG [ "<STR_LIT:input>" ] ( self . label ( ) , ** attr ) <EOL> class S3XFormsOptionsWidget ( S3XFormsWidget ) : <EOL> """<STR_LIT>""" <EOL> def widget ( self , field , attr ) : <EOL> """<STR_LIT>""" <EOL> requires = field . requires <EOL> if not hasattr ( requires , "<STR_LIT>" ) : <EOL> return TAG [ "<STR_LIT:input>" ] ( self . label ( ) , ** attr ) <EOL> items = [ self . label ( ) , self . hint ( ) ] + self . items ( requires . options ( ) ) <EOL> return TAG [ "<STR_LIT>" ] ( items , ** attr ) <EOL> def items ( self , options ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> setstr = self . setstr <EOL> getstr = self . getstr <EOL> for index , option in enumerate ( options ) : <EOL> value , text = option <EOL> key = "<STR_LIT>" % index <EOL> if hasattr ( text , "<STR_LIT:m>" ) or hasattr ( text , "<STR_LIT>" ) : <EOL> setstr ( key , text ) <EOL> text = getstr ( "<STR_LIT:label>" , key ) <EOL> else : <EOL> text = TAG [ "<STR_LIT:label>" ] ( text ) <EOL> items . append ( TAG [ "<STR_LIT>" ] ( text , <EOL> TAG [ "<STR_LIT:value>" ] ( value ) , <EOL> ) ) <EOL> return items <EOL> class S3XFormsMultipleOptionsWidget ( S3XFormsOptionsWidget ) : <EOL> """<STR_LIT>""" <EOL> def widget ( self , field , attr ) : <EOL> """<STR_LIT>""" <EOL> requires = field . requires <EOL> if not hasattr ( requires , "<STR_LIT>" ) : <EOL> return TAG [ "<STR_LIT:input>" ] ( self . label ( ) , ** attr ) <EOL> items = [ self . label ( ) , self . hint ( ) ] + self . items ( requires . options ( ) ) <EOL> return TAG [ "<STR_LIT>" ] ( items , ** attr ) <EOL> class S3XFormsBooleanWidget ( S3XFormsWidget ) : <EOL> """<STR_LIT>""" <EOL> def widget ( self , field , attr ) : <EOL> """<STR_LIT>""" <EOL> T = current . T <EOL> setstr = self . setstr <EOL> setstr ( "<STR_LIT:false>" , T ( "<STR_LIT>" ) ) <EOL> setstr ( "<STR_LIT:true>" , T ( "<STR_LIT>" ) ) <EOL> getstr = self . getstr <EOL> items = [ self . label ( ) , <EOL> self . hint ( ) , <EOL> TAG [ "<STR_LIT>" ] ( getstr ( "<STR_LIT:label>" , "<STR_LIT:true>" ) , TAG [ "<STR_LIT:value>" ] ( <NUM_LIT:1> ) ) , <EOL> TAG [ "<STR_LIT>" ] ( getstr ( "<STR_LIT:label>" , "<STR_LIT:true>" ) , TAG [ "<STR_LIT:value>" ] ( <NUM_LIT:0> ) ) , <EOL> ] <EOL> return TAG [ "<STR_LIT>" ] ( items , ** attr ) <EOL> class S3XFormsUploadWidget ( S3XFormsWidget ) : <EOL> """<STR_LIT>""" <EOL> def widget ( self , field , attr ) : <EOL> """<STR_LIT>""" <EOL> attr [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> return TAG [ "<STR_LIT>" ] ( self . label ( ) , self . hint ( ) , ** attr ) <EOL> class S3XFormsField ( object ) : <EOL> """<STR_LIT>""" <EOL> widgets = { <EOL> "<STR_LIT:string>" : S3XFormsStringWidget , <EOL> "<STR_LIT:text>" : S3XFormsTextWidget , <EOL> "<STR_LIT>" : S3XFormsStringWidget , <EOL> "<STR_LIT>" : S3XFormsStringWidget , <EOL> "<STR_LIT>" : S3XFormsStringWidget , <EOL> "<STR_LIT:time>" : S3XFormsStringWidget , <EOL> "<STR_LIT:date>" : S3XFormsStringWidget , <EOL> "<STR_LIT>" : S3XFormsStringWidget , <EOL> "<STR_LIT>" : S3XFormsUploadWidget , <EOL> "<STR_LIT>" : S3XFormsBooleanWidget , <EOL> "<STR_LIT>" : S3XFormsOptionsWidget , <EOL> "<STR_LIT>" : S3XFormsMultipleOptionsWidget , <EOL> } <EOL> types = { "<STR_LIT:string>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:date>" : "<STR_LIT:date>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:int>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT:text>" , <EOL> } <EOL> def __init__ ( self , tablename , field , translate = True ) : <EOL> """<STR_LIT>""" <EOL> self . tablename = tablename <EOL> self . field = field <EOL> self . name = field . name <EOL> self . ref = "<STR_LIT>" % ( self . tablename , self . name ) <EOL> self . translate = translate <EOL> self . _model = None <EOL> self . _binding = None <EOL> self . _strings = None <EOL> self . _widget = None <EOL> @ property <EOL> def model ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _model is None : <EOL> self . _introspect ( ) <EOL> return self . _model <EOL> @ property <EOL> def binding ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _binding is None : <EOL> self . _introspect ( ) <EOL> return self . _binding <EOL> @ property <EOL> def strings ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _strings is None : <EOL> self . _introspect ( ) <EOL> return self . _strings <EOL> @ property <EOL> def widget ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _widget is None : <EOL> self . _introspect ( ) <EOL> return self . _widget <EOL> def _introspect ( self ) : <EOL> """<STR_LIT>""" <EOL> field = self . field <EOL> self . _strings = { } <EOL> self . _model = TAG [ self . name ] ( ) <EOL> readonly = None <EOL> required = None <EOL> if not field . writable : <EOL> readonly = "<STR_LIT>" <EOL> elif self . _required ( field ) : <EOL> required = "<STR_LIT>" <EOL> attr = { "<STR_LIT>" : self . ref , <EOL> "<STR_LIT>" : required , <EOL> "<STR_LIT>" : readonly , <EOL> } <EOL> fieldtype = self . types . get ( str ( field . type ) , "<STR_LIT:string>" ) <EOL> requires = field . requires <EOL> options = False <EOL> multiple = False <EOL> if requires : <EOL> if not isinstance ( requires , list ) : <EOL> requires = [ requires ] <EOL> first = requires [ <NUM_LIT:0> ] <EOL> if hasattr ( first , "<STR_LIT>" ) : <EOL> options = True <EOL> if hasattr ( first , "<STR_LIT>" ) and first . multiple : <EOL> multiple = True <EOL> elif fieldtype in ( "<STR_LIT>" , "<STR_LIT:int>" , "<STR_LIT:date>" , "<STR_LIT>" ) : <EOL> constraint = self . _range ( requires ) <EOL> if constraint : <EOL> attr [ "<STR_LIT>" ] = constraint <EOL> if not options : <EOL> attr [ "<STR_LIT>" ] = fieldtype <EOL> self . _binding = TAG [ "<STR_LIT>" ] ( ** attr ) <EOL> if hasattr ( field , "<STR_LIT>" ) : <EOL> widget = field . xform <EOL> else : <EOL> if not field . writable : <EOL> widget_type = "<STR_LIT>" <EOL> else : <EOL> widget_type = str ( field . type ) <EOL> if options : <EOL> if multiple : <EOL> widget_type = "<STR_LIT>" <EOL> else : <EOL> widget_type = "<STR_LIT>" <EOL> widgets = self . widgets <EOL> if widget_type in widgets : <EOL> widget = widgets [ widget_type ] <EOL> else : <EOL> widget = None <EOL> if widget is not None : <EOL> if isinstance ( widget , type ) : <EOL> widget = widget ( translate = self . translate ) <EOL> if hasattr ( field , "<STR_LIT:label>" ) : <EOL> label = field . label <EOL> else : <EOL> label = None <EOL> if not label : <EOL> label = current . T ( "<STR_LIT:U+0020>" . join ( s . capitalize ( ) <EOL> for s in self . name . split ( "<STR_LIT:_>" ) ) . strip ( ) ) <EOL> self . _widget , strings = widget ( field , label , self . ref ) <EOL> if self . translate : <EOL> self . _strings . update ( strings ) <EOL> else : <EOL> self . _widget = TAG [ "<STR_LIT>" ] ( ) <EOL> return <EOL> @ staticmethod <EOL> def _range ( validators ) : <EOL> """<STR_LIT>""" <EOL> constraints = [ ] <EOL> for validator in validators : <EOL> if hasattr ( validator , "<STR_LIT>" ) : <EOL> v = validator . other <EOL> else : <EOL> v = validator <EOL> if isinstance ( v , ( IS_INT_IN_RANGE , IS_FLOAT_IN_RANGE ) ) : <EOL> maximum = v . maximum <EOL> if maximum is not None : <EOL> constraints . append ( "<STR_LIT>" % maximum ) <EOL> minimum = v . minimum <EOL> if minimum is not None : <EOL> constraints . append ( "<STR_LIT>" % minimum ) <EOL> if constraints : <EOL> return "<STR_LIT>" % "<STR_LIT>" . join ( constraints ) <EOL> return None <EOL> @ staticmethod <EOL> def _required ( field ) : <EOL> """<STR_LIT>""" <EOL> required = False <EOL> validators = field . requires <EOL> if isinstance ( validators , IS_EMPTY_OR ) : <EOL> required = False <EOL> else : <EOL> required = field . required or field . notnull <EOL> if not required and validators : <EOL> if not isinstance ( validators , ( list , tuple ) ) : <EOL> validators = [ validators ] <EOL> for v in validators : <EOL> if hasattr ( v , "<STR_LIT>" ) : <EOL> if hasattr ( v , "<STR_LIT>" ) and v . zero is None : <EOL> continue <EOL> if hasattr ( v , "<STR_LIT>" ) : <EOL> if v . mark_required : <EOL> required = True <EOL> break <EOL> else : <EOL> continue <EOL> try : <EOL> val , error = v ( "<STR_LIT>" ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> if error : <EOL> required = True <EOL> break <EOL> return required <EOL> class S3XFormsForm ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , table , name = None , translate = True ) : <EOL> """<STR_LIT>""" <EOL> self . table = table <EOL> if name : <EOL> self . name = name <EOL> elif hasattr ( table , "<STR_LIT>" ) : <EOL> self . name = table . _tablename <EOL> else : <EOL> self . name = str ( table ) <EOL> fields = [ ] <EOL> append = fields . append <EOL> for field in table : <EOL> if field . readable : <EOL> append ( S3XFormsField ( table , field , translate = translate ) ) <EOL> self . _fields = fields <EOL> self . translate = translate <EOL> def xml ( self ) : <EOL> """<STR_LIT>""" <EOL> ns = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> document = TAG [ "<STR_LIT>" ] ( self . _head ( ) , self . _body ( ) , ** ns ) <EOL> return document . xml ( ) <EOL> def _head ( self ) : <EOL> """<STR_LIT>""" <EOL> title = TAG [ "<STR_LIT>" ] ( self . name ) <EOL> model = self . _model ( ) <EOL> return TAG [ "<STR_LIT>" ] ( title , model ) <EOL> def _body ( self ) : <EOL> """<STR_LIT>""" <EOL> widgets = self . _widgets ( ) <EOL> return TAG [ "<STR_LIT>" ] ( widgets ) <EOL> def _model ( self ) : <EOL> """<STR_LIT>""" <EOL> instance = self . _instance ( ) <EOL> bindings = self . _bindings ( ) <EOL> translations = self . _translations ( ) <EOL> return TAG [ "<STR_LIT>" ] ( instance , <EOL> bindings , <EOL> translations , <EOL> ) <EOL> def _instance ( self ) : <EOL> """<STR_LIT>""" <EOL> nodes = [ ] <EOL> append = nodes . append <EOL> for field in self . _fields : <EOL> append ( field . model ) <EOL> return TAG [ "<STR_LIT>" ] ( TAG [ self . name ] ( nodes ) , _id = self . name ) <EOL> def _bindings ( self ) : <EOL> """<STR_LIT>""" <EOL> bindings = [ ] <EOL> append = bindings . append <EOL> for field in self . _fields : <EOL> append ( field . binding ) <EOL> return TAG [ "<STR_LIT>" ] ( bindings ) <EOL> def _widgets ( self ) : <EOL> """<STR_LIT>""" <EOL> strings = self . _strings <EOL> widgets = [ ] <EOL> append = widgets . append <EOL> fields = self . _fields <EOL> for field in self . _fields : <EOL> append ( field . widget ) <EOL> return TAG [ "<STR_LIT>" ] ( widgets ) <EOL> def _strings ( self ) : <EOL> """<STR_LIT>""" <EOL> strings = { } <EOL> if self . translate : <EOL> update = strings . update <EOL> for field in self . _fields : <EOL> update ( field . strings ) <EOL> return strings <EOL> def _translations ( self ) : <EOL> """<STR_LIT>""" <EOL> T = current . T <EOL> translations = TAG [ "<STR_LIT>" ] ( ) <EOL> strings = self . _strings ( ) <EOL> if self . translate and strings : <EOL> append_translation = translations . append <EOL> languages = [ l for l in current . response . s3 . l10n_languages <EOL> if l != "<STR_LIT>" ] <EOL> languages . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> for language in languages : <EOL> translation = TAG [ "<STR_LIT>" ] ( _lang = language ) <EOL> append_string = translation . append <EOL> for key , string in strings . items ( ) : <EOL> tstr = T ( string . m if hasattr ( string , "<STR_LIT:m>" ) else string , <EOL> language = language , <EOL> ) <EOL> append_string ( TAG [ "<STR_LIT:text>" ] ( TAG [ "<STR_LIT:value>" ] ( tstr ) , _id = key ) ) <EOL> if len ( translation ) : <EOL> append_translation ( translation ) <EOL> return TAG [ "<STR_LIT>" ] ( translations ) </s>
