<s> """<STR_LIT>""" <EOL> from StringIO import StringIO <EOL> from django . db import models <EOL> from django . utils . encoding import smart_unicode <EOL> class SerializerDoesNotExist ( KeyError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SerializationError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class DeserializationError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Serializer ( object ) : <EOL> """<STR_LIT>""" <EOL> internal_use_only = False <EOL> def serialize ( self , queryset , ** options ) : <EOL> """<STR_LIT>""" <EOL> self . options = options <EOL> self . stream = options . pop ( "<STR_LIT>" , StringIO ( ) ) <EOL> self . selected_fields = options . pop ( "<STR_LIT>" , None ) <EOL> self . use_natural_keys = options . pop ( "<STR_LIT>" , False ) <EOL> self . start_serialization ( ) <EOL> for obj in queryset : <EOL> self . start_object ( obj ) <EOL> concrete_model = obj . _meta . concrete_model <EOL> for field in concrete_model . _meta . local_fields : <EOL> if field . serialize : <EOL> if field . rel is None : <EOL> if self . selected_fields is None or field . attname in self . selected_fields : <EOL> self . handle_field ( obj , field ) <EOL> else : <EOL> if self . selected_fields is None or field . attname [ : - <NUM_LIT:3> ] in self . selected_fields : <EOL> self . handle_fk_field ( obj , field ) <EOL> for field in concrete_model . _meta . many_to_many : <EOL> if field . serialize : <EOL> if self . selected_fields is None or field . attname in self . selected_fields : <EOL> self . handle_m2m_field ( obj , field ) <EOL> self . end_object ( obj ) <EOL> self . end_serialization ( ) <EOL> return self . getvalue ( ) <EOL> def start_serialization ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def end_serialization ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def start_object ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def end_object ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def handle_field ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def handle_fk_field ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def handle_m2m_field ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def getvalue ( self ) : <EOL> """<STR_LIT>""" <EOL> if callable ( getattr ( self . stream , '<STR_LIT>' , None ) ) : <EOL> return self . stream . getvalue ( ) <EOL> class Deserializer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream_or_string , ** options ) : <EOL> """<STR_LIT>""" <EOL> self . options = options <EOL> if isinstance ( stream_or_string , basestring ) : <EOL> self . stream = StringIO ( stream_or_string ) <EOL> else : <EOL> self . stream = stream_or_string <EOL> models . get_apps ( ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class DeserializedObject ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , obj , m2m_data = None ) : <EOL> self . object = obj <EOL> self . m2m_data = m2m_data <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . object . _meta . app_label , self . object . _meta . object_name , self . object . pk ) <EOL> def save ( self , save_m2m = True , using = None ) : <EOL> models . Model . save_base ( self . object , using = using , raw = True ) <EOL> if self . m2m_data and save_m2m : <EOL> for accessor_name , object_list in self . m2m_data . items ( ) : <EOL> setattr ( self . object , accessor_name , object_list ) <EOL> self . m2m_data = None </s>
