Enable email confirmation

master
Tait Hoyem 4 years ago
parent b5416d1ad3
commit b112d87fe1

@ -2,7 +2,7 @@ from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import LameUser
class UserSignUpForm(UserCreationForm):
class LameUserSignupForm(UserCreationForm):
email = forms.EmailField(max_length=100, help_text='Required')
class Meta:
model = LameUser

@ -0,0 +1,4 @@
{% extends 'common/master.html' %}
{% block body %}
<p>Thank you for your email confirmation. Now you can login your account.</p>
{% endblock %}

@ -0,0 +1,6 @@
{% autoescape on %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
https://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}

@ -0,0 +1,4 @@
{% extends 'common/master.html' %}
{% block body %}
<p>Please confirm your email address to complete the registration.</p>
{% endblock %}

@ -0,0 +1,4 @@
{% extends 'common/master.html' %}
{% block body %}
<p>Activation link invalid.</p>
{% endblock %}

@ -0,0 +1,10 @@
from django.contrib.auth.tokens import PasswordResetTokenGenerator
import six
class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) +
six.text_type(user.is_active)
)
account_activation_token = TokenGenerator()

@ -1,9 +1,11 @@
from django.urls import path, include
from django.conf.urls import url
from . import views
urlpatterns = [
path('', views.index, name="index"),
path('signup/', views.SignUp.as_view(), name="signup"),
path('signup/', views.signup, name="signup"),
path('', include('django.contrib.auth.urls')),
path('activate/<slug:uidb64>/<slug:token>/', views.activate, name='activate')
]

@ -1,17 +1,61 @@
from django.shortcuts import (
render, HttpResponse
render, HttpResponse, redirect
)
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login, authenticate
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from .tokens import account_activation_token
from .forms import LameUserSignupForm
from .models import LameUser
from . import forms
# Create your views here.
def index(request):
return render(request, 'index.html')
class SignUp(generic.CreateView):
form_class = forms.UserSignUpForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
def signup(request):
if request.method == 'POST':
form = LameUserSignupForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
mail_subject = 'Activate your lamegames account.'
message = render_to_string('registration/activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return render(request, 'registration/confirm_your_email.html', {})
else:
form = LameUserSignupForm()
return render(request, 'registration/signup.html', {'form': form})
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = LameUser.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, LameUser.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
login(request, user)
# return redirect('home')
return render(request, 'registration/account_activated.html')
else:
return render(request, 'registration/invalid_link.html')

Loading…
Cancel
Save