<s> from django . utils . translation import ugettext_lazy as _ <EOL> from rest_framework . exceptions import ParseError <EOL> from rest_framework . serializers import * <EOL> from rest_framework_json_api . relations import ResourceRelatedField <EOL> from rest_framework_json_api . utils import ( <EOL> get_resource_type_from_model , get_resource_type_from_instance , <EOL> get_resource_type_from_serializer , get_included_serializers ) <EOL> class ResourceIdentifierObjectSerializer ( BaseSerializer ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> model_class = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . model_class = kwargs . pop ( '<STR_LIT>' , self . model_class ) <EOL> if '<STR_LIT>' not in kwargs and not self . model_class : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> super ( ResourceIdentifierObjectSerializer , self ) . __init__ ( * args , ** kwargs ) <EOL> def to_representation ( self , instance ) : <EOL> return { <EOL> '<STR_LIT:type>' : get_resource_type_from_instance ( instance ) , <EOL> '<STR_LIT:id>' : str ( instance . pk ) <EOL> } <EOL> def to_internal_value ( self , data ) : <EOL> if data [ '<STR_LIT:type>' ] != get_resource_type_from_model ( self . model_class ) : <EOL> self . fail ( '<STR_LIT>' , model_type = self . model_class , received_type = data [ '<STR_LIT:type>' ] ) <EOL> pk = data [ '<STR_LIT:id>' ] <EOL> try : <EOL> return self . model_class . objects . get ( pk = pk ) <EOL> except ObjectDoesNotExist : <EOL> self . fail ( '<STR_LIT>' , pk_value = pk ) <EOL> except ( TypeError , ValueError ) : <EOL> self . fail ( '<STR_LIT>' , data_type = type ( data [ '<STR_LIT>' ] ) . __name__ ) <EOL> class SparseFieldsetsMixin ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> context = kwargs . get ( '<STR_LIT>' ) <EOL> request = context . get ( '<STR_LIT>' ) if context else None <EOL> if request : <EOL> sparse_fieldset_query_param = '<STR_LIT>' . format ( get_resource_type_from_serializer ( self ) ) <EOL> try : <EOL> param_name = next ( key for key in request . query_params if sparse_fieldset_query_param in key ) <EOL> except StopIteration : <EOL> pass <EOL> else : <EOL> fieldset = request . query_params . get ( param_name ) . split ( '<STR_LIT:U+002C>' ) <EOL> for field_name , field in self . fields . fields . copy ( ) . items ( ) : <EOL> if field_name == api_settings . URL_FIELD_NAME : <EOL> continue <EOL> if field_name not in fieldset : <EOL> self . fields . pop ( field_name ) <EOL> super ( SparseFieldsetsMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> class IncludedResourcesValidationMixin ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> context = kwargs . get ( '<STR_LIT>' ) <EOL> request = context . get ( '<STR_LIT>' ) if context else None <EOL> view = context . get ( '<STR_LIT>' ) if context else None <EOL> def validate_path ( serializer_class , field_path , path ) : <EOL> serializers = get_included_serializers ( serializer_class ) <EOL> if serializers is None : <EOL> raise ParseError ( '<STR_LIT>' ) <EOL> this_field_name = field_path [ <NUM_LIT:0> ] <EOL> this_included_serializer = serializers . get ( this_field_name ) <EOL> if this_included_serializer is None : <EOL> raise ParseError ( <EOL> '<STR_LIT>' . format ( <EOL> path <EOL> ) <EOL> ) <EOL> if len ( field_path ) > <NUM_LIT:1> : <EOL> new_included_field_path = field_path [ <NUM_LIT:1> : ] <EOL> validate_path ( this_included_serializer , new_included_field_path , path ) <EOL> if request and view : <EOL> include_resources_param = request . query_params . get ( '<STR_LIT>' ) if request else None <EOL> if include_resources_param : <EOL> included_resources = include_resources_param . split ( '<STR_LIT:U+002C>' ) <EOL> for included_field_name in included_resources : <EOL> included_field_path = included_field_name . split ( '<STR_LIT:.>' ) <EOL> this_serializer_class = view . get_serializer_class ( ) <EOL> validate_path ( this_serializer_class , included_field_path , included_field_name ) <EOL> super ( IncludedResourcesValidationMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> class HyperlinkedModelSerializer ( IncludedResourcesValidationMixin , SparseFieldsetsMixin , HyperlinkedModelSerializer ) : <EOL> """<STR_LIT>""" <EOL> class ModelSerializer ( IncludedResourcesValidationMixin , SparseFieldsetsMixin , ModelSerializer ) : <EOL> """<STR_LIT>""" <EOL> serializer_related_field = ResourceRelatedField <EOL> def get_field_names ( self , declared_fields , info ) : <EOL> """<STR_LIT>""" <EOL> meta_fields = getattr ( self . Meta , '<STR_LIT>' , [ ] ) <EOL> declared = OrderedDict ( ) <EOL> for field_name in set ( declared_fields . keys ( ) ) : <EOL> field = declared_fields [ field_name ] <EOL> if field_name not in meta_fields : <EOL> declared [ field_name ] = field <EOL> fields = super ( ModelSerializer , self ) . get_field_names ( declared , info ) <EOL> return list ( fields ) + list ( getattr ( self . Meta , '<STR_LIT>' , list ( ) ) ) </s>
