<s> from django import forms <EOL> from django . core . exceptions import ValidationError <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django_mongoengine . forms . widgets import Dictionary , EmbeddedFieldWidget <EOL> class ReferenceField ( forms . ModelChoiceField ) : <EOL> pass <EOL> class DocumentMultipleChoiceField ( forms . ModelMultipleChoiceField ) : <EOL> pass <EOL> class DictField ( forms . Field ) : <EOL> """<STR_LIT>""" <EOL> error_messages = { <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( u'<STR_LIT>' ) <EOL> } <EOL> invalid_keys = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> illegal_characters = [ '<STR_LIT:.>' ] <EOL> key_limit = <NUM_LIT:200> <EOL> max_depth = None <EOL> def __init__ ( self , max_depth = None , flags = None , sub_attrs = None , attrs = None , * args , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs . keys ( ) : <EOL> kwargs [ '<STR_LIT>' ] . update ( self . error_messages ) <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = self . error_messages <EOL> self . max_depth = ( max_depth if max_depth and max_depth >= <NUM_LIT:0> else None ) <EOL> if '<STR_LIT>' not in kwargs . keys ( ) : <EOL> schema = None <EOL> if '<STR_LIT>' in kwargs and not callable ( kwargs [ '<STR_LIT>' ] ) : <EOL> if isinstance ( kwargs [ '<STR_LIT>' ] , dict ) : <EOL> schema = kwargs [ '<STR_LIT>' ] <EOL> kwargs [ '<STR_LIT>' ] = Dictionary ( max_depth = max_depth , flags = flags , schema = schema , sub_attrs = sub_attrs ) <EOL> super ( DictField , self ) . __init__ ( * args , ** kwargs ) <EOL> def prepare_value ( self , value ) : <EOL> return value <EOL> def to_python ( self , value ) : <EOL> value = self . get_dict ( value ) <EOL> return value <EOL> def clean ( self , value ) : <EOL> self . max_depth = self . widget . max_depth <EOL> value = self . to_python ( value ) <EOL> self . validate ( value ) <EOL> return value <EOL> def get_dict ( self , a_list ) : <EOL> """<STR_LIT>""" <EOL> d = { } <EOL> for k in a_list : <EOL> if ( isinstance ( k , list ) ) : <EOL> if isinstance ( k [ <NUM_LIT:1> ] , list ) and k [ <NUM_LIT:0> ] : <EOL> d . update ( { k [ <NUM_LIT:0> ] : self . get_dict ( k [ <NUM_LIT:1> ] ) } ) <EOL> elif k [ <NUM_LIT:0> ] : <EOL> d . update ( { k [ <NUM_LIT:0> ] : k [ <NUM_LIT:1> ] } ) <EOL> return d <EOL> def validate ( self , value , depth = <NUM_LIT:0> ) : <EOL> if self . max_depth is not None and depth > self . max_depth : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . max_depth ) <EOL> for k , v in value . items ( ) : <EOL> self . run_validators ( k ) <EOL> if k in self . invalid_keys : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . invalid_keys ) <EOL> if len ( k ) > self . key_limit : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . key_limit ) <EOL> for u in self . illegal_characters : <EOL> if u in k : <EOL> raise ValidationError ( self . error_messages [ '<STR_LIT>' ] % self . illegal_characters ) <EOL> if isinstance ( v , dict ) : <EOL> self . validate ( v , depth + <NUM_LIT:1> ) <EOL> class EmbeddedDocumentField ( forms . MultiValueField ) : <EOL> def __init__ ( self , form , * args , ** kwargs ) : <EOL> self . form = form ( ) <EOL> kwargs [ '<STR_LIT>' ] = EmbeddedFieldWidget ( self . form . fields ) <EOL> kwargs [ '<STR_LIT>' ] = [ f . initial for f in self . form . fields . values ( ) ] <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> super ( EmbeddedDocumentField , self ) . __init__ ( fields = tuple ( [ f for f in self . form . fields . values ( ) ] ) , * args , ** kwargs ) <EOL> def bound_data ( self , data , initial ) : <EOL> return data <EOL> def prepare_value ( self , value ) : <EOL> return value <EOL> def compress ( self , data_list ) : <EOL> data = { } <EOL> if data_list : <EOL> data = dict ( ( f . name , data_list [ i ] ) for i , f in enumerate ( self . form ) ) <EOL> f = self . form . __class__ ( data ) <EOL> f . is_valid ( ) <EOL> return f . cleaned_data <EOL> return data <EOL> def clean ( self , value ) : <EOL> return self . to_python ( super ( EmbeddedDocumentField , self ) . clean ( value ) ) <EOL> def to_python ( self , value ) : <EOL> obj = self . form . _meta . model ( ) <EOL> [ obj . __setattr__ ( k , value [ k ] ) for k in value . keys ( ) ] <EOL> return obj </s>
