<s> from django . views . generic import TemplateView <EOL> import re <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . contrib . auth import login as auth_login <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from django . contrib . auth . forms import AuthenticationForm <EOL> from django . contrib . sites . models import Site <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from django . views . decorators . cache import never_cache <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from tos . compat import get_runtime_user_model , get_request_site <EOL> from tos . models import has_user_agreed_latest_tos , TermsOfService , UserAgreement <EOL> class TosView ( TemplateView ) : <EOL> template_name = "<STR_LIT>" <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( TosView , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = TermsOfService . objects . get_current_tos ( ) <EOL> return context <EOL> def _redirect_to ( redirect_to ) : <EOL> """<STR_LIT>""" <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> redirect_to = settings . LOGIN_REDIRECT_URL <EOL> return redirect_to <EOL> @ csrf_protect <EOL> @ never_cache <EOL> def check_tos ( request , template_name = '<STR_LIT>' , <EOL> redirect_field_name = REDIRECT_FIELD_NAME , ) : <EOL> redirect_to = _redirect_to ( request . POST . get ( redirect_field_name , request . GET . get ( redirect_field_name , '<STR_LIT>' ) ) ) <EOL> tos = TermsOfService . objects . get_current_tos ( ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> if request . POST . get ( "<STR_LIT>" , "<STR_LIT>" ) == "<STR_LIT>" : <EOL> user = get_runtime_user_model ( ) . objects . get ( pk = request . session [ '<STR_LIT>' ] ) <EOL> user . backend = request . session [ '<STR_LIT>' ] <EOL> UserAgreement . objects . create ( terms_of_service = tos , user = user ) <EOL> auth_login ( request , user ) <EOL> if request . session . test_cookie_worked ( ) : <EOL> request . session . delete_test_cookie ( ) <EOL> return HttpResponseRedirect ( redirect_to ) <EOL> else : <EOL> messages . error ( <EOL> request , <EOL> _ ( u"<STR_LIT>" ) <EOL> ) <EOL> return render_to_response ( template_name , { <EOL> '<STR_LIT>' : tos , <EOL> redirect_field_name : redirect_to , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ csrf_protect <EOL> @ never_cache <EOL> def login ( request , template_name = '<STR_LIT>' , <EOL> redirect_field_name = REDIRECT_FIELD_NAME , <EOL> authentication_form = AuthenticationForm ) : <EOL> """<STR_LIT>""" <EOL> redirect_to = request . POST . get ( redirect_field_name , request . GET . 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> redirect_to = _redirect_to ( redirect_to ) <EOL> user = form . get_user ( ) <EOL> if has_user_agreed_latest_tos ( user ) : <EOL> auth_login ( request , user ) <EOL> if request . session . test_cookie_worked ( ) : <EOL> request . session . delete_test_cookie ( ) <EOL> return HttpResponseRedirect ( redirect_to ) <EOL> else : <EOL> request . session [ '<STR_LIT>' ] = user . pk <EOL> request . session [ '<STR_LIT>' ] = user . backend <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> redirect_field_name : redirect_to , <EOL> '<STR_LIT>' : TermsOfService . objects . get_current_tos ( ) <EOL> } , context_instance = RequestContext ( request ) ) <EOL> else : <EOL> form = authentication_form ( request ) <EOL> request . session . set_test_cookie ( ) <EOL> if Site . _meta . installed : <EOL> current_site = Site . objects . get_current ( ) <EOL> else : <EOL> current_site = get_request_site ( ) ( request ) <EOL> return render_to_response ( template_name , { <EOL> '<STR_LIT>' : form , <EOL> redirect_field_name : redirect_to , <EOL> '<STR_LIT>' : current_site , <EOL> '<STR_LIT>' : current_site . name , <EOL> } , context_instance = RequestContext ( request ) ) </s>
