2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.auth.forms import AuthenticationForm
|
|
|
|
from django.contrib.auth.forms import PasswordResetForm, PasswordChangeForm
|
2006-12-16 02:00:50 +08:00
|
|
|
from django import oldforms
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.contrib.sites.models import Site
|
2006-07-22 01:11:13 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2007-04-25 16:49:57 +08:00
|
|
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-06-06 13:16:05 +08:00
|
|
|
def login(request, template_name='registration/login.html'):
|
2006-05-02 09:31:56 +08:00
|
|
|
"Displays the login form and handles the login action."
|
|
|
|
manipulator = AuthenticationForm(request)
|
|
|
|
redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '')
|
|
|
|
if request.POST:
|
|
|
|
errors = manipulator.get_validation_errors(request.POST)
|
|
|
|
if not errors:
|
|
|
|
# Light security check -- make sure redirect_to isn't garbage.
|
|
|
|
if not redirect_to or '://' in redirect_to or ' ' in redirect_to:
|
2007-04-25 16:49:57 +08:00
|
|
|
from django.conf import settings
|
|
|
|
redirect_to = settings.LOGIN_REDIRECT_URL
|
2006-06-29 00:37:02 +08:00
|
|
|
from django.contrib.auth import login
|
|
|
|
login(request, manipulator.get_user())
|
2006-05-02 09:31:56 +08:00
|
|
|
request.session.delete_test_cookie()
|
|
|
|
return HttpResponseRedirect(redirect_to)
|
|
|
|
else:
|
|
|
|
errors = {}
|
|
|
|
request.session.set_test_cookie()
|
2006-06-06 13:16:05 +08:00
|
|
|
return render_to_response(template_name, {
|
2006-12-16 02:00:50 +08:00
|
|
|
'form': oldforms.FormWrapper(manipulator, request.POST, errors),
|
2006-05-02 09:31:56 +08:00
|
|
|
REDIRECT_FIELD_NAME: redirect_to,
|
|
|
|
'site_name': Site.objects.get_current().name,
|
|
|
|
}, context_instance=RequestContext(request))
|
|
|
|
|
2006-06-06 13:16:05 +08:00
|
|
|
def logout(request, next_page=None, template_name='registration/logged_out.html'):
|
2006-05-02 09:31:56 +08:00
|
|
|
"Logs out the user and displays 'You are logged out' message."
|
2006-06-29 00:37:02 +08:00
|
|
|
from django.contrib.auth import logout
|
2006-07-24 07:14:36 +08:00
|
|
|
logout(request)
|
|
|
|
if next_page is None:
|
2006-06-08 13:04:10 +08:00
|
|
|
return render_to_response(template_name, {'title': _('Logged out')}, context_instance=RequestContext(request))
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
|
|
|
# Redirect to this page until the session has been cleared.
|
|
|
|
return HttpResponseRedirect(next_page or request.path)
|
|
|
|
|
2007-04-25 16:49:57 +08:00
|
|
|
def logout_then_login(request, login_url=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"Logs out the user if he is logged in. Then redirects to the log-in page."
|
2007-04-25 16:49:57 +08:00
|
|
|
if not login_url:
|
|
|
|
from django.conf import settings
|
|
|
|
login_url = settings.LOGIN_URL
|
2006-05-02 09:31:56 +08:00
|
|
|
return logout(request, login_url)
|
|
|
|
|
2007-04-25 16:49:57 +08:00
|
|
|
def redirect_to_login(next, login_url=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
"Redirects the user to the login page, passing the given 'next' page"
|
2007-04-25 16:49:57 +08:00
|
|
|
if not login_url:
|
|
|
|
from django.conf import settings
|
|
|
|
login_url = settings.LOGIN_URL
|
2006-05-02 09:31:56 +08:00
|
|
|
return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, next))
|
|
|
|
|
2006-07-28 01:48:35 +08:00
|
|
|
def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html',
|
|
|
|
email_template_name='registration/password_reset_email.html'):
|
2006-05-02 09:31:56 +08:00
|
|
|
new_data, errors = {}, {}
|
|
|
|
form = PasswordResetForm()
|
|
|
|
if request.POST:
|
|
|
|
new_data = request.POST.copy()
|
|
|
|
errors = form.get_validation_errors(new_data)
|
|
|
|
if not errors:
|
|
|
|
if is_admin_site:
|
2006-07-28 01:48:35 +08:00
|
|
|
form.save(domain_override=request.META['HTTP_HOST'])
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
2006-07-28 01:48:35 +08:00
|
|
|
form.save(email_template_name=email_template_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
return HttpResponseRedirect('%sdone/' % request.path)
|
2006-12-16 02:00:50 +08:00
|
|
|
return render_to_response(template_name, {'form': oldforms.FormWrapper(form, new_data, errors)},
|
2006-05-02 09:31:56 +08:00
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
2006-06-06 13:16:05 +08:00
|
|
|
def password_reset_done(request, template_name='registration/password_reset_done.html'):
|
|
|
|
return render_to_response(template_name, context_instance=RequestContext(request))
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-06-06 13:16:05 +08:00
|
|
|
def password_change(request, template_name='registration/password_change_form.html'):
|
2006-05-02 09:31:56 +08:00
|
|
|
new_data, errors = {}, {}
|
|
|
|
form = PasswordChangeForm(request.user)
|
|
|
|
if request.POST:
|
|
|
|
new_data = request.POST.copy()
|
|
|
|
errors = form.get_validation_errors(new_data)
|
|
|
|
if not errors:
|
|
|
|
form.save(new_data)
|
|
|
|
return HttpResponseRedirect('%sdone/' % request.path)
|
2006-12-16 02:00:50 +08:00
|
|
|
return render_to_response(template_name, {'form': oldforms.FormWrapper(form, new_data, errors)},
|
2006-05-02 09:31:56 +08:00
|
|
|
context_instance=RequestContext(request))
|
|
|
|
password_change = login_required(password_change)
|
|
|
|
|
2006-06-06 13:16:05 +08:00
|
|
|
def password_change_done(request, template_name='registration/password_change_done.html'):
|
|
|
|
return render_to_response(template_name, context_instance=RequestContext(request))
|