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.

167 lines
3.7 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
10 years ago
9 years ago
10 years ago
10 years ago
10 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.models import Check, Ping
  8. from hc.front.forms import TimeoutForm
  9. def _welcome(request):
  10. if "welcome_code" not in request.session:
  11. check = Check()
  12. check.save()
  13. code = str(check.code)
  14. request.session["welcome_code"] = code
  15. else:
  16. code = request.session["welcome_code"]
  17. check = Check.objects.get(code=code)
  18. if check.alert_after:
  19. duration = check.alert_after - timezone.now()
  20. timer = int(duration.total_seconds())
  21. timer_formatted = "%dh %dm %ds" % (timer / 3600, (timer / 60) % 60, timer % 60)
  22. else:
  23. timer = 0
  24. timer_formatted = "Never"
  25. ctx = {
  26. "page": "welcome",
  27. "check": check,
  28. "timer": timer,
  29. "timer_formatted": timer_formatted,
  30. "ping_url": check.url()
  31. }
  32. return render(request, "front/welcome.html", ctx)
  33. def _my_checks(request):
  34. checks = Check.objects.filter(user=request.user).order_by("created")
  35. ctx = {
  36. "checks": checks,
  37. "now": timezone.now()
  38. }
  39. return render(request, "front/my_checks.html", ctx)
  40. def index(request):
  41. if request.user.is_authenticated():
  42. return _my_checks(request)
  43. else:
  44. return _welcome(request)
  45. def pricing(request):
  46. return render(request, "front/pricing.html", {"page": "pricing"})
  47. def docs(request):
  48. ctx = {
  49. "page": "docs",
  50. "ping_endpoint": settings.PING_ENDPOINT
  51. }
  52. return render(request, "front/docs.html", ctx)
  53. def about(request):
  54. return render(request, "front/about.html", {"page": "about"})
  55. @login_required
  56. def add_check(request):
  57. assert request.method == "POST"
  58. check = Check(user=request.user)
  59. check.save()
  60. return redirect("hc-index")
  61. @login_required
  62. def update_name(request, code):
  63. assert request.method == "POST"
  64. check = Check.objects.get(code=code)
  65. if check.user != request.user:
  66. return HttpResponseForbidden()
  67. check.name = request.POST["name"]
  68. check.save()
  69. return redirect("hc-index")
  70. @login_required
  71. def update_timeout(request, code):
  72. assert request.method == "POST"
  73. check = Check.objects.get(code=code)
  74. if check.user != request.user:
  75. return HttpResponseForbidden()
  76. form = TimeoutForm(request.POST)
  77. if form.is_valid():
  78. check.timeout = td(seconds=form.cleaned_data["timeout"])
  79. check.grace = td(seconds=form.cleaned_data["grace"])
  80. check.save()
  81. return redirect("hc-index")
  82. @login_required
  83. def email_preview(request, code):
  84. """ A debug view to see how email will look.
  85. Will keep it around until I'm happy with email stying.
  86. """
  87. check = Check.objects.get(code=code)
  88. if check.user != request.user:
  89. return HttpResponseForbidden()
  90. ctx = {
  91. "check": check,
  92. "checks": check.user.check_set.all(),
  93. "now": timezone.now()
  94. }
  95. return render(request, "emails/alert/body.html", ctx)
  96. @login_required
  97. def remove(request, code):
  98. assert request.method == "POST"
  99. check = Check.objects.get(code=code)
  100. if check.user != request.user:
  101. return HttpResponseForbidden()
  102. check.delete()
  103. return redirect("hc-index")
  104. @login_required
  105. def log(request, code):
  106. check = Check.objects.get(code=code)
  107. if check.user != request.user:
  108. return HttpResponseForbidden()
  109. pings = Ping.objects.filter(owner=check).order_by("-created")[:100]
  110. ctx = {
  111. "check": check,
  112. "pings": pings
  113. }
  114. return render(request, "front/log.html", ctx)