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.

237 lines
5.5 KiB

9 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
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
  1. from datetime import timedelta as td
  2. from django.conf import settings
  3. from django.contrib.auth.decorators import login_required
  4. from django.http import HttpResponseForbidden
  5. from django.shortcuts import redirect, render
  6. from django.utils import timezone
  7. from hc.api.decorators import uuid_or_400
  8. from hc.api.models import Channel, Check, Ping
  9. from hc.front.forms import AddChannelForm, TimeoutForm
  10. def _welcome(request):
  11. if "welcome_code" not in request.session:
  12. check = Check()
  13. check.save()
  14. code = str(check.code)
  15. request.session["welcome_code"] = code
  16. else:
  17. code = request.session["welcome_code"]
  18. check = Check.objects.get(code=code)
  19. ctx = {
  20. "page": "welcome",
  21. "check": check,
  22. "ping_url": check.url()
  23. }
  24. return render(request, "front/welcome.html", ctx)
  25. def _my_checks(request):
  26. checks = Check.objects.filter(user=request.user).order_by("created")
  27. ctx = {
  28. "checks": checks,
  29. "now": timezone.now()
  30. }
  31. return render(request, "front/my_checks.html", ctx)
  32. def index(request):
  33. if request.user.is_authenticated():
  34. return _my_checks(request)
  35. else:
  36. return _welcome(request)
  37. def pricing(request):
  38. return render(request, "front/pricing.html", {"page": "pricing"})
  39. def docs(request):
  40. if "welcome_code" in request.session:
  41. code = request.session["welcome_code"]
  42. check = Check.objects.get(code=code)
  43. else:
  44. check = Check(code="uuid-goes-here")
  45. ctx = {
  46. "page": "docs",
  47. "ping_endpoint": settings.PING_ENDPOINT,
  48. "check": check,
  49. "ping_url": check.url()
  50. }
  51. return render(request, "front/docs.html", ctx)
  52. def about(request):
  53. return render(request, "front/about.html", {"page": "about"})
  54. @login_required
  55. def add_check(request):
  56. assert request.method == "POST"
  57. check = Check(user=request.user)
  58. check.save()
  59. check.assign_all_channels()
  60. return redirect("hc-index")
  61. @login_required
  62. @uuid_or_400
  63. def update_name(request, code):
  64. assert request.method == "POST"
  65. check = Check.objects.get(code=code)
  66. if check.user != request.user:
  67. return HttpResponseForbidden()
  68. check.name = request.POST["name"]
  69. check.save()
  70. return redirect("hc-index")
  71. @login_required
  72. @uuid_or_400
  73. def update_timeout(request, code):
  74. assert request.method == "POST"
  75. check = Check.objects.get(code=code)
  76. if check.user != request.user:
  77. return HttpResponseForbidden()
  78. form = TimeoutForm(request.POST)
  79. if form.is_valid():
  80. check.timeout = td(seconds=form.cleaned_data["timeout"])
  81. check.grace = td(seconds=form.cleaned_data["grace"])
  82. check.save()
  83. return redirect("hc-index")
  84. @login_required
  85. @uuid_or_400
  86. def email_preview(request, code):
  87. """ A debug view to see how email will look.
  88. Will keep it around until I'm happy with email stying.
  89. """
  90. check = Check.objects.get(code=code)
  91. if check.user != request.user:
  92. return HttpResponseForbidden()
  93. ctx = {
  94. "check": check,
  95. "checks": check.user.check_set.all(),
  96. "now": timezone.now()
  97. }
  98. return render(request, "emails/alert/body.html", ctx)
  99. @login_required
  100. @uuid_or_400
  101. def remove(request, code):
  102. assert request.method == "POST"
  103. check = Check.objects.get(code=code)
  104. if check.user != request.user:
  105. return HttpResponseForbidden()
  106. check.delete()
  107. return redirect("hc-index")
  108. @login_required
  109. @uuid_or_400
  110. def log(request, code):
  111. check = Check.objects.get(code=code)
  112. if check.user != request.user:
  113. return HttpResponseForbidden()
  114. pings = Ping.objects.filter(owner=check).order_by("-created")[:100]
  115. ctx = {
  116. "check": check,
  117. "pings": pings
  118. }
  119. return render(request, "front/log.html", ctx)
  120. @login_required
  121. def channels(request):
  122. if request.method == "POST":
  123. code = request.POST["channel"]
  124. channel = Channel.objects.get(code=code)
  125. if channel.user != request.user:
  126. return HttpResponseForbidden()
  127. new_checks = []
  128. for key in request.POST:
  129. if key.startswith("check-"):
  130. code = key[6:]
  131. check = Check.objects.get(code=code)
  132. if check.user != request.user:
  133. return HttpResponseForbidden()
  134. new_checks.append(check)
  135. channel.checks = new_checks
  136. return redirect("hc-channels")
  137. channels = Channel.objects.filter(user=request.user).order_by("created")
  138. num_checks = Check.objects.filter(user=request.user).count()
  139. ctx = {
  140. "channels": channels,
  141. "num_checks": num_checks
  142. }
  143. return render(request, "front/channels.html", ctx)
  144. @login_required
  145. def add_channel(request):
  146. assert request.method == "POST"
  147. form = AddChannelForm(request.POST)
  148. if form.is_valid():
  149. channel = form.save(commit=False)
  150. channel.user = request.user
  151. channel.save()
  152. checks = Check.objects.filter(user=request.user)
  153. channel.checks.add(*checks)
  154. return redirect("hc-channels")
  155. @login_required
  156. @uuid_or_400
  157. def channel_checks(request, code):
  158. channel = Channel.objects.get(code=code)
  159. assigned = set([check.code for check in channel.checks.all()])
  160. checks = Check.objects.filter(user=request.user).order_by("created")
  161. ctx = {
  162. "checks": checks,
  163. "assigned": assigned,
  164. "channel": channel
  165. }
  166. return render(request, "front/channel_checks.html", ctx)