You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.9 KiB

  1. from django.shortcuts import render
  2. from .forms import SignupForm
  3. from django.contrib.sites.shortcuts import get_current_site
  4. from django.utils.encoding import force_bytes
  5. from django.utils.http import urlsafe_base64_encode
  6. from django.template.loader import render_to_string
  7. from .tokens import account_activation_token
  8. from django.core.mail import EmailMessage
  9. from django.views.generic import TemplateView
  10. from app import settings
  11. class SignupView(TemplateView):
  12. template_name = 'signup.html'
  13. form_class = SignupForm
  14. def get(self, request, *args, **kwargs):
  15. form = self.form_class()
  16. return render(request, self.template_name, {'form': form, 'allow_signup': bool(settings.ALLOW_SIGNUP)})
  17. def post(self, request, *args, **kwargs):
  18. form = self.form_class(request.POST)
  19. # here we make sure that a post request won't trigger a subscription in case allow_signup is False
  20. if not bool(settings.ALLOW_SIGNUP):
  21. return redirect('signup')
  22. if form.is_valid():
  23. user = form.save(commit=False)
  24. user.is_active = False
  25. user.save()
  26. current_site = get_current_site(request)
  27. mail_subject = 'Activate your blog account.'
  28. message = render_to_string('acc_active_email.html', {
  29. 'user': user,
  30. 'domain': current_site.domain,
  31. 'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
  32. 'token': account_activation_token.make_token(user),
  33. })
  34. to_email = form.cleaned_data.get('email')
  35. email = EmailMessage(
  36. mail_subject, message, to=[to_email]
  37. )
  38. email.send()
  39. return render(request, 'validate_mail_address_complete.html')
  40. else:
  41. return render(request, self.template_name, {'form': form, 'allow_signup': bool(settings.ALLOW_SIGNUP)})