<s> import datetime <EOL> import sys <EOL> from peewee import Model <EOL> from flask_peewee . utils import get_dictionary_from_model <EOL> from flask_peewee . utils import get_model_from_dictionary <EOL> class Serializer ( object ) : <EOL> date_format = '<STR_LIT>' <EOL> time_format = '<STR_LIT>' <EOL> datetime_format = '<STR_LIT:U+0020>' . join ( [ date_format , time_format ] ) <EOL> def convert_value ( self , value ) : <EOL> if isinstance ( value , datetime . datetime ) : <EOL> return value . strftime ( self . datetime_format ) <EOL> elif isinstance ( value , datetime . date ) : <EOL> return value . strftime ( self . date_format ) <EOL> elif isinstance ( value , datetime . time ) : <EOL> return value . strftime ( self . time_format ) <EOL> elif isinstance ( value , Model ) : <EOL> return value . get_id ( ) <EOL> else : <EOL> return value <EOL> def clean_data ( self , data ) : <EOL> for key , value in data . items ( ) : <EOL> if isinstance ( value , dict ) : <EOL> self . clean_data ( value ) <EOL> elif isinstance ( value , ( list , tuple ) ) : <EOL> data [ key ] = map ( self . clean_data , value ) <EOL> else : <EOL> data [ key ] = self . convert_value ( value ) <EOL> return data <EOL> def serialize_object ( self , obj , fields = None , exclude = None ) : <EOL> data = get_dictionary_from_model ( obj , fields , exclude ) <EOL> return self . clean_data ( data ) <EOL> class Deserializer ( object ) : <EOL> def deserialize_object ( self , model , data ) : <EOL> return get_model_from_dictionary ( model , data ) </s>
