<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from django . conf import settings <EOL> from django . core . serializers import base <EOL> from django . db import models , DEFAULT_DB_ALIAS <EOL> from django . utils . encoding import smart_text , is_protected_type <EOL> from django . utils import six <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 ( self . get_dump_object ( obj ) ) <EOL> self . _current = None <EOL> def get_dump_object ( self , obj ) : <EOL> return { <EOL> "<STR_LIT>" : smart_text ( obj . _get_pk_val ( ) , strings_only = True ) , <EOL> "<STR_LIT>" : smart_text ( obj . _meta ) , <EOL> "<STR_LIT>" : self . _current <EOL> } <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> if self . use_natural_keys and hasattr ( field . rel . to , '<STR_LIT>' ) : <EOL> related = getattr ( obj , field . name ) <EOL> if related : <EOL> value = related . natural_key ( ) <EOL> else : <EOL> value = None <EOL> else : <EOL> value = getattr ( obj , field . get_attname ( ) ) <EOL> self . _current [ field . name ] = value <EOL> def handle_m2m_field ( self , obj , field ) : <EOL> if field . rel . through . _meta . auto_created : <EOL> if self . use_natural_keys and hasattr ( field . rel . to , '<STR_LIT>' ) : <EOL> m2m_value = lambda value : value . natural_key ( ) <EOL> else : <EOL> m2m_value = lambda value : smart_text ( value . _get_pk_val ( ) , strings_only = True ) <EOL> self . _current [ field . name ] = [ m2m_value ( related ) <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> db = options . pop ( '<STR_LIT>' , DEFAULT_DB_ALIAS ) <EOL> ignore = options . pop ( '<STR_LIT>' , False ) <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 . get ( "<STR_LIT>" , None ) ) } <EOL> m2m_data = { } <EOL> model_fields = Model . _meta . get_all_field_names ( ) <EOL> for ( field_name , field_value ) in six . iteritems ( d [ "<STR_LIT>" ] ) : <EOL> if ignore and field_name not in model_fields : <EOL> continue <EOL> if isinstance ( field_value , str ) : <EOL> field_value = smart_text ( 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> if hasattr ( field . rel . to . _default_manager , '<STR_LIT>' ) : <EOL> def m2m_convert ( value ) : <EOL> if hasattr ( value , '<STR_LIT>' ) and not isinstance ( value , six . text_type ) : <EOL> return field . rel . to . _default_manager . db_manager ( db ) . get_by_natural_key ( * value ) . pk <EOL> else : <EOL> return smart_text ( field . rel . to . _meta . pk . to_python ( value ) ) <EOL> else : <EOL> m2m_convert = lambda v : smart_text ( field . rel . to . _meta . pk . to_python ( v ) ) <EOL> m2m_data [ field . name ] = [ m2m_convert ( pk ) for pk in field_value ] <EOL> elif field . rel and isinstance ( field . rel , models . ManyToOneRel ) : <EOL> if field_value is not None : <EOL> if hasattr ( field . rel . to . _default_manager , '<STR_LIT>' ) : <EOL> if hasattr ( field_value , '<STR_LIT>' ) and not isinstance ( field_value , six . text_type ) : <EOL> obj = field . rel . to . _default_manager . db_manager ( db ) . get_by_natural_key ( * field_value ) <EOL> value = getattr ( obj , field . rel . field_name ) <EOL> if field . rel . to . _meta . pk . rel : <EOL> value = value . pk <EOL> else : <EOL> value = field . rel . to . _meta . get_field ( field . rel . field_name ) . to_python ( field_value ) <EOL> data [ field . attname ] = value <EOL> else : <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 ( "<STR_LIT>" % model_identifier ) <EOL> return Model </s>
