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.

88 lines
2.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
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.http import HttpResponseBadRequest
  8. from django.shortcuts import redirect, render
  9. from hc.accounts.forms import EmailForm
  10. def create(request):
  11. assert request.method == "POST"
  12. form = EmailForm(request.POST)
  13. if form.is_valid():
  14. email = form.cleaned_data["email"]
  15. num_existing = User.objects.filter(email=email).count()
  16. if num_existing > 0:
  17. # FIXME be more polite about this
  18. return HttpResponseBadRequest()
  19. username = str(uuid.uuid4())[:30]
  20. temp_password = str(uuid.uuid4())
  21. user = User(username=username, email=email)
  22. user.set_password(temp_password)
  23. user.save()
  24. user = authenticate(username=username, password=temp_password)
  25. user.set_unusable_password()
  26. user.save()
  27. auth_login(request, user)
  28. return redirect("hc-checks")
  29. # FIXME do something nicer here
  30. return HttpResponseBadRequest()
  31. def login(request):
  32. if request.method == 'POST':
  33. form = EmailForm(request.POST)
  34. if form.is_valid():
  35. email = form.cleaned_data["email"].lower()
  36. user = User.objects.get(email=email)
  37. # We don't want to reset passwords of staff users :-)
  38. if user.is_staff:
  39. return HttpResponseBadRequest()
  40. token = str(uuid.uuid4())
  41. user.set_password(token)
  42. user.save()
  43. login_link = reverse("hc-check-token", args=[user.username, token])
  44. login_link = settings.SITE_ROOT + login_link
  45. body = "login link: %s" % login_link
  46. send_mail('Log In', body, '[email protected]', [email],
  47. fail_silently=False)
  48. return redirect("hc-login-link-sent")
  49. else:
  50. form = EmailForm()
  51. ctx = {"form": form}
  52. return render(request, "accounts/login.html", ctx)
  53. def login_link_sent(request):
  54. return render(request, "accounts/login_link_sent.html")
  55. def check_token(request, username, token):
  56. user = authenticate(username=username, password=token)
  57. if user is not None:
  58. if user.is_active:
  59. user.set_unusable_password()
  60. user.save()
  61. auth_login(request, user)
  62. return redirect("hc-checks")
  63. return render(request, "bad_link.html")