<s> import json <EOL> import six <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from rest_framework import serializers <EOL> class TagList ( list ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> pretty_print = kwargs . pop ( "<STR_LIT>" , True ) <EOL> list . __init__ ( self , * args , ** kwargs ) <EOL> self . pretty_print = pretty_print <EOL> def __add__ ( self , rhs ) : <EOL> return TagList ( list . __add__ ( self , rhs ) ) <EOL> def __getitem__ ( self , item ) : <EOL> result = list . __getitem__ ( self , item ) <EOL> try : <EOL> return TagList ( result ) <EOL> except TypeError : <EOL> return result <EOL> def __str__ ( self ) : <EOL> if self . pretty_print : <EOL> return json . dumps ( <EOL> self , sort_keys = True , indent = <NUM_LIT:4> , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) ) <EOL> else : <EOL> return json . dumps ( self ) <EOL> class TagListSerializerField ( serializers . Field ) : <EOL> child = serializers . CharField ( ) <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) <EOL> } <EOL> order_by = None <EOL> def __init__ ( self , ** kwargs ) : <EOL> pretty_print = kwargs . pop ( "<STR_LIT>" , True ) <EOL> style = kwargs . pop ( "<STR_LIT>" , { } ) <EOL> kwargs [ "<STR_LIT>" ] = { '<STR_LIT>' : '<STR_LIT>' } <EOL> kwargs [ "<STR_LIT>" ] . update ( style ) <EOL> super ( TagListSerializerField , self ) . __init__ ( ** kwargs ) <EOL> self . pretty_print = pretty_print <EOL> def to_internal_value ( self , value ) : <EOL> if isinstance ( value , six . string_types ) : <EOL> if not value : <EOL> value = "<STR_LIT>" <EOL> try : <EOL> value = json . loads ( value ) <EOL> except ValueError : <EOL> self . fail ( '<STR_LIT>' ) <EOL> if not isinstance ( value , list ) : <EOL> self . fail ( '<STR_LIT>' , input_type = type ( value ) . __name__ ) <EOL> for s in value : <EOL> if not isinstance ( s , six . string_types ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> self . child . run_validation ( s ) <EOL> return value <EOL> def to_representation ( self , value ) : <EOL> if not isinstance ( value , TagList ) : <EOL> if not isinstance ( value , list ) : <EOL> if self . order_by : <EOL> tags = value . all ( ) . order_by ( * self . order_by ) <EOL> else : <EOL> tags = value . all ( ) <EOL> value = [ tag . name for tag in tags ] <EOL> value = TagList ( value , pretty_print = self . pretty_print ) <EOL> return value <EOL> class TaggitSerializer ( serializers . Serializer ) : <EOL> def create ( self , validated_data ) : <EOL> to_be_tagged , validated_data = self . _pop_tags ( validated_data ) <EOL> tag_object = super ( TaggitSerializer , self ) . create ( validated_data ) <EOL> return self . _save_tags ( tag_object , to_be_tagged ) <EOL> def update ( self , instance , validated_data ) : <EOL> to_be_tagged , validated_data = self . _pop_tags ( validated_data ) <EOL> tag_object = super ( TaggitSerializer , self ) . update ( <EOL> instance , validated_data ) <EOL> return self . _save_tags ( tag_object , to_be_tagged ) <EOL> def _save_tags ( self , tag_object , tags ) : <EOL> for key in tags . keys ( ) : <EOL> tag_values = tags . get ( key ) <EOL> getattr ( tag_object , key ) . set ( * tag_values ) <EOL> return tag_object <EOL> def _pop_tags ( self , validated_data ) : <EOL> to_be_tagged = { } <EOL> for key in self . fields . keys ( ) : <EOL> field = self . fields [ key ] <EOL> if isinstance ( field , TagListSerializerField ) : <EOL> if key in validated_data : <EOL> to_be_tagged [ key ] = validated_data . pop ( key ) <EOL> return ( to_be_tagged , validated_data ) </s>
