<s> import operator <EOL> import hashlib <EOL> from importlib import import_module <EOL> from django . conf import settings <EOL> from django . contrib import auth <EOL> from django . core . urlresolvers import NoReverseMatch <EOL> from rest_framework import serializers , exceptions , relations <EOL> from rest_framework . reverse import reverse <EOL> from oscar . core . loading import get_class <EOL> import oscar . models . fields <EOL> Selector = get_class ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def overridable ( name , default ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( settings , name , default ) <EOL> def expand_field_mapping ( extra_fields ) : <EOL> field_mapping = serializers . ModelSerializer . serializer_field_mapping <EOL> field_mapping . update ( extra_fields ) <EOL> return field_mapping <EOL> class OscarSerializer ( object ) : <EOL> field_mapping = expand_field_mapping ( { <EOL> oscar . models . fields . NullCharField : serializers . CharField <EOL> } ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> fields = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( OscarSerializer , self ) . __init__ ( * args , ** kwargs ) <EOL> if fields : <EOL> allowed = set ( fields ) <EOL> existing = set ( self . fields . keys ( ) ) <EOL> for field_name in existing - allowed : <EOL> self . fields . pop ( field_name ) <EOL> def to_native ( self , obj ) : <EOL> num_fields = len ( self . get_fields ( ) ) <EOL> native = super ( OscarSerializer , self ) . to_native ( obj ) <EOL> if num_fields == <NUM_LIT:1> : <EOL> _ , val = next ( native . iteritems ( ) ) <EOL> return val <EOL> return native <EOL> class OscarModelSerializer ( OscarSerializer , serializers . ModelSerializer ) : <EOL> """<STR_LIT>""" <EOL> class OscarHyperlinkedModelSerializer ( <EOL> OscarSerializer , serializers . HyperlinkedModelSerializer ) : <EOL> """<STR_LIT>""" <EOL> class DrillDownHyperlinkedIdentityField ( relations . HyperlinkedIdentityField ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> try : <EOL> self . extra_url_kwargs = kwargs . pop ( '<STR_LIT>' ) <EOL> except KeyError : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg ) <EOL> super ( DrillDownHyperlinkedIdentityField , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_extra_url_kwargs ( self , obj ) : <EOL> return { <EOL> key : operator . attrgetter ( path ) ( obj ) <EOL> for key , path in self . extra_url_kwargs . items ( ) <EOL> } <EOL> def get_url ( self , obj , view_name , request , format ) : <EOL> """<STR_LIT>""" <EOL> lookup_field = getattr ( obj , self . lookup_field , None ) <EOL> kwargs = { self . lookup_field : lookup_field } <EOL> kwargs . update ( self . get_extra_url_kwargs ( obj ) ) <EOL> if lookup_field is None : <EOL> return None <EOL> try : <EOL> return reverse ( view_name , kwargs = kwargs , request = request , format = format ) <EOL> except NoReverseMatch : <EOL> pass <EOL> if self . pk_url_kwarg != '<STR_LIT>' : <EOL> kwargs = { self . pk_url_kwarg : obj . pk } <EOL> kwargs . update ( self . get_extra_url_kwargs ( obj ) ) <EOL> try : <EOL> return reverse ( view_name , kwargs = kwargs , request = request , format = format ) <EOL> except NoReverseMatch : <EOL> pass <EOL> slug = getattr ( obj , self . slug_field , None ) <EOL> if slug : <EOL> kwargs = { self . slug_url_kwarg : slug } <EOL> kwargs . update ( self . get_extra_url_kwargs ( obj ) ) <EOL> try : <EOL> return reverse ( view_name , kwargs = kwargs , request = request , format = format ) <EOL> except NoReverseMatch : <EOL> pass <EOL> raise NoReverseMatch ( ) <EOL> def get_domain ( request ) : <EOL> return request . get_host ( ) . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> def login_and_upgrade_session ( request , user ) : <EOL> "<STR_LIT>" <EOL> parsed_session_uri = getattr ( request , '<STR_LIT>' , None ) <EOL> if parsed_session_uri is not None : <EOL> assert ( parsed_session_uri [ '<STR_LIT:type>' ] == '<STR_LIT>' ) <EOL> parsed_session_uri [ '<STR_LIT:type>' ] = "<STR_LIT>" <EOL> session_id = session_id_from_parsed_session_uri ( parsed_session_uri ) <EOL> request . session . clear ( ) <EOL> request . session . delete ( ) <EOL> request . session = get_session ( session_id ) <EOL> request . session [ auth . SESSION_KEY ] = user . pk <EOL> if hasattr ( user , '<STR_LIT>' ) : <EOL> request . session [ auth . HASH_SESSION_KEY ] = user . get_session_auth_hash ( ) <EOL> auth . login ( request , user ) <EOL> request . session . save ( ) <EOL> def session_id_from_parsed_session_uri ( parsed_session_uri ) : <EOL> session_id_base = u"<STR_LIT>" % ( <EOL> parsed_session_uri ) <EOL> combined = session_id_base + settings . SECRET_KEY <EOL> return hashlib . sha1 ( combined . encode ( ) ) . hexdigest ( ) <EOL> def get_session ( session_id , raise_on_create = False ) : <EOL> "<STR_LIT>" <EOL> engine = import_module ( settings . SESSION_ENGINE ) <EOL> session = engine . SessionStore ( session_id ) <EOL> if not session . exists ( session_id ) : <EOL> if raise_on_create : <EOL> raise exceptions . NotAuthenticated ( ) <EOL> else : <EOL> session . save ( must_create = True ) <EOL> else : <EOL> session . load ( ) <EOL> if session . session_key != session_id : <EOL> engine . SessionStore . clear_expired ( ) <EOL> session = get_session ( session_id , raise_on_create ) <EOL> return session </s>
