<s> import urlparse <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , QueryDict <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . http import base36_to_int , is_safe_url <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . debug import sensitive_post_parameters <EOL> from django . views . decorators . cache import never_cache <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME , login as auth_login , logout as auth_logout <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth . forms import AuthenticationForm , PasswordResetForm , SetPasswordForm , PasswordChangeForm <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . auth . tokens import default_token_generator <EOL> from django . contrib . sites . models import get_current_site <EOL> @ sensitive_post_parameters ( ) <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> current_app = None , extra_context = None ) : <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 is_safe_url ( url = redirect_to , host = request . get_host ( ) ) : <EOL> redirect_to = settings . LOGIN_REDIRECT_URL <EOL> auth_login ( request , form . get_user ( ) ) <EOL> if request . session . test_cookie_worked ( ) : <EOL> request . session . delete_test_cookie ( ) <EOL> return HttpResponseRedirect ( redirect_to ) <EOL> else : <EOL> form = authentication_form ( request ) <EOL> request . session . set_test_cookie ( ) <EOL> current_site = get_current_site ( request ) <EOL> context = { <EOL> '<STR_LIT>' : form , <EOL> redirect_field_name : redirect_to , <EOL> '<STR_LIT>' : current_site , <EOL> '<STR_LIT>' : current_site . name , <EOL> } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context , <EOL> current_app = current_app ) <EOL> def logout ( request , next_page = None , <EOL> template_name = '<STR_LIT>' , <EOL> redirect_field_name = REDIRECT_FIELD_NAME , <EOL> current_app = None , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> auth_logout ( request ) <EOL> if redirect_field_name in request . REQUEST : <EOL> next_page = request . REQUEST [ redirect_field_name ] <EOL> if not is_safe_url ( url = next_page , host = request . get_host ( ) ) : <EOL> next_page = request . path <EOL> if next_page : <EOL> return HttpResponseRedirect ( next_page ) <EOL> current_site = get_current_site ( request ) <EOL> context = { <EOL> '<STR_LIT>' : current_site , <EOL> '<STR_LIT>' : current_site . name , <EOL> '<STR_LIT:title>' : _ ( '<STR_LIT>' ) <EOL> } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context , <EOL> current_app = current_app ) <EOL> def logout_then_login ( request , login_url = None , current_app = None , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> if not login_url : <EOL> login_url = settings . LOGIN_URL <EOL> return logout ( request , login_url , current_app = current_app , extra_context = extra_context ) <EOL> def redirect_to_login ( next , login_url = None , <EOL> redirect_field_name = REDIRECT_FIELD_NAME ) : <EOL> """<STR_LIT>""" <EOL> if not login_url : <EOL> login_url = settings . LOGIN_URL <EOL> login_url_parts = list ( urlparse . urlparse ( login_url ) ) <EOL> if redirect_field_name : <EOL> querystring = QueryDict ( login_url_parts [ <NUM_LIT:4> ] , mutable = True ) <EOL> querystring [ redirect_field_name ] = next <EOL> login_url_parts [ <NUM_LIT:4> ] = querystring . urlencode ( safe = '<STR_LIT:/>' ) <EOL> return HttpResponseRedirect ( urlparse . urlunparse ( login_url_parts ) ) <EOL> @ csrf_protect <EOL> def password_reset ( request , is_admin_site = False , <EOL> template_name = '<STR_LIT>' , <EOL> email_template_name = '<STR_LIT>' , <EOL> subject_template_name = '<STR_LIT>' , <EOL> password_reset_form = PasswordResetForm , <EOL> token_generator = default_token_generator , <EOL> post_reset_redirect = None , <EOL> from_email = None , <EOL> current_app = None , <EOL> extra_context = None ) : <EOL> if post_reset_redirect is None : <EOL> post_reset_redirect = reverse ( '<STR_LIT>' ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = password_reset_form ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> opts = { <EOL> '<STR_LIT>' : request . is_secure ( ) , <EOL> '<STR_LIT>' : token_generator , <EOL> '<STR_LIT>' : from_email , <EOL> '<STR_LIT>' : email_template_name , <EOL> '<STR_LIT>' : subject_template_name , <EOL> '<STR_LIT>' : request , <EOL> } <EOL> if is_admin_site : <EOL> opts = dict ( opts , domain_override = request . get_host ( ) ) <EOL> form . save ( ** opts ) <EOL> return HttpResponseRedirect ( post_reset_redirect ) <EOL> else : <EOL> form = password_reset_form ( ) <EOL> context = { <EOL> '<STR_LIT>' : form , <EOL> } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context , <EOL> current_app = current_app ) <EOL> def password_reset_done ( request , <EOL> template_name = '<STR_LIT>' , <EOL> current_app = None , extra_context = None ) : <EOL> context = { } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context , <EOL> current_app = current_app ) <EOL> @ sensitive_post_parameters ( ) <EOL> @ never_cache <EOL> def password_reset_confirm ( request , uidb36 = None , token = None , <EOL> template_name = '<STR_LIT>' , <EOL> token_generator = default_token_generator , <EOL> set_password_form = SetPasswordForm , <EOL> post_reset_redirect = None , <EOL> current_app = None , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> assert uidb36 is not None and token is not None <EOL> if post_reset_redirect is None : <EOL> post_reset_redirect = reverse ( '<STR_LIT>' ) <EOL> try : <EOL> uid_int = base36_to_int ( uidb36 ) <EOL> user = User . objects . get ( id = uid_int ) <EOL> except ( ValueError , User . DoesNotExist ) : <EOL> user = None <EOL> if user is not None and token_generator . check_token ( user , token ) : <EOL> validlink = True <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = set_password_form ( user , request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return HttpResponseRedirect ( post_reset_redirect ) <EOL> else : <EOL> form = set_password_form ( None ) <EOL> else : <EOL> validlink = False <EOL> form = None <EOL> context = { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : validlink , <EOL> } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context , <EOL> current_app = current_app ) <EOL> def password_reset_complete ( request , <EOL> template_name = '<STR_LIT>' , <EOL> current_app = None , extra_context = None ) : <EOL> context = { <EOL> '<STR_LIT>' : settings . LOGIN_URL <EOL> } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context , <EOL> current_app = current_app ) <EOL> @ sensitive_post_parameters ( ) <EOL> @ csrf_protect <EOL> @ login_required <EOL> def password_change ( request , <EOL> template_name = '<STR_LIT>' , <EOL> post_change_redirect = None , <EOL> password_change_form = PasswordChangeForm , <EOL> current_app = None , extra_context = None ) : <EOL> if post_change_redirect is None : <EOL> post_change_redirect = reverse ( '<STR_LIT>' ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = password_change_form ( user = request . user , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return HttpResponseRedirect ( post_change_redirect ) <EOL> else : <EOL> form = password_change_form ( user = request . user ) <EOL> context = { <EOL> '<STR_LIT>' : form , <EOL> } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context , <EOL> current_app = current_app ) <EOL> @ login_required <EOL> def password_change_done ( request , <EOL> template_name = '<STR_LIT>' , <EOL> current_app = None , extra_context = None ) : <EOL> context = { } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context , <EOL> current_app = current_app ) </s>
