<s> from stream_framework . serializers . base import BaseSerializer <EOL> from stream_framework . utils import epoch_to_datetime , datetime_to_epoch <EOL> from stream_framework . verbs import get_verb_by_id <EOL> import pickle <EOL> import six <EOL> class ActivitySerializer ( BaseSerializer ) : <EOL> '''<STR_LIT>''' <EOL> def dumps ( self , activity ) : <EOL> self . check_type ( activity ) <EOL> activity_time = '<STR_LIT>' % datetime_to_epoch ( activity . time ) <EOL> parts = [ activity . actor_id , activity . verb . id , <EOL> activity . object_id , activity . target_id or <NUM_LIT:0> ] <EOL> extra_context = activity . extra_context . copy ( ) <EOL> pickle_string = '<STR_LIT>' <EOL> if extra_context : <EOL> pickle_string = pickle . dumps ( activity . extra_context ) <EOL> if six . PY3 : <EOL> pickle_string = pickle_string . decode ( '<STR_LIT>' ) <EOL> parts += [ activity_time , pickle_string ] <EOL> serialized_activity = '<STR_LIT:U+002C>' . join ( map ( str , parts ) ) <EOL> return serialized_activity <EOL> def loads ( self , serialized_activity ) : <EOL> parts = serialized_activity . split ( '<STR_LIT:U+002C>' , <NUM_LIT:5> ) <EOL> actor_id , verb_id , object_id , target_id = map ( <EOL> int , parts [ : <NUM_LIT:4> ] ) <EOL> activity_datetime = epoch_to_datetime ( float ( parts [ <NUM_LIT:4> ] ) ) <EOL> pickle_string = parts [ <NUM_LIT:5> ] <EOL> if not target_id : <EOL> target_id = None <EOL> verb = get_verb_by_id ( verb_id ) <EOL> extra_context = { } <EOL> if pickle_string : <EOL> if six . PY3 : <EOL> pickle_string = pickle_string . encode ( '<STR_LIT>' ) <EOL> extra_context = pickle . loads ( pickle_string ) <EOL> activity = self . activity_class ( actor_id , verb , object_id , target_id , <EOL> time = activity_datetime , extra_context = extra_context ) <EOL> return activity </s>
