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.

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