<s> """<STR_LIT>""" <EOL> import inspect <EOL> from werkzeug import secure_filename <EOL> from wtforms import Form , validators , fields as f <EOL> from fields import ModelSelectField , ModelSelectMultipleField , ListField <EOL> from mongoengine . fields import ReferenceField , IntField , FloatField <EOL> from flask_superadmin . model import AdminModelConverter as AdminModelConverter_ <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def converts ( * args ) : <EOL> """<STR_LIT>""" <EOL> def _inner ( func ) : <EOL> func . _converter_for = frozenset ( args ) <EOL> return func <EOL> return _inner <EOL> class ModelConverter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , converters = None ) : <EOL> if not converters : <EOL> converters = { } <EOL> for name in dir ( self ) : <EOL> obj = getattr ( self , name ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> for classname in obj . _converter_for : <EOL> converters [ classname ] = obj <EOL> self . converters = converters <EOL> def convert ( self , model , field , field_args , multiple = False ) : <EOL> kwargs = { <EOL> '<STR_LIT:label>' : unicode ( field . verbose_name or field . name or '<STR_LIT>' ) , <EOL> '<STR_LIT:description>' : field . help_text or '<STR_LIT>' , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:default>' : field . default , <EOL> } <EOL> if field_args : <EOL> kwargs . update ( field_args ) <EOL> if field . required : <EOL> if isinstance ( field , IntField ) or isinstance ( field , FloatField ) : <EOL> kwargs [ '<STR_LIT>' ] . append ( validators . InputRequired ( ) ) <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] . append ( validators . Required ( ) ) <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] . append ( validators . Optional ( ) ) <EOL> if field . choices : <EOL> kwargs [ '<STR_LIT>' ] = field . choices <EOL> if isinstance ( field , IntField ) : <EOL> kwargs [ '<STR_LIT>' ] = int <EOL> if not multiple : <EOL> return f . SelectField ( ** kwargs ) <EOL> else : <EOL> return f . SelectMultipleField ( ** kwargs ) <EOL> ftype = type ( field ) . __name__ <EOL> if hasattr ( field , '<STR_LIT>' ) : <EOL> return field . to_form_field ( model , kwargs ) <EOL> if ftype in self . converters : <EOL> return self . converters [ ftype ] ( model , field , kwargs ) <EOL> @ classmethod <EOL> def _string_common ( cls , model , field , kwargs ) : <EOL> if field . max_length or field . min_length : <EOL> kwargs [ '<STR_LIT>' ] . append ( <EOL> validators . Length ( max = field . max_length or - <NUM_LIT:1> , <EOL> min = field . min_length or - <NUM_LIT:1> ) ) <EOL> @ classmethod <EOL> def _number_common ( cls , model , field , kwargs ) : <EOL> if field . max_value or field . min_value : <EOL> kwargs [ '<STR_LIT>' ] . append ( <EOL> validators . NumberRange ( max = field . max_value , <EOL> min = field . min_value ) ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_String ( self , model , field , kwargs ) : <EOL> if field . regex : <EOL> kwargs [ '<STR_LIT>' ] . append ( validators . Regexp ( regex = field . regex ) ) <EOL> self . _string_common ( model , field , kwargs ) <EOL> if field . max_length : <EOL> return f . TextField ( ** kwargs ) <EOL> return f . TextAreaField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_URL ( self , model , field , kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] . append ( validators . URL ( ) ) <EOL> self . _string_common ( model , field , kwargs ) <EOL> return f . TextField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Email ( self , model , field , kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] . append ( validators . Email ( ) ) <EOL> self . _string_common ( model , field , kwargs ) <EOL> return f . TextField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Int ( self , model , field , kwargs ) : <EOL> self . _number_common ( model , field , kwargs ) <EOL> return f . IntegerField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Float ( self , model , field , kwargs ) : <EOL> self . _number_common ( model , field , kwargs ) <EOL> return f . FloatField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_File ( self , model , field , kwargs ) : <EOL> return f . FileField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Image ( self , model , field , kwargs ) : <EOL> return f . FileField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Decimal ( self , model , field , kwargs ) : <EOL> self . _number_common ( model , field , kwargs ) <EOL> return f . DecimalField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Boolean ( self , model , field , kwargs ) : <EOL> return f . BooleanField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_DateTime ( self , model , field , kwargs ) : <EOL> return f . DateTimeField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Binary ( self , model , field , kwargs ) : <EOL> if field . max_bytes : <EOL> kwargs [ '<STR_LIT>' ] . append ( validators . Length ( max = field . max_bytes ) ) <EOL> return f . TextAreaField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Dict ( self , model , field , kwargs ) : <EOL> return f . TextAreaField ( ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_List ( self , model , field , kwargs ) : <EOL> kwargs = kwargs or { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:label>' : unicode ( field . verbose_name or field . name or '<STR_LIT>' ) , <EOL> } <EOL> if field . field . choices : <EOL> return self . convert ( model , field . field , None , multiple = True ) <EOL> if isinstance ( field . field , ReferenceField ) : <EOL> return ModelSelectMultipleField ( model = field . field . document_type , ** kwargs ) <EOL> unbound_field = self . convert ( model , field . field , { } ) <EOL> return ListField ( unbound_field , min_entries = <NUM_LIT:0> , ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_SortedList ( self , model , field , kwargs ) : <EOL> return self . conv_List ( model , field , kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_GeoLocation ( self , model , field , kwargs ) : <EOL> return <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_ObjectId ( self , model , field , kwargs ) : <EOL> return <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_EmbeddedDocument ( self , model , field , kwargs ) : <EOL> kwargs = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> form_class = model_form ( field . document_type_obj , field_args = { } , <EOL> converter = self ) <EOL> return f . FormField ( form_class , ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_Reference ( self , model , field , kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = not field . required <EOL> return ModelSelectField ( model = field . document_type , ** kwargs ) <EOL> @ converts ( '<STR_LIT>' ) <EOL> def conv_GenericReference ( self , model , field , kwargs ) : <EOL> return <EOL> def model_fields ( model , fields = None , readonly_fields = None , exclude = None , <EOL> field_args = None , converter = None ) : <EOL> """<STR_LIT>""" <EOL> from mongoengine . base import BaseDocument <EOL> if BaseDocument not in inspect . getmro ( model ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> readonly_fields = readonly_fields or [ ] <EOL> exclude = exclude or [ ] <EOL> converter = converter or ModelConverter ( ) <EOL> field_args = field_args or { } <EOL> field_names = fields if fields else model . _fields . keys ( ) <EOL> field_names = ( x for x in field_names if x not in exclude ) <EOL> field_dict = { } <EOL> for name in field_names : <EOL> if name not in readonly_fields and name not in model . _fields : <EOL> raise KeyError ( '<STR_LIT>' % name ) <EOL> if name in model . _fields and name not in readonly_fields : <EOL> model_field = model . _fields [ name ] <EOL> field = converter . convert ( model , model_field , field_args . get ( name ) ) <EOL> if field is not None : <EOL> field_dict [ name ] = field <EOL> return field_dict <EOL> import mongoengine . fields as fields <EOL> _unset_value = object ( ) <EOL> _remove_file_value = object ( ) <EOL> def data_to_field ( field , data ) : <EOL> if isinstance ( field , fields . EmbeddedDocumentField ) : <EOL> return data_to_document ( field . document_type_obj , data ) <EOL> elif isinstance ( field , ( fields . ListField , fields . SequenceField , <EOL> fields . SortedListField ) ) : <EOL> l = [ ] <EOL> for d in data : <EOL> l . append ( data_to_field ( field . field , d ) ) <EOL> return l <EOL> elif isinstance ( field , ( fields . FileField ) ) : <EOL> if data . filename : <EOL> gfs = field . proxy_class ( <EOL> db_alias = field . db_alias , <EOL> collection_name = field . collection_name , <EOL> instance = field . owner_document ( ) , <EOL> key = field . name ) <EOL> gfs . put ( data . stream , filename = secure_filename ( data . filename ) , content_type = data . mimetype ) <EOL> return gfs <EOL> elif data . clear : <EOL> return _remove_file_value <EOL> return _unset_value <EOL> elif isinstance ( field , ( fields . ReferenceField , fields . ObjectIdField ) ) and isinstance ( data , basestring ) : <EOL> from bson . objectid import ObjectId <EOL> return ObjectId ( data ) <EOL> else : <EOL> return data <EOL> def data_to_document ( document , data ) : <EOL> from inspect import isclass <EOL> new = document ( ) if isclass ( document ) else document <EOL> for name , value in data . iteritems ( ) : <EOL> field = getattr ( new . __class__ , name ) <EOL> field_value = data_to_field ( field , value ) <EOL> if field_value != _unset_value : <EOL> if field_value == _remove_file_value : <EOL> getattr ( new , name ) . delete ( ) <EOL> else : <EOL> setattr ( new , name , field_value ) <EOL> return new <EOL> def model_form ( model , base_class = Form , fields = None , readonly_fields = None , <EOL> exclude = None , field_args = None , converter = None ) : <EOL> """<STR_LIT>""" <EOL> field_dict = model_fields ( model , fields , readonly_fields , exclude , <EOL> field_args , converter ) <EOL> field_dict [ '<STR_LIT>' ] = model <EOL> def populate_obj ( self , obj ) : <EOL> return data_to_document ( obj , self . data ) <EOL> field_dict [ '<STR_LIT>' ] = populate_obj <EOL> return type ( model . __name__ + '<STR_LIT>' , ( base_class , ) , field_dict ) <EOL> class AdminModelConverter ( AdminModelConverter_ , ModelConverter ) : <EOL> pass </s>
