<s> from django . http import HttpResponseRedirect <EOL> from django . db . models import get_model <EOL> from socialoauth import socialsites <EOL> from socialoauth . utils import import_oauth_class <EOL> from socialoauth . exception import SocialAPIError <EOL> from . models import SocialUser <EOL> from . app_settings import ( <EOL> SOCIALOAUTH_SITES , <EOL> SOCIAL_LOGIN_USER_INFO_MODEL , <EOL> SOCIAL_LOGIN_DONE_REDIRECT_URL , <EOL> SOCIAL_LOGIN_ERROR_REDIRECT_URL , <EOL> ) <EOL> socialsites . config ( SOCIALOAUTH_SITES ) <EOL> def social_login_callback ( request , sitename ) : <EOL> code = request . GET . get ( '<STR_LIT:code>' , None ) <EOL> if not code : <EOL> return HttpResponseRedirect ( SOCIAL_LOGIN_ERROR_REDIRECT_URL ) <EOL> s = import_oauth_class ( socialsites [ sitename ] ) ( ) <EOL> try : <EOL> s . get_access_token ( code ) <EOL> except SocialAPIError : <EOL> return HttpResponseRedirect ( SOCIAL_LOGIN_ERROR_REDIRECT_URL ) <EOL> user_info_model = get_model ( * SOCIAL_LOGIN_USER_INFO_MODEL . split ( '<STR_LIT:.>' ) ) <EOL> try : <EOL> user = SocialUser . objects . get ( site_uid = s . uid , site_id = s . site_id ) <EOL> user_info_model . objects . filter ( user_id = user . user_id ) . update ( <EOL> username = s . name , avatar = s . avatar <EOL> ) <EOL> except SocialUser . DoesNotExist : <EOL> user = SocialUser . objects . create ( site_uid = s . uid , site_id = s . site_id ) <EOL> user_info_model . objects . create ( <EOL> user_id = user . user_id , <EOL> username = s . name , <EOL> avatar = s . avatar <EOL> ) <EOL> request . session [ '<STR_LIT>' ] = user . user_id <EOL> return HttpResponseRedirect ( SOCIAL_LOGIN_DONE_REDIRECT_URL ) </s>
