From 1e65212d2222dd4a86ac6c7f6f2ac4caf47a38bf Mon Sep 17 00:00:00 2001 From: Guillim Date: Mon, 17 Jun 2019 18:27:08 +0200 Subject: [PATCH] We create the Signup view, one of the most important. It is important to keep in mind the difference of action between the GET and the POST. --- app/authentification/forms.py | 10 ++++++++ app/authentification/tokens.py | 9 ++++++++ app/authentification/views.py | 42 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 app/authentification/forms.py create mode 100644 app/authentification/tokens.py create mode 100644 app/authentification/views.py diff --git a/app/authentification/forms.py b/app/authentification/forms.py new file mode 100644 index 00000000..4baa354a --- /dev/null +++ b/app/authentification/forms.py @@ -0,0 +1,10 @@ +from django import forms +from django.contrib.auth.forms import UserCreationForm +from django.contrib.auth.models import User + +class SignupForm(UserCreationForm): + email = forms.EmailField(max_length=200, help_text='Required') + + class Meta: + model = User + fields = ('username', 'email', 'password1', 'password2') diff --git a/app/authentification/tokens.py b/app/authentification/tokens.py new file mode 100644 index 00000000..ad5ab594 --- /dev/null +++ b/app/authentification/tokens.py @@ -0,0 +1,9 @@ +from django.contrib.auth.tokens import PasswordResetTokenGenerator +from django.utils 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() diff --git a/app/authentification/views.py b/app/authentification/views.py new file mode 100644 index 00000000..49564d9b --- /dev/null +++ b/app/authentification/views.py @@ -0,0 +1,42 @@ +from django.shortcuts import render +from .forms import SignupForm +from django.contrib.sites.shortcuts import get_current_site +from django.utils.encoding import force_bytes +from django.utils.http import urlsafe_base64_encode +from django.template.loader import render_to_string +from .tokens import account_activation_token +from django.core.mail import EmailMessage +from django.views.generic import TemplateView + +class SignupView(TemplateView): + template_name = 'signup.html' + form_class = SignupForm + + def get(self, request, *args, **kwargs): + form = self.form_class() + return render(request, self.template_name, {'form': form}) + + def post(self, request, *args, **kwargs): + form = self.form_class(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 blog account.' + + message = render_to_string('acc_active_email.html', { + 'user': user, + 'domain': current_site.domain, + 'uid':urlsafe_base64_encode(force_bytes(user.pk)).decode(), + '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, 'validate_mail_address_complete.html') + else: + return render(request, self.template_name, {'form': form})