Guest User

Untitled

a guest
Jun 2nd, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. def register(request):
  2. """1. register a new user
  3. 2. generate activating code
  4. 3. send validating email
  5. 4. prompt to check activating email.
  6. """
  7. if request.method == "GET":
  8. form = UserForm()
  9. if request.method == "POST":
  10. form = UserForm(request.POST)
  11. print(vars(form))
  12. if form.is_valid():
  13. #1. Create User and save to sever
  14. user = User.objects.create_user(
  15. form.cleaned_data['username'],
  16. first_name=form.cleaned_data['first_name'],
  17. last_name=form.cleaned_data['last_name'],
  18. email=form.cleaned_data['email'],
  19. password=form.cleaned_data['password'])
  20. user.is_active = False
  21. user.save()
  22.  
  23. #2. Create activate code and save to server.
  24. uuid_code = str(uuid.uuid4()).replace("-", '')
  25. activate_code = ActivateCode(user=user, code=uuid_code)
  26. activate_code.save()
  27.  
  28. #3. Send Validation Email
  29. activate_link = "http://%s/user/activate/%s" %(request.get_host(), uuid_code)
  30. activate_message = """
  31. You're almost done!
  32. <a href="%s">Click here to complete your registration</a>
  33. """ % activate_link
  34. send_mail(subject="Complete Registration With Code Journey",
  35. message="Click here to complete your registration: %s" %activate_link,
  36. html_message=activate_message,
  37. from_email="anexmaple@mail.com",
  38. recipient_list=[form.cleaned_data['email'],],
  39. fail_silently=False)
  40.  
  41. #4. Prompt user to check his email.
  42. context = {'email': form.cleaned_data['email'],}
  43. return render(request, "user/validate.html", context)
Add Comment
Please, Sign In to add comment