<s> from django . conf import settings <EOL> from django . contrib . auth import login as auth_login , REDIRECT_FIELD_NAME <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . contrib . auth . forms import AuthenticationForm <EOL> import re <EOL> from comrade . users import multipass <EOL> from comrade . views . simple import direct_to_template <EOL> import commonware . log <EOL> logger = commonware . log . getLogger ( __name__ ) <EOL> def _add_sso ( request , use_multipass , tender_url , redirect_to ) : <EOL> if use_multipass and redirect_to == tender_url : <EOL> redirect_to += '<STR_LIT>' + multipass . multipass ( request . user ) <EOL> return redirect_to <EOL> def login ( request , multipass = False , template_name = '<STR_LIT>' , <EOL> redirect_field_name = REDIRECT_FIELD_NAME , <EOL> authentication_form = AuthenticationForm ) : <EOL> """<STR_LIT>""" <EOL> redirect_to = request . REQUEST . get ( redirect_field_name , '<STR_LIT>' ) <EOL> tender_url = getattr ( settings , '<STR_LIT>' , None ) <EOL> if not redirect_to or '<STR_LIT:U+0020>' in redirect_to : <EOL> redirect_to = settings . LOGIN_REDIRECT_URL <EOL> elif '<STR_LIT>' in redirect_to and re . match ( r'<STR_LIT>' , redirect_to ) : <EOL> if redirect_to != tender_url : <EOL> redirect_to = settings . LOGIN_REDIRECT_URL <EOL> if request . user . is_authenticated ( ) : <EOL> redirect_to = _add_sso ( request , multipass , tender_url , redirect_to ) <EOL> return HttpResponseRedirect ( redirect_to ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = authentication_form ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> auth_login ( request , form . get_user ( ) ) <EOL> if request . session . test_cookie_worked ( ) : <EOL> request . session . delete_test_cookie ( ) <EOL> redirect_to = _add_sso ( request , multipass , tender_url , redirect_to ) <EOL> return HttpResponseRedirect ( redirect_to ) <EOL> else : <EOL> form = authentication_form ( request ) <EOL> if multipass : <EOL> login_url = reverse ( '<STR_LIT>' ) <EOL> else : <EOL> login_url = reverse ( '<STR_LIT>' ) <EOL> request . session . set_test_cookie ( ) <EOL> return direct_to_template ( request , template_name , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : redirect_field_name , <EOL> '<STR_LIT>' : redirect_to , <EOL> '<STR_LIT>' : login_url } ) </s>
