<s> from __future__ import unicode_literals <EOL> from collections import OrderedDict <EOL> from django . core . exceptions import ImproperlyConfigured , ObjectDoesNotExist <EOL> from django . core . urlresolvers import ( <EOL> NoReverseMatch , Resolver404 , get_script_prefix , resolve <EOL> ) <EOL> from django . db . models import Manager <EOL> from django . db . models . query import QuerySet <EOL> from django . utils import six <EOL> from django . utils . encoding import smart_text <EOL> from django . utils . six . moves . urllib import parse as urlparse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from rest_framework . fields import ( <EOL> Field , empty , get_attribute , is_simple_callable , iter_options <EOL> ) <EOL> from rest_framework . reverse import reverse <EOL> from rest_framework . utils import html <EOL> def method_overridden ( method_name , klass , instance ) : <EOL> """<STR_LIT>""" <EOL> method = getattr ( klass , method_name ) <EOL> default_method = getattr ( method , '<STR_LIT>' , method ) <EOL> return default_method is not getattr ( instance , method_name ) . __func__ <EOL> class Hyperlink ( six . text_type ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( self , url , name ) : <EOL> ret = six . text_type . __new__ ( self , url ) <EOL> ret . name = name <EOL> return ret <EOL> def __getnewargs__ ( self ) : <EOL> return ( str ( self ) , self . name , ) <EOL> is_hyperlink = True <EOL> class PKOnlyObject ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pk ) : <EOL> self . pk = pk <EOL> MANY_RELATION_KWARGS = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:default>' , '<STR_LIT>' , '<STR_LIT:source>' , <EOL> '<STR_LIT:label>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> class RelatedField ( Field ) : <EOL> queryset = None <EOL> html_cutoff = <NUM_LIT:1000> <EOL> html_cutoff_text = _ ( '<STR_LIT>' ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . queryset = kwargs . pop ( '<STR_LIT>' , self . queryset ) <EOL> self . html_cutoff = kwargs . pop ( '<STR_LIT>' , self . html_cutoff ) <EOL> self . html_cutoff_text = kwargs . pop ( '<STR_LIT>' , self . html_cutoff_text ) <EOL> if not method_overridden ( '<STR_LIT>' , RelatedField , self ) : <EOL> assert self . queryset is not None or kwargs . get ( '<STR_LIT>' , None ) , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> assert not ( self . queryset is not None and kwargs . get ( '<STR_LIT>' , None ) ) , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( RelatedField , self ) . __init__ ( ** kwargs ) <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> if kwargs . pop ( '<STR_LIT>' , False ) : <EOL> return cls . many_init ( * args , ** kwargs ) <EOL> return super ( RelatedField , cls ) . __new__ ( cls , * args , ** kwargs ) <EOL> @ classmethod <EOL> def many_init ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> list_kwargs = { '<STR_LIT>' : cls ( * args , ** kwargs ) } <EOL> for key in kwargs . keys ( ) : <EOL> if key in MANY_RELATION_KWARGS : <EOL> list_kwargs [ key ] = kwargs [ key ] <EOL> return ManyRelatedField ( ** list_kwargs ) <EOL> def run_validation ( self , data = empty ) : <EOL> if data == '<STR_LIT>' : <EOL> data = None <EOL> return super ( RelatedField , self ) . run_validation ( data ) <EOL> def get_queryset ( self ) : <EOL> queryset = self . queryset <EOL> if isinstance ( queryset , ( QuerySet , Manager ) ) : <EOL> queryset = queryset . all ( ) <EOL> return queryset <EOL> def use_pk_only_optimization ( self ) : <EOL> return False <EOL> def get_attribute ( self , instance ) : <EOL> if self . use_pk_only_optimization ( ) and self . source_attrs : <EOL> try : <EOL> instance = get_attribute ( instance , self . source_attrs [ : - <NUM_LIT:1> ] ) <EOL> value = instance . serializable_value ( self . source_attrs [ - <NUM_LIT:1> ] ) <EOL> if is_simple_callable ( value ) : <EOL> value = value ( ) . pk <EOL> return PKOnlyObject ( pk = value ) <EOL> except AttributeError : <EOL> pass <EOL> return get_attribute ( instance , self . source_attrs ) <EOL> @ property <EOL> def choices ( self ) : <EOL> queryset = self . get_queryset ( ) <EOL> if queryset is None : <EOL> return { } <EOL> return OrderedDict ( [ <EOL> ( <EOL> six . text_type ( self . to_representation ( item ) ) , <EOL> self . display_value ( item ) <EOL> ) <EOL> for item in queryset <EOL> ] ) <EOL> @ property <EOL> def grouped_choices ( self ) : <EOL> return self . choices <EOL> def iter_options ( self ) : <EOL> return iter_options ( <EOL> self . grouped_choices , <EOL> cutoff = self . html_cutoff , <EOL> cutoff_text = self . html_cutoff_text <EOL> ) <EOL> def display_value ( self , instance ) : <EOL> return six . text_type ( instance ) <EOL> class StringRelatedField ( RelatedField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> super ( StringRelatedField , self ) . __init__ ( ** kwargs ) <EOL> def to_representation ( self , value ) : <EOL> return six . text_type ( value ) <EOL> class PrimaryKeyRelatedField ( RelatedField ) : <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . pk_field = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( PrimaryKeyRelatedField , self ) . __init__ ( ** kwargs ) <EOL> def use_pk_only_optimization ( self ) : <EOL> return True <EOL> def to_internal_value ( self , data ) : <EOL> if self . pk_field is not None : <EOL> data = self . pk_field . to_internal_value ( data ) <EOL> try : <EOL> return self . get_queryset ( ) . get ( pk = data ) <EOL> except ObjectDoesNotExist : <EOL> self . fail ( '<STR_LIT>' , pk_value = data ) <EOL> except ( TypeError , ValueError ) : <EOL> self . fail ( '<STR_LIT>' , data_type = type ( data ) . __name__ ) <EOL> def to_representation ( self , value ) : <EOL> if self . pk_field is not None : <EOL> return self . pk_field . to_representation ( value . pk ) <EOL> return value . pk <EOL> class HyperlinkedRelatedField ( RelatedField ) : <EOL> lookup_field = '<STR_LIT>' <EOL> view_name = None <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , view_name = None , ** kwargs ) : <EOL> if view_name is not None : <EOL> self . view_name = view_name <EOL> assert self . view_name is not None , '<STR_LIT>' <EOL> self . lookup_field = kwargs . pop ( '<STR_LIT>' , self . lookup_field ) <EOL> self . lookup_url_kwarg = kwargs . pop ( '<STR_LIT>' , self . lookup_field ) <EOL> self . format = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . reverse = reverse <EOL> super ( HyperlinkedRelatedField , self ) . __init__ ( ** kwargs ) <EOL> def use_pk_only_optimization ( self ) : <EOL> return self . lookup_field == '<STR_LIT>' <EOL> def get_object ( self , view_name , view_args , view_kwargs ) : <EOL> """<STR_LIT>""" <EOL> lookup_value = view_kwargs [ self . lookup_url_kwarg ] <EOL> lookup_kwargs = { self . lookup_field : lookup_value } <EOL> return self . get_queryset ( ) . get ( ** lookup_kwargs ) <EOL> def get_url ( self , obj , view_name , request , format ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( obj , '<STR_LIT>' ) and obj . pk in ( None , '<STR_LIT>' ) : <EOL> return None <EOL> lookup_value = getattr ( obj , self . lookup_field ) <EOL> kwargs = { self . lookup_url_kwarg : lookup_value } <EOL> return self . reverse ( view_name , kwargs = kwargs , request = request , format = format ) <EOL> def get_name ( self , obj ) : <EOL> return six . text_type ( obj ) <EOL> def to_internal_value ( self , data ) : <EOL> request = self . context . get ( '<STR_LIT>' , None ) <EOL> try : <EOL> http_prefix = data . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except AttributeError : <EOL> self . fail ( '<STR_LIT>' , data_type = type ( data ) . __name__ ) <EOL> if http_prefix : <EOL> data = urlparse . urlparse ( data ) . path <EOL> prefix = get_script_prefix ( ) <EOL> if data . startswith ( prefix ) : <EOL> data = '<STR_LIT:/>' + data [ len ( prefix ) : ] <EOL> try : <EOL> match = resolve ( data ) <EOL> except Resolver404 : <EOL> self . fail ( '<STR_LIT>' ) <EOL> try : <EOL> expected_viewname = request . versioning_scheme . get_versioned_viewname ( <EOL> self . view_name , request <EOL> ) <EOL> except AttributeError : <EOL> expected_viewname = self . view_name <EOL> if match . view_name != expected_viewname : <EOL> self . fail ( '<STR_LIT>' ) <EOL> try : <EOL> return self . get_object ( match . view_name , match . args , match . kwargs ) <EOL> except ( ObjectDoesNotExist , TypeError , ValueError ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def to_representation ( self , value ) : <EOL> request = self . context . get ( '<STR_LIT>' , None ) <EOL> format = self . context . get ( '<STR_LIT>' , None ) <EOL> assert request is not None , ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . __class__ . __name__ <EOL> ) <EOL> if format and self . format and self . format != format : <EOL> format = self . format <EOL> try : <EOL> url = self . get_url ( value , self . view_name , request , format ) <EOL> except NoReverseMatch : <EOL> msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if value in ( '<STR_LIT>' , None ) : <EOL> value_string = { '<STR_LIT>' : '<STR_LIT>' , None : '<STR_LIT:None>' } [ value ] <EOL> msg += ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % value_string <EOL> ) <EOL> raise ImproperlyConfigured ( msg % self . view_name ) <EOL> if url is None : <EOL> return None <EOL> name = self . get_name ( value ) <EOL> return Hyperlink ( url , name ) <EOL> class HyperlinkedIdentityField ( HyperlinkedRelatedField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , view_name = None , ** kwargs ) : <EOL> assert view_name is not None , '<STR_LIT>' <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> kwargs [ '<STR_LIT:source>' ] = '<STR_LIT:*>' <EOL> super ( HyperlinkedIdentityField , self ) . __init__ ( view_name , ** kwargs ) <EOL> def use_pk_only_optimization ( self ) : <EOL> return False <EOL> class SlugRelatedField ( RelatedField ) : <EOL> """<STR_LIT>""" <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> } <EOL> def __init__ ( self , slug_field = None , ** kwargs ) : <EOL> assert slug_field is not None , '<STR_LIT>' <EOL> self . slug_field = slug_field <EOL> super ( SlugRelatedField , self ) . __init__ ( ** kwargs ) <EOL> def to_internal_value ( self , data ) : <EOL> try : <EOL> return self . get_queryset ( ) . get ( ** { self . slug_field : data } ) <EOL> except ObjectDoesNotExist : <EOL> self . fail ( '<STR_LIT>' , slug_name = self . slug_field , value = smart_text ( data ) ) <EOL> except ( TypeError , ValueError ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def to_representation ( self , obj ) : <EOL> return getattr ( obj , self . slug_field ) <EOL> class ManyRelatedField ( Field ) : <EOL> """<STR_LIT>""" <EOL> initial = [ ] <EOL> default_empty_html = [ ] <EOL> default_error_messages = { <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) <EOL> } <EOL> html_cutoff = <NUM_LIT:1000> <EOL> html_cutoff_text = _ ( '<STR_LIT>' ) <EOL> def __init__ ( self , child_relation = None , * args , ** kwargs ) : <EOL> self . child_relation = child_relation <EOL> self . allow_empty = kwargs . pop ( '<STR_LIT>' , True ) <EOL> self . html_cutoff = kwargs . pop ( '<STR_LIT>' , self . html_cutoff ) <EOL> self . html_cutoff_text = kwargs . pop ( '<STR_LIT>' , self . html_cutoff_text ) <EOL> assert child_relation is not None , '<STR_LIT>' <EOL> super ( ManyRelatedField , self ) . __init__ ( * args , ** kwargs ) <EOL> self . child_relation . bind ( field_name = '<STR_LIT>' , parent = self ) <EOL> def get_value ( self , dictionary ) : <EOL> if html . is_html_input ( dictionary ) : <EOL> if self . field_name not in dictionary : <EOL> if getattr ( self . root , '<STR_LIT>' , False ) : <EOL> return empty <EOL> return dictionary . getlist ( self . field_name ) <EOL> return dictionary . get ( self . field_name , empty ) <EOL> def to_internal_value ( self , data ) : <EOL> if isinstance ( data , type ( '<STR_LIT>' ) ) or not hasattr ( data , '<STR_LIT>' ) : <EOL> self . fail ( '<STR_LIT>' , input_type = type ( data ) . __name__ ) <EOL> if not self . allow_empty and len ( data ) == <NUM_LIT:0> : <EOL> self . fail ( '<STR_LIT>' ) <EOL> return [ <EOL> self . child_relation . to_internal_value ( item ) <EOL> for item in data <EOL> ] <EOL> def get_attribute ( self , instance ) : <EOL> if hasattr ( instance , '<STR_LIT>' ) and instance . pk is None : <EOL> return [ ] <EOL> relationship = get_attribute ( instance , self . source_attrs ) <EOL> return relationship . all ( ) if ( hasattr ( relationship , '<STR_LIT:all>' ) ) else relationship <EOL> def to_representation ( self , iterable ) : <EOL> return [ <EOL> self . child_relation . to_representation ( value ) <EOL> for value in iterable <EOL> ] <EOL> @ property <EOL> def choices ( self ) : <EOL> return self . child_relation . choices <EOL> @ property <EOL> def grouped_choices ( self ) : <EOL> return self . choices <EOL> def iter_options ( self ) : <EOL> return iter_options ( <EOL> self . grouped_choices , <EOL> cutoff = self . html_cutoff , <EOL> cutoff_text = self . html_cutoff_text <EOL> ) </s>
