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.

52 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
5 years ago
  1. from django.conf import settings
  2. from django.contrib.sites.shortcuts import get_current_site
  3. from django.core.mail import EmailMessage
  4. from django.shortcuts import redirect, render
  5. from django.template.loader import render_to_string
  6. from django.utils.encoding import force_bytes
  7. from django.utils.http import urlsafe_base64_encode
  8. from django.views.generic import TemplateView
  9. from .forms import SignupForm
  10. from .tokens import account_activation_token
  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 not hasattr(settings, "EMAIL_BACKEND") and not hasattr(settings, "EMAIL_HOST"):
  23. return render(request, 'email_not_set.html')
  24. if form.is_valid():
  25. user = form.save(commit=False)
  26. user.is_active = False
  27. user.save()
  28. current_site = get_current_site(request)
  29. mail_subject = 'Activate your account.'
  30. message = render_to_string('acc_active_email.html', {
  31. 'user': user,
  32. 'domain': current_site.domain,
  33. 'scheme': request.scheme,
  34. 'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
  35. 'token': account_activation_token.make_token(user),
  36. })
  37. to_email = form.cleaned_data.get('email')
  38. email = EmailMessage(
  39. mail_subject, message, to=[to_email]
  40. )
  41. email.send()
  42. return render(request, 'validate_mail_address_complete.html')
  43. else:
  44. return render(request, self.template_name, {'form': form, 'allow_signup': bool(settings.ALLOW_SIGNUP)})