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.

147 lines
4.0 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
9 years ago
  1. import uuid
  2. from django.conf import settings
  3. from django.contrib import messages
  4. from django.contrib.auth import login as auth_login
  5. from django.contrib.auth import logout as auth_logout
  6. from django.contrib.auth import authenticate
  7. from django.contrib.auth.decorators import login_required
  8. from django.contrib.auth.models import User
  9. from django.core import signing
  10. from django.core.urlresolvers import reverse
  11. from django.http import HttpResponseBadRequest
  12. from django.shortcuts import redirect, render
  13. from hc.accounts.forms import EmailForm, ReportSettingsForm
  14. from hc.accounts.models import Profile
  15. from hc.api.models import Channel, Check
  16. from hc.lib import emails
  17. def _make_user(email):
  18. username = str(uuid.uuid4())[:30]
  19. user = User(username=username, email=email)
  20. user.save()
  21. channel = Channel()
  22. channel.user = user
  23. channel.kind = "email"
  24. channel.value = email
  25. channel.email_verified = True
  26. channel.save()
  27. return user
  28. def _associate_demo_check(request, user):
  29. if "welcome_code" in request.session:
  30. check = Check.objects.get(code=request.session["welcome_code"])
  31. # Only associate demo check if it doesn't have an owner already.
  32. if check.user is None:
  33. check.user = user
  34. check.save()
  35. check.assign_all_channels()
  36. del request.session["welcome_code"]
  37. def _send_login_link(user):
  38. token = str(uuid.uuid4())
  39. user.set_password(token)
  40. user.save()
  41. login_link = reverse("hc-check-token", args=[user.username, token])
  42. login_link = settings.SITE_ROOT + login_link
  43. ctx = {"login_link": login_link}
  44. emails.login(user.email, ctx)
  45. def login(request):
  46. if request.method == 'POST':
  47. form = EmailForm(request.POST)
  48. if form.is_valid():
  49. email = form.cleaned_data["email"]
  50. try:
  51. user = User.objects.get(email=email)
  52. except User.DoesNotExist:
  53. user = _make_user(email)
  54. _associate_demo_check(request, user)
  55. # We don't want to reset passwords of staff users :-)
  56. if user.is_staff:
  57. return HttpResponseBadRequest()
  58. _send_login_link(user)
  59. return redirect("hc-login-link-sent")
  60. else:
  61. form = EmailForm()
  62. bad_link = request.session.pop("bad_link", None)
  63. ctx = {"form": form, "bad_link": bad_link}
  64. return render(request, "accounts/login.html", ctx)
  65. def logout(request):
  66. auth_logout(request)
  67. return redirect("hc-index")
  68. def login_link_sent(request):
  69. return render(request, "accounts/login_link_sent.html")
  70. def check_token(request, username, token):
  71. if request.user.is_authenticated() and request.user.username == username:
  72. # User is already logged in
  73. return redirect("hc-checks")
  74. user = authenticate(username=username, password=token)
  75. if user is not None:
  76. if user.is_active:
  77. # This should get rid of "welcome_code" in session
  78. request.session.flush()
  79. user.set_unusable_password()
  80. user.save()
  81. auth_login(request, user)
  82. return redirect("hc-checks")
  83. request.session["bad_link"] = True
  84. return redirect("hc-login")
  85. @login_required
  86. def profile(request):
  87. profile = Profile.objects.for_user(request.user)
  88. if request.method == "POST":
  89. form = ReportSettingsForm(request.POST)
  90. if form.is_valid():
  91. profile.reports_allowed = form.cleaned_data["reports_allowed"]
  92. profile.save()
  93. messages.info(request, "Your settings have been updated!")
  94. ctx = {
  95. "profile": profile
  96. }
  97. return render(request, "accounts/profile.html", ctx)
  98. def unsubscribe_reports(request, username):
  99. try:
  100. signing.Signer().unsign(request.GET.get("token"))
  101. except signing.BadSignature:
  102. return HttpResponseBadRequest()
  103. user = User.objects.get(username=username)
  104. profile = Profile.objects.for_user(user)
  105. profile.reports_allowed = False
  106. profile.save()
  107. return render(request, "accounts/unsubscribed.html")