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.

42 lines
1.6 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. class SignupView(TemplateView):
  11. template_name = 'signup.html'
  12. form_class = SignupForm
  13. def get(self, request, *args, **kwargs):
  14. form = self.form_class()
  15. return render(request, self.template_name, {'form': form})
  16. def post(self, request, *args, **kwargs):
  17. form = self.form_class(request.POST)
  18. if form.is_valid():
  19. user = form.save(commit=False)
  20. user.is_active = False
  21. user.save()
  22. current_site = get_current_site(request)
  23. mail_subject = 'Activate your blog account.'
  24. message = render_to_string('acc_active_email.html', {
  25. 'user': user,
  26. 'domain': current_site.domain,
  27. 'uid':urlsafe_base64_encode(force_bytes(user.pk)).decode(),
  28. 'token':account_activation_token.make_token(user),
  29. })
  30. to_email = form.cleaned_data.get('email')
  31. email = EmailMessage(
  32. mail_subject, message, to=[to_email]
  33. )
  34. email.send()
  35. return render(request, 'validate_mail_address_complete.html')
  36. else:
  37. return render(request, self.template_name, {'form': form})