<s> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . contrib . auth import login as standard_login <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import redirect <EOL> from django . views . decorators . cache import never_cache <EOL> from django . views . generic import simple <EOL> from accounts . backends import get_backend <EOL> from accounts . forms import ExtendedAuthenticationForm <EOL> def prepare ( request , backend , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> backend = get_backend ( backend ) <EOL> return redirect ( backend . prepare ( request ) ) <EOL> def authenticate ( request , backend , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> backend = get_backend ( backend ) <EOL> user = backend . authenticate ( request ) <EOL> if user is not None : <EOL> return backend . grant_user ( request , user ) <EOL> elif request . user . is_authenticated ( ) : <EOL> return backend . link_user ( request , user ) <EOL> else : <EOL> return backend . create_user ( request , user ) <EOL> @ login_required <EOL> def deauthenticate ( request , backend , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> backend = get_backend ( backend ) <EOL> success = backend . deauthenticate ( request ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> @ never_cache <EOL> def login ( request , template_name = '<STR_LIT>' , <EOL> redirect_field_name = REDIRECT_FIELD_NAME , <EOL> authentication_form = ExtendedAuthenticationForm ) : <EOL> """<STR_LIT>""" <EOL> redirect_to = request . REQUEST . get ( redirect_field_name , '<STR_LIT>' ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = authentication_form ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> if not redirect_to or '<STR_LIT>' in redirect_to or '<STR_LIT:U+0020>' in redirect_to : <EOL> redirect_to = settings . LOGIN_REDIRECT_URL <EOL> standard_login ( request , form . get_user ( ) ) <EOL> messages . success ( request , '<STR_LIT>' ) <EOL> if request . session . test_cookie_worked ( ) : <EOL> request . session . delete_test_cookie ( ) <EOL> if form . cleaned_data [ '<STR_LIT>' ] : <EOL> request . session [ settings . PERSISTENT_SESSION_KEY ] = True <EOL> else : <EOL> request . session . set_expiry ( <NUM_LIT:0> ) <EOL> return redirect ( redirect_to ) <EOL> else : <EOL> form = authentication_form ( request ) <EOL> request . session . set_test_cookie ( ) <EOL> return simple . direct_to_template ( request , <EOL> template = template_name , <EOL> extra_context = { <EOL> '<STR_LIT>' : form , <EOL> redirect_field_name : redirect_to , <EOL> } <EOL> ) <EOL> def logout_then_login ( request , login_url = None ) : <EOL> """<STR_LIT>""" <EOL> from django . contrib . auth import logout_then_login <EOL> if not login_url : <EOL> login_url = settings . LOGIN_URL <EOL> request . session . clear ( ) <EOL> messages . success ( request , '<STR_LIT>' ) <EOL> return logout_then_login ( request , login_url ) </s>
