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.

303 lines
7.2 KiB

9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 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 HttpResponseBadRequest, HttpResponseForbidden
  5. from django.shortcuts import get_object_or_404, 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. @login_required
  11. def my_checks(request):
  12. checks = Check.objects.filter(user=request.user).order_by("created")
  13. ctx = {
  14. "page": "checks",
  15. "checks": checks,
  16. "now": timezone.now()
  17. }
  18. return render(request, "front/my_checks.html", ctx)
  19. def index(request):
  20. if request.user.is_authenticated():
  21. return redirect("hc-checks")
  22. if "welcome_code" not in request.session:
  23. check = Check()
  24. check.save()
  25. code = str(check.code)
  26. request.session["welcome_code"] = code
  27. else:
  28. code = request.session["welcome_code"]
  29. check = Check.objects.get(code=code)
  30. ctx = {
  31. "page": "welcome",
  32. "check": check,
  33. "ping_url": check.url()
  34. }
  35. return render(request, "front/welcome.html", ctx)
  36. def pricing(request):
  37. return render(request, "front/pricing.html", {"page": "pricing"})
  38. def docs(request):
  39. if "welcome_code" in request.session:
  40. code = request.session["welcome_code"]
  41. check = Check.objects.get(code=code)
  42. else:
  43. check = Check(code="uuid-goes-here")
  44. ctx = {
  45. "page": "docs",
  46. "ping_endpoint": settings.PING_ENDPOINT,
  47. "check": check,
  48. "ping_url": check.url()
  49. }
  50. return render(request, "front/docs.html", ctx)
  51. def about(request):
  52. return render(request, "front/about.html", {"page": "about"})
  53. @login_required
  54. def add_check(request):
  55. assert request.method == "POST"
  56. check = Check(user=request.user)
  57. check.save()
  58. check.assign_all_channels()
  59. return redirect("hc-checks")
  60. @login_required
  61. @uuid_or_400
  62. def update_name(request, code):
  63. assert request.method == "POST"
  64. check = get_object_or_404(Check, code=code)
  65. if check.user != request.user:
  66. return HttpResponseForbidden()
  67. check.name = request.POST["name"]
  68. check.save()
  69. return redirect("hc-checks")
  70. @login_required
  71. @uuid_or_400
  72. def update_timeout(request, code):
  73. assert request.method == "POST"
  74. check = get_object_or_404(Check, code=code)
  75. if check.user != request.user:
  76. return HttpResponseForbidden()
  77. form = TimeoutForm(request.POST)
  78. if form.is_valid():
  79. check.timeout = td(seconds=form.cleaned_data["timeout"])
  80. check.grace = td(seconds=form.cleaned_data["grace"])
  81. check.save()
  82. return redirect("hc-checks")
  83. @login_required
  84. @uuid_or_400
  85. def email_preview(request, code):
  86. """ A debug view to see how email will look.
  87. Will keep it around until I'm happy with email stying.
  88. """
  89. check = Check.objects.get(code=code)
  90. if check.user != request.user:
  91. return HttpResponseForbidden()
  92. ctx = {
  93. "check": check,
  94. "checks": check.user.check_set.all(),
  95. "now": timezone.now()
  96. }
  97. return render(request, "emails/alert/body.html", ctx)
  98. @login_required
  99. @uuid_or_400
  100. def remove_check(request, code):
  101. assert request.method == "POST"
  102. check = get_object_or_404(Check, code=code)
  103. if check.user != request.user:
  104. return HttpResponseForbidden()
  105. check.delete()
  106. return redirect("hc-checks")
  107. @login_required
  108. @uuid_or_400
  109. def log(request, code):
  110. check = get_object_or_404(Check, code=code)
  111. if check.user != request.user:
  112. return HttpResponseForbidden()
  113. pings = Ping.objects.filter(owner=check).order_by("-created")[:100]
  114. ctx = {
  115. "check": check,
  116. "pings": pings
  117. }
  118. return render(request, "front/log.html", ctx)
  119. @login_required
  120. def channels(request):
  121. if request.method == "POST":
  122. code = request.POST["channel"]
  123. try:
  124. channel = Channel.objects.get(code=code)
  125. except Channel.DoesNotExist:
  126. return HttpResponseBadRequest()
  127. if channel.user != request.user:
  128. return HttpResponseForbidden()
  129. new_checks = []
  130. for key in request.POST:
  131. if key.startswith("check-"):
  132. code = key[6:]
  133. try:
  134. check = Check.objects.get(code=code)
  135. except Check.DoesNotExist:
  136. return HttpResponseBadRequest()
  137. if check.user != request.user:
  138. return HttpResponseForbidden()
  139. new_checks.append(check)
  140. channel.checks = new_checks
  141. return redirect("hc-channels")
  142. channels = Channel.objects.filter(user=request.user).order_by("created")
  143. num_checks = Check.objects.filter(user=request.user).count()
  144. ctx = {
  145. "page": "channels",
  146. "channels": channels,
  147. "num_checks": num_checks
  148. }
  149. return render(request, "front/channels.html", ctx)
  150. @login_required
  151. def add_channel(request):
  152. assert request.method == "POST"
  153. form = AddChannelForm(request.POST)
  154. if form.is_valid():
  155. channel = form.save(commit=False)
  156. channel.user = request.user
  157. channel.save()
  158. checks = Check.objects.filter(user=request.user)
  159. channel.checks.add(*checks)
  160. if channel.kind == "email":
  161. channel.send_verify_link()
  162. return redirect("hc-channels")
  163. else:
  164. return HttpResponseBadRequest()
  165. @login_required
  166. @uuid_or_400
  167. def channel_checks(request, code):
  168. channel = get_object_or_404(Channel, code=code)
  169. if channel.user != request.user:
  170. return HttpResponseForbidden()
  171. assigned = set([check.code for check in channel.checks.all()])
  172. checks = Check.objects.filter(user=request.user).order_by("created")
  173. ctx = {
  174. "checks": checks,
  175. "assigned": assigned,
  176. "channel": channel
  177. }
  178. return render(request, "front/channel_checks.html", ctx)
  179. @uuid_or_400
  180. def verify_email(request, code, token):
  181. channel = get_object_or_404(Channel, code=code)
  182. if channel.make_token() == token:
  183. channel.email_verified = True
  184. channel.save()
  185. return render(request, "front/verify_email_success.html")
  186. return render(request, "bad_link.html")
  187. @login_required
  188. @uuid_or_400
  189. def remove_channel(request, code):
  190. assert request.method == "POST"
  191. channel = get_object_or_404(Channel, code=code)
  192. if channel.user != request.user:
  193. return HttpResponseForbidden()
  194. channel.delete()
  195. return redirect("hc-channels")
  196. @login_required
  197. def add_email(request):
  198. ctx = {"page": "channels"}
  199. return render(request, "integrations/add_email.html", ctx)
  200. @login_required
  201. def add_webhook(request):
  202. ctx = {"page": "channels"}
  203. return render(request, "integrations/add_webhook.html", ctx)
  204. @login_required
  205. def add_pd(request):
  206. ctx = {"page": "channels"}
  207. return render(request, "integrations/add_pd.html", ctx)
  208. @login_required
  209. def add_slack(request):
  210. ctx = {"page": "channels"}
  211. return render(request, "integrations/add_slack.html", ctx)
  212. @login_required
  213. def add_hipchat(request):
  214. ctx = {"page": "channels"}
  215. return render(request, "integrations/add_hipchat.html", ctx)