Commit d44360a3 authored by Claude Paroz's avatar Claude Paroz

Use class-based LoginView

parent 7582b83f
......@@ -44,7 +44,7 @@ class CommonTest(TestCase):
self.r2.save()
def test_house_keeping(self):
response = self.client.get('/register/activate/a_activation_key')
response = self.client.get('/register/activate/a_activation_key', follow=True)
self.assertContains(response, 'Your account has been activated.')
call_command('run-maintenance')
......
......@@ -3,6 +3,7 @@ from threading import Thread
from django.conf import settings
from django.contrib.auth import login
from django.contrib.auth.views import LoginView as AuthLoginView
from django.contrib import messages
from django.core.management import call_command
from django.http import HttpResponse, HttpResponseForbidden, HttpResponseRedirect, Http404
......@@ -43,44 +44,18 @@ def about(request):
return render(request, 'about.html', context)
def site_login(request):
""" Site-specific login page. Not named 'login' to not confuse with auth.login """
def redirect(referer):
if is_safe_url(referer, allowed_hosts={request.get_host()}):
return HttpResponseRedirect(referer)
else:
return HttpResponseRedirect(reverse("home"))
class LoginView(AuthLoginView):
form_class = LoginForm
referer = request.META.get('HTTP_REFERER', None)
openid_path = ''
login_form = LoginForm(request, data=request.POST)
if request.method == 'POST':
if request.POST.get('referer', None):
referer = request.POST['referer']
if login_form.is_valid():
user = login_form.get_user()
login(request, user)
if Role.objects.filter(person__username=user.username).count() < 1:
message = _("You have not joined any translation team yet. "
"You can do it from <a href=\"%(url)s\">your profile</a>.") % {
'url': reverse('person_team_join'),
}
messages.info(request, message)
return redirect(referer)
else:
messages.error(request, _("Login unsuccessful. Please verify your username and password."))
if 'django_openid_auth' in settings.INSTALLED_APPS:
openid_path = '/openid/login/'
context = {
'pageSection': 'home',
'openid_path': openid_path,
'referer': referer,
'next': referer,
'form': login_form,
}
return render(request, 'login.html', context)
def form_valid(self, form):
response = super().form_valid(form)
if Role.objects.filter(person__username=self.request.user.username).count() < 1:
message = _("You have not joined any translation team yet. "
"You can do it from <a href=\"%(url)s\">your profile</a>.") % {
'url': reverse('person_team_join'),
}
messages.info(self.request, message)
return response
def site_register(request):
......@@ -101,6 +76,7 @@ def site_register(request):
}
return render(request, 'registration/register.html', context)
def activate_account(request, key):
""" Activate an account through the link a requestor has received by email """
try:
......@@ -109,7 +85,8 @@ def activate_account(request, key):
return render(request, 'error.html', {'error': _("Sorry, the key you provided is not valid.")})
person.activate()
messages.success(request, _("Your account has been activated."))
return site_login(request)
return HttpResponseRedirect(reverse('login'))
def help(request, topic, modal):
template = 'help/%s.html' % topic
......
......@@ -18,7 +18,7 @@ urlpatterns = [
common_views.about,
name='about'),
url(r'^login/$',
common_views.site_login,
common_views.LoginView.as_view(template_name='login.html'),
name='login'),
url(r'^logout/$',
require_POST(auth_views.LogoutView.as_view()),
......
......@@ -50,7 +50,7 @@ class PeopleTestCase(TestCase):
response = self.client.get('/register/activate/a_invalid_key')
self.assertContains(response, 'Sorry, the key you provided is not valid.')
response = self.client.get('/register/activate/%s' % self.newu.activation_key)
response = self.client.get('/register/activate/%s' % self.newu.activation_key, follow=True)
self.assertContains(response, 'Your account has been activated.')
self.newu = Person.objects.get(username='newuser')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment