<s> import sys <EOL> from collections import MutableMapping <EOL> from types import MethodType <EOL> from django . db . models . fields import FieldDoesNotExist <EOL> from django . utils . text import capfirst <EOL> from django . utils . functional import LazyObject , new_method_proxy <EOL> try : <EOL> from django . utils . text import camel_case_to_spaces <EOL> except ImportError : <EOL> from django . db . models . options import get_verbose_name as camel_case_to_spaces <EOL> from django . conf import settings <EOL> from mongoengine . fields import ReferenceField , ListField <EOL> def patch_document ( function , instance , bound = True ) : <EOL> if bound : <EOL> method = MethodType ( function , instance ) <EOL> else : <EOL> method = function <EOL> setattr ( instance , function . __name__ , method ) <EOL> def create_verbose_name ( name ) : <EOL> name = camel_case_to_spaces ( name ) <EOL> name = name . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> return name <EOL> class Relation ( object ) : <EOL> limit_choices_to = { } <EOL> def __init__ ( self , to ) : <EOL> self . _to = to <EOL> @ property <EOL> def to ( self ) : <EOL> if not isinstance ( self . _to . _meta , ( DocumentMetaWrapper , LazyDocumentMetaWrapper ) ) : <EOL> self . _to . _meta = DocumentMetaWrapper ( self . _to ) <EOL> return self . _to <EOL> @ to . setter <EOL> def to ( self , value ) : <EOL> self . _to = value <EOL> class PkWrapper ( object ) : <EOL> editable = False <EOL> fake = False <EOL> def __init__ ( self , wrapped ) : <EOL> self . obj = wrapped <EOL> def __getattr__ ( self , attr ) : <EOL> if attr in dir ( self . obj ) : <EOL> return getattr ( self . obj , attr ) <EOL> raise AttributeError <EOL> def __setattr__ ( self , attr , value ) : <EOL> if attr != '<STR_LIT>' and hasattr ( self . obj , attr ) : <EOL> setattr ( self . obj , attr , value ) <EOL> super ( PkWrapper , self ) . __setattr__ ( attr , value ) <EOL> class LazyDocumentMetaWrapper ( LazyObject ) : <EOL> _document = None <EOL> _meta = None <EOL> def __init__ ( self , document ) : <EOL> self . _document = document <EOL> self . _meta = document . _meta <EOL> super ( LazyDocumentMetaWrapper , self ) . __init__ ( ) <EOL> def _setup ( self ) : <EOL> self . _wrapped = DocumentMetaWrapper ( self . _document , self . _meta ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name in [ "<STR_LIT>" , "<STR_LIT>" , ] : <EOL> object . __setattr__ ( self , name , value ) <EOL> else : <EOL> super ( LazyDocumentMetaWrapper , self ) . __setattr__ ( name , value ) <EOL> __len__ = new_method_proxy ( len ) <EOL> @ new_method_proxy <EOL> def __contains__ ( self , key ) : <EOL> return key in self <EOL> class DocumentMetaWrapper ( MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> _deprecated_attrs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> pk = None <EOL> pk_name = None <EOL> _app_label = None <EOL> model_name = None <EOL> _verbose_name = None <EOL> has_auto_field = False <EOL> object_name = None <EOL> proxy = [ ] <EOL> proxied_children = [ ] <EOL> parents = { } <EOL> many_to_many = [ ] <EOL> _field_cache = None <EOL> document = None <EOL> _meta = None <EOL> concrete_model = None <EOL> concrete_managers = [ ] <EOL> virtual_fields = [ ] <EOL> auto_created = False <EOL> def __init__ ( self , document , meta = None ) : <EOL> super ( DocumentMetaWrapper , self ) . __init__ ( ) <EOL> self . document = document <EOL> self . concrete_model = document <EOL> if meta is None : <EOL> meta = getattr ( document , '<STR_LIT>' , { } ) <EOL> if isinstance ( meta , LazyDocumentMetaWrapper ) : <EOL> meta = meta . _meta <EOL> self . _meta = meta <EOL> try : <EOL> self . object_name = self . document . __name__ <EOL> except AttributeError : <EOL> self . object_name = self . document . __class__ . __name__ <EOL> self . model_name = self . object_name . lower ( ) <EOL> self . _setup_document_fields ( ) <EOL> self . _init_pk ( ) <EOL> def _setup_document_fields ( self ) : <EOL> for f in self . document . _fields . values ( ) : <EOL> if not hasattr ( f , '<STR_LIT>' ) : <EOL> if isinstance ( f , ReferenceField ) : <EOL> f . rel = Relation ( f . document_type ) <EOL> f . is_relation = True <EOL> elif isinstance ( f , ListField ) and isinstance ( f . field , ReferenceField ) : <EOL> f . field . rel = Relation ( f . field . document_type ) <EOL> f . field . is_relation = True <EOL> else : <EOL> f . many_to_many = None <EOL> f . many_to_one = None <EOL> f . one_to_many = None <EOL> f . one_to_one = None <EOL> f . related_model = None <EOL> f . rel = None <EOL> f . is_relation = False <EOL> if not hasattr ( f , '<STR_LIT>' ) or f . verbose_name is None : <EOL> f . verbose_name = capfirst ( create_verbose_name ( f . name ) ) <EOL> if not hasattr ( f , '<STR_LIT>' ) : <EOL> flat = [ ] <EOL> if f . choices is not None : <EOL> for choice , value in f . choices : <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> flat . extend ( value ) <EOL> else : <EOL> flat . append ( ( choice , value ) ) <EOL> f . flatchoices = flat <EOL> if isinstance ( f , ReferenceField ) and not isinstance ( f . document_type . _meta , ( DocumentMetaWrapper , LazyDocumentMetaWrapper ) ) and self . document != f . document_type : <EOL> f . document_type . _meta = LazyDocumentMetaWrapper ( f . document_type ) <EOL> if not hasattr ( f , '<STR_LIT>' ) : <EOL> f . auto_created = False <EOL> def _init_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . _meta : <EOL> self . pk_name = self . _meta [ '<STR_LIT>' ] <EOL> pk_field = getattr ( self . document , self . pk_name ) <EOL> else : <EOL> pk_field = None <EOL> self . pk = PkWrapper ( pk_field ) <EOL> def _get_pk_val ( obj ) : <EOL> return obj . pk <EOL> patch_document ( _get_pk_val , self . document , False ) <EOL> if pk_field is not None : <EOL> self . pk . name = self . pk_name <EOL> self . pk . attname = self . pk_name <EOL> else : <EOL> self . pk . fake = True <EOL> self . pk . editable = True <EOL> @ property <EOL> def app_label ( self ) : <EOL> if self . _app_label is None : <EOL> if self . _meta . get ( '<STR_LIT>' ) : <EOL> self . _app_label = self . _meta [ "<STR_LIT>" ] <EOL> else : <EOL> model_module = sys . modules [ self . document . __module__ ] <EOL> self . _app_label = model_module . __name__ . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:2> ] <EOL> return self . _app_label <EOL> @ property <EOL> def verbose_name ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _verbose_name is None : <EOL> verbose_name = self . _meta . get ( '<STR_LIT>' , self . object_name ) <EOL> self . _verbose_name = capfirst ( create_verbose_name ( verbose_name ) ) <EOL> return self . _verbose_name <EOL> @ property <EOL> def verbose_name_raw ( self ) : <EOL> return self . verbose_name <EOL> @ property <EOL> def verbose_name_plural ( self ) : <EOL> return "<STR_LIT>" % self . verbose_name <EOL> def get_add_permission ( self ) : <EOL> return '<STR_LIT>' % self . object_name . lower ( ) <EOL> def get_change_permission ( self ) : <EOL> return '<STR_LIT>' % self . object_name . lower ( ) <EOL> def get_delete_permission ( self ) : <EOL> return '<STR_LIT>' % self . object_name . lower ( ) <EOL> def get_ordered_objects ( self ) : <EOL> return [ ] <EOL> def get_field_by_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . document . _fields : <EOL> field = self . document . _fields [ name ] <EOL> if isinstance ( field , ReferenceField ) : <EOL> return ( field , field . document_type , False , False ) <EOL> else : <EOL> return ( field , None , True , False ) <EOL> else : <EOL> raise FieldDoesNotExist ( '<STR_LIT>' % <EOL> ( self . object_name , name ) ) <EOL> def get_field ( self , name , many_to_many = True ) : <EOL> """<STR_LIT>""" <EOL> return self . get_field_by_name ( name ) [ <NUM_LIT:0> ] <EOL> def get_fields ( self , include_hidden = False ) : <EOL> return self . document . _fields . values ( ) <EOL> @ property <EOL> def swapped ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _meta . get ( '<STR_LIT>' , False ) : <EOL> model_label = '<STR_LIT>' % ( self . app_label , self . object_name . lower ( ) ) <EOL> swapped_for = getattr ( settings , self . swappable , None ) <EOL> if swapped_for : <EOL> try : <EOL> swapped_label , swapped_object = swapped_for . split ( '<STR_LIT:.>' ) <EOL> except ValueError : <EOL> return swapped_for <EOL> if '<STR_LIT>' % ( swapped_label , swapped_object . lower ( ) ) not in ( None , model_label ) : <EOL> return swapped_for <EOL> return None <EOL> def __getattr__ ( self , name ) : <EOL> if name in self . _deprecated_attrs : <EOL> return getattr ( self , self . _deprecated_attrs . get ( name ) ) <EOL> try : <EOL> return self . _meta [ name ] <EOL> except KeyError : <EOL> raise AttributeError <EOL> def __setattr__ ( self , name , value ) : <EOL> if not hasattr ( self , name ) : <EOL> self . _meta [ name ] = value <EOL> else : <EOL> super ( DocumentMetaWrapper , self ) . __setattr__ ( name , value ) <EOL> def __contains__ ( self , key ) : <EOL> return key in self . _meta <EOL> def __getitem__ ( self , key ) : <EOL> return self . _meta [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _meta [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> return self . _meta . __delitem__ ( key ) <EOL> def __iter__ ( self ) : <EOL> return self . _meta . __iter__ ( ) <EOL> def __len__ ( self ) : <EOL> return self . _meta . __len__ ( ) <EOL> def __cmp__ ( self , other ) : <EOL> return hash ( self ) == hash ( other ) <EOL> def __hash__ ( self ) : <EOL> return id ( self ) <EOL> def get ( self , key , default = None ) : <EOL> try : <EOL> return self . __getitem__ ( key ) <EOL> except KeyError : <EOL> return default <EOL> def get_parent_list ( self ) : <EOL> return [ ] <EOL> def get_all_related_objects ( self , * args , ** kwargs ) : <EOL> return [ ] <EOL> def iteritems ( self ) : <EOL> return iter ( self . _meta . items ( ) ) </s>
