<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from django . core . serializers import base <EOL> from django . db import models <EOL> from django . utils . encoding import smart_unicode , is_protected_type <EOL> class Serializer ( base . Serializer ) : <EOL> """<STR_LIT>""" <EOL> internal_use_only = True <EOL> def start_serialization ( self ) : <EOL> self . _current = None <EOL> self . objects = [ ] <EOL> def end_serialization ( self ) : <EOL> pass <EOL> def start_object ( self , obj ) : <EOL> self . _current = { } <EOL> def end_object ( self , obj ) : <EOL> self . objects . append ( { <EOL> "<STR_LIT>" : smart_unicode ( obj . _meta ) , <EOL> "<STR_LIT>" : smart_unicode ( obj . _get_pk_val ( ) , strings_only = True ) , <EOL> "<STR_LIT>" : self . _current <EOL> } ) <EOL> self . _current = None <EOL> def handle_field ( self , obj , field ) : <EOL> value = field . _get_val_from_obj ( obj ) <EOL> if is_protected_type ( value ) : <EOL> self . _current [ field . name ] = value <EOL> else : <EOL> self . _current [ field . name ] = field . value_to_string ( obj ) <EOL> def handle_fk_field ( self , obj , field ) : <EOL> related = getattr ( obj , field . name ) <EOL> if related is not None : <EOL> if field . rel . field_name == related . _meta . pk . name : <EOL> related = related . _get_pk_val ( ) <EOL> else : <EOL> related = getattr ( related , field . rel . field_name ) <EOL> self . _current [ field . name ] = smart_unicode ( related , strings_only = True ) <EOL> def handle_m2m_field ( self , obj , field ) : <EOL> if field . creates_table : <EOL> self . _current [ field . name ] = [ smart_unicode ( related . _get_pk_val ( ) , strings_only = True ) <EOL> for related in getattr ( obj , field . name ) . iterator ( ) ] <EOL> def getvalue ( self ) : <EOL> return self . objects <EOL> def Deserializer ( object_list , ** options ) : <EOL> """<STR_LIT>""" <EOL> models . get_apps ( ) <EOL> for d in object_list : <EOL> Model = _get_model ( d [ "<STR_LIT>" ] ) <EOL> data = { Model . _meta . pk . attname : Model . _meta . pk . to_python ( d [ "<STR_LIT>" ] ) } <EOL> m2m_data = { } <EOL> for ( field_name , field_value ) in d [ "<STR_LIT>" ] . iteritems ( ) : <EOL> if isinstance ( field_value , str ) : <EOL> field_value = smart_unicode ( field_value , options . get ( "<STR_LIT>" , settings . DEFAULT_CHARSET ) , strings_only = True ) <EOL> field = Model . _meta . get_field ( field_name ) <EOL> if field . rel and isinstance ( field . rel , models . ManyToManyRel ) : <EOL> m2m_convert = field . rel . to . _meta . pk . to_python <EOL> m2m_data [ field . name ] = [ m2m_convert ( smart_unicode ( pk ) ) for pk in field_value ] <EOL> elif field . rel and isinstance ( field . rel , models . ManyToOneRel ) : <EOL> if field_value is not None : <EOL> data [ field . attname ] = field . rel . to . _meta . get_field ( field . rel . field_name ) . to_python ( field_value ) <EOL> else : <EOL> data [ field . attname ] = None <EOL> else : <EOL> data [ field . name ] = field . to_python ( field_value ) <EOL> yield base . DeserializedObject ( Model ( ** data ) , m2m_data ) <EOL> def _get_model ( model_identifier ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> Model = models . get_model ( * model_identifier . split ( "<STR_LIT:.>" ) ) <EOL> except TypeError : <EOL> Model = None <EOL> if Model is None : <EOL> raise base . DeserializationError ( u"<STR_LIT>" % model_identifier ) <EOL> return Model </s>
