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.

59 lines
1.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. import uuid
  2. from django.conf import settings
  3. from django.contrib.auth import authenticate, login as auth_login
  4. from django.contrib.auth.models import User
  5. from django.core.mail import send_mail
  6. from django.core.urlresolvers import reverse
  7. from django.shortcuts import redirect, render
  8. from hc.accounts.forms import EmailForm
  9. def login(request):
  10. if request.method == 'POST':
  11. # create a form instance and populate it with data from the request:
  12. form = EmailForm(request.POST)
  13. # check whether it's valid:
  14. if form.is_valid():
  15. email = form.cleaned_data["email"]
  16. user = User.objects.get(email=email)
  17. token = str(uuid.uuid4())
  18. user.set_password(token)
  19. user.save()
  20. login_link = reverse("hc-check-token", args=[user.username, token])
  21. login_link = settings.SITE_ROOT + login_link
  22. body = "login link: %s" % login_link
  23. send_mail('Log In', body, '[email protected]', [email],
  24. fail_silently=False)
  25. # FIXME send login token here
  26. return redirect("hc-login-link-sent")
  27. else:
  28. form = EmailForm()
  29. ctx = {
  30. "form": form
  31. }
  32. return render(request, "accounts/login.html", ctx)
  33. def login_link_sent(request):
  34. return render(request, "accounts/login_link_sent.html")
  35. def check_token(request, username, token):
  36. user = authenticate(username=username, password=token)
  37. if user is not None:
  38. if user.is_active:
  39. user.set_unusable_password()
  40. user.save()
  41. auth_login(request, user)
  42. return redirect("hc-checks")
  43. return render(request, "bad_link.html")