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.

53 lines
2.1 KiB

5 years ago
5 years ago
  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 django.shortcuts import redirect
  11. from django.conf import settings
  12. class SignupView(TemplateView):
  13. template_name = 'signup.html'
  14. form_class = SignupForm
  15. def get(self, request, *args, **kwargs):
  16. form = self.form_class()
  17. return render(request, self.template_name, {'form': form, 'allow_signup': bool(settings.ALLOW_SIGNUP)})
  18. def post(self, request, *args, **kwargs):
  19. form = self.form_class(request.POST)
  20. # here we make sure that a post request won't trigger a subscription in case allow_signup is False
  21. if not bool(settings.ALLOW_SIGNUP):
  22. return redirect('signup')
  23. if not hasattr(settings, "EMAIL_BACKEND") and not hasattr(settings, "EMAIL_HOST"):
  24. return render(request, 'email_not_set.html')
  25. if form.is_valid():
  26. user = form.save(commit=False)
  27. user.is_active = False
  28. user.save()
  29. current_site = get_current_site(request)
  30. mail_subject = 'Activate your account.'
  31. message = render_to_string('acc_active_email.html', {
  32. 'user': user,
  33. 'domain': current_site.domain,
  34. 'scheme': request.scheme,
  35. 'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
  36. 'token': account_activation_token.make_token(user),
  37. })
  38. to_email = form.cleaned_data.get('email')
  39. email = EmailMessage(
  40. mail_subject, message, to=[to_email]
  41. )
  42. email.send()
  43. return render(request, 'validate_mail_address_complete.html')
  44. else:
  45. return render(request, self.template_name, {'form': form, 'allow_signup': bool(settings.ALLOW_SIGNUP)})