Commit 6947da20 authored by Chris Lamb's avatar Chris Lamb
Browse files

Add email validation to registration process. (Closes: #18)

parent 22bebb60
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from email_from_template import send_mail
from freenodejobs.utils.tokens import get_token
UserModel = get_user_model()
......@@ -10,3 +14,12 @@ class RegistrationForm(UserCreationForm):
fields = (
'email',
)
def save(self):
user = super().save()
send_mail((user.email,), 'registration/validate.email', {
'token': get_token(user),
}, fail_silently=True)
return user
......@@ -2,6 +2,7 @@ from django.conf import settings
from django.contrib.auth import get_user_model
from freenodejobs.utils.test import TestCase
from freenodejobs.utils.tokens import get_token
UserModel = get_user_model()
......@@ -40,3 +41,31 @@ class RegistrationTests(TestCase):
self.assertFormError(response, 'form', 'email', [
"User with this Email already exists.",
])
def test_validate(self):
user = self.assertRegister()
self.assertGET(
200,
'registration:validate',
get_token(user),
login=None,
follow=True,
)
user.refresh_from_db()
self.assertTrue(user.email_validated)
def test_validate_invalid_token(self):
user = self.assertRegister()
self.assertGET(
200,
'registration:validate',
'invalid-token',
login=None,
follow=True,
)
user.refresh_from_db()
self.assertFalse(user.email_validated)
......@@ -7,4 +7,8 @@ app_name = 'registration'
urlpatterns = (
path('register', views.view,
name='view'),
path('register/success', views.success,
name='success'),
path('register/validate/<token>', views.validate,
name='validate'),
)
from django.conf import settings
from django.utils import timezone
from django.contrib import messages
from django.shortcuts import render, redirect
from django.contrib.auth import logout
from freenodejobs.utils.user import login
from freenodejobs.utils.tokens import get_user_from_token
from freenodejobs.utils.decorators import logout_required
from .forms import RegistrationForm
......@@ -14,11 +17,9 @@ def view(request):
form = RegistrationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
messages.success(request, "Registered.")
form.save()
return redirect(settings.LOGIN_REDIRECT_URL)
return redirect('registration:success')
else:
form = RegistrationForm()
......@@ -26,3 +27,29 @@ def view(request):
return render(request, 'registration/view.html', {
'form': form,
})
@logout_required
def success(request):
return render(request, 'registration/success.html')
def validate(request, token):
# Always log the user out, at least to avoid confusion
logout(request)
user = get_user_from_token(token)
if user is None or user.email_validated:
messages.error(request,
"The link you followed is invalid or has expired.")
return redirect(settings.LOGOUT_REDIRECT_URL)
user.email_validated = timezone.now()
user.save()
login(request, user)
messages.success(request, "Email successfully validated.")
return redirect(settings.LOGIN_REDIRECT_URL)
{% extends "base/base.html" %}
{% block title %}Registration successful &mdash; {{ block.super }}{% endblock %}
{% block subtitle %}Registration successful{% endblock %}
{% block container %}
<h2>
Registration successful
</h2>
<p>
Thanks for signing up on Freenode Jobs.
</p>
<p>
We have just sent you a confirmation email with a URL to activate your
account &mdash; please check your email and click the link.
</p>
{% endblock %}
{% extends email_from_template %}
{% block subject %}
Freenode Jobs account validation
{% endblock %}
{% block body %}
Hi,
Thanks for signing up on Freenode Jobs. To validate your email
address, please click the following link:
{{ settings.SITE_URL }}{% url "registration:validate" token %}
If you did not signup for Freenode Jobs, please ignore this email.
Regards,
--
Freenode Jobs
{% endblock %}
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