<s> from django . conf import settings <EOL> from django . contrib . auth import logout <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . generic . base import View , TemplateView <EOL> from socialregistration . clients . oauth import OAuthError <EOL> from socialregistration . contrib . openid . client import OpenIDClient <EOL> from socialregistration . mixins import SocialRegistration <EOL> import logging <EOL> import socket <EOL> GENERATE_USERNAME = getattr ( settings , '<STR_LIT>' , False ) <EOL> USERNAME_FUNCTION = getattr ( settings , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> FORM_CLASS = getattr ( settings , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> INITAL_DATA_FUNCTION = getattr ( settings , '<STR_LIT>' , <EOL> None ) <EOL> CONTEXT_FUNCTION = getattr ( settings , '<STR_LIT>' , <EOL> None ) <EOL> ALLOW_OPENID_SIGNUPS = getattr ( settings , '<STR_LIT>' , <EOL> True ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Setup ( SocialRegistration , View ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get_form ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . import_attribute ( FORM_CLASS ) <EOL> def get_username_function ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . import_attribute ( USERNAME_FUNCTION ) <EOL> def get_initial_data ( self , request , user , profile , client ) : <EOL> """<STR_LIT>""" <EOL> if INITAL_DATA_FUNCTION : <EOL> func = self . import_attribute ( INITAL_DATA_FUNCTION ) <EOL> return func ( request , user , profile , client ) <EOL> return { } <EOL> def get_context ( self , request , user , profile , client ) : <EOL> """<STR_LIT>""" <EOL> if CONTEXT_FUNCTION : <EOL> func = self . import_attribute ( CONTEXT_FUNCTION ) <EOL> return func ( request , user , profile , client ) <EOL> return { } <EOL> def generate_username_and_redirect ( self , request , user , profile , client ) : <EOL> """<STR_LIT>""" <EOL> func = self . get_username_function ( ) <EOL> user . username = func ( user , profile , client ) <EOL> user . set_unusable_password ( ) <EOL> user . save ( ) <EOL> profile . user = user <EOL> profile . save ( ) <EOL> user = profile . authenticate ( ) <EOL> self . send_connect_signal ( request , user , profile , client ) <EOL> self . login ( request , user ) <EOL> self . send_login_signal ( request , user , profile , client ) <EOL> self . delete_session_data ( request ) <EOL> return HttpResponseRedirect ( self . get_next ( request ) ) <EOL> def get ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if request . user . is_authenticated ( ) : <EOL> return HttpResponseRedirect ( self . get_next ( request ) ) <EOL> try : <EOL> user , profile , client = self . get_session_data ( request ) <EOL> except KeyError : <EOL> return self . error_to_response ( request , dict ( <EOL> error = _ ( "<STR_LIT>" ) ) ) <EOL> if GENERATE_USERNAME : <EOL> return self . generate_username_and_redirect ( request , user , profile , client ) <EOL> form = self . get_form ( ) ( initial = self . get_initial_data ( request , user , profile , client ) ) <EOL> additional_context = self . get_context ( request , user , profile , client ) <EOL> return self . render_to_response ( dict ( { '<STR_LIT>' : form } , ** additional_context ) ) <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if request . user . is_authenticated ( ) : <EOL> return self . error_to_response ( request , dict ( <EOL> error = _ ( "<STR_LIT>" ) ) ) <EOL> try : <EOL> user , profile , client = self . get_session_data ( request ) <EOL> except KeyError : <EOL> return self . error_to_response ( request , dict ( <EOL> error = _ ( "<STR_LIT>" ) ) ) <EOL> form = self . get_form ( ) ( request . POST , request . FILES , <EOL> initial = self . get_initial_data ( request , user , profile , client ) ) <EOL> if not form . is_valid ( ) : <EOL> additional_context = self . get_context ( request , user , profile , client ) <EOL> return self . render_to_response ( dict ( { '<STR_LIT>' : form } , ** additional_context ) ) <EOL> user , profile = form . save ( request , user , profile , client ) <EOL> user = profile . authenticate ( ) <EOL> self . send_connect_signal ( request , user , profile , client ) <EOL> self . login ( request , user ) <EOL> self . send_login_signal ( request , user , profile , client ) <EOL> self . delete_session_data ( request ) <EOL> return HttpResponseRedirect ( self . get_next ( request ) ) <EOL> class Logout ( View ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request ) : <EOL> logout ( request ) <EOL> url = getattr ( settings , '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> return HttpResponseRedirect ( url ) <EOL> class OAuthRedirect ( SocialRegistration , View ) : <EOL> """<STR_LIT>""" <EOL> client = None <EOL> template_name = None <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> request . session [ '<STR_LIT>' ] = self . get_next ( request ) <EOL> client = self . get_client ( ) ( ) <EOL> request . session [ self . get_client ( ) . get_session_key ( ) ] = client <EOL> url = client . get_redirect_url ( request = request ) <EOL> logger . debug ( "<STR_LIT>" , url ) <EOL> try : <EOL> return HttpResponseRedirect ( url ) <EOL> except OAuthError , error : <EOL> return self . error_to_response ( request , { '<STR_LIT:error>' : error } ) <EOL> except socket . timeout : <EOL> return self . error_to_response ( request , { '<STR_LIT:error>' : <EOL> _ ( '<STR_LIT>' ) } ) <EOL> class OAuthCallback ( SocialRegistration , View ) : <EOL> """<STR_LIT>""" <EOL> client = None <EOL> template_name = None <EOL> def get_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> client = request . session [ self . get_client ( ) . get_session_key ( ) ] <EOL> logger . debug ( "<STR_LIT>" , request . GET ) <EOL> client . complete ( dict ( request . GET . items ( ) ) ) <EOL> request . session [ self . get_client ( ) . get_session_key ( ) ] = client <EOL> return HttpResponseRedirect ( self . get_redirect ( ) ) <EOL> except KeyError : <EOL> return self . error_to_response ( request , { '<STR_LIT:error>' : "<STR_LIT>" } ) <EOL> except OAuthError , error : <EOL> return self . error_to_response ( request , { '<STR_LIT:error>' : error } ) <EOL> except socket . timeout : <EOL> return self . error_to_response ( request , { '<STR_LIT:error>' : <EOL> _ ( '<STR_LIT>' ) } ) <EOL> class SetupCallback ( SocialRegistration , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> client = request . session [ self . get_client ( ) . get_session_key ( ) ] <EOL> except KeyError : <EOL> return self . error_to_response ( request , { '<STR_LIT:error>' : "<STR_LIT>" } ) <EOL> lookup_kwargs = self . get_lookup_kwargs ( request , client ) <EOL> if request . user . is_authenticated ( ) : <EOL> try : <EOL> profile = self . get_profile ( ** lookup_kwargs ) <EOL> if not profile . user == request . user : <EOL> self . delete_session_data ( request ) <EOL> return self . error_to_response ( request , { <EOL> '<STR_LIT:error>' : _ ( '<STR_LIT>' ) <EOL> } ) <EOL> except self . get_model ( ) . DoesNotExist : <EOL> profile , created = self . get_or_create_profile ( request . user , <EOL> save = True , ** lookup_kwargs ) <EOL> self . send_connect_signal ( request , request . user , profile , client ) <EOL> return self . redirect ( request ) <EOL> user = self . authenticate ( ** lookup_kwargs ) <EOL> if user is None : <EOL> if not ALLOW_OPENID_SIGNUPS and self . client is OpenIDClient : <EOL> return self . error_to_response ( request , { <EOL> '<STR_LIT:error>' : _ ( '<STR_LIT>' ) <EOL> } ) <EOL> user = self . create_user ( ) <EOL> profile = self . create_profile ( user , ** lookup_kwargs ) <EOL> self . store_user ( request , user ) <EOL> self . store_profile ( request , profile ) <EOL> self . store_client ( request , client ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> if not user . is_active : <EOL> return self . inactive_response ( request ) <EOL> self . login ( request , user ) <EOL> profile = self . get_profile ( user = user , ** lookup_kwargs ) <EOL> self . send_login_signal ( request , user , profile , client ) <EOL> return self . redirect ( request ) </s>
