From 3c57e4026a3dc47aa6943a24191664b9b0b453a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Tue, 7 Jul 2015 23:49:05 +0300 Subject: [PATCH] Move timer call to api --- hc/api/urls.py | 1 + hc/api/views.py | 23 +++++++++++++++++++++++ hc/front/urls.py | 1 - hc/front/views.py | 25 +------------------------ static/js/index.js | 5 +++-- templates/index.html | 2 +- 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/hc/api/urls.py b/hc/api/urls.py index 71c41a42..3f688c92 100644 --- a/hc/api/urls.py +++ b/hc/api/urls.py @@ -5,4 +5,5 @@ from hc.api import views urlpatterns = [ url(r'^ping/([\w-]+)/$', views.ping, name="hc-ping"), url(r'^ping/([\w-]+)$', views.ping, name="hc-ping"), + url(r'^status/([\w-]+)/$', views.status, name="hc-status"), ] diff --git a/hc/api/views.py b/hc/api/views.py index 6b923266..29e009cd 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -1,3 +1,6 @@ +import json + +from django.contrib.humanize.templatetags.humanize import naturaltime from django.http import HttpResponse, HttpResponseBadRequest from django.utils import timezone @@ -17,3 +20,23 @@ def ping(request, code): check.save() return HttpResponse("OK") + + +def status(request, code): + response = { + "last_ping": None, + "last_ping_human": None, + "secs_to_alert": None + } + + check = Check.objects.get(code=code) + + if check.last_ping and check.alert_after: + response["last_ping"] = check.last_ping.isoformat() + response["last_ping_human"] = naturaltime(check.last_ping) + + duration = check.alert_after - timezone.now() + response["secs_to_alert"] = int(duration.total_seconds()) + + return HttpResponse(json.dumps(response), + content_type="application/javascript") diff --git a/hc/front/urls.py b/hc/front/urls.py index 8a11152d..c7f2fc58 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -11,5 +11,4 @@ urlpatterns = [ url(r'^pricing/$', views.pricing, name="hc-pricing"), url(r'^docs/$', views.docs, name="hc-docs"), url(r'^about/$', views.about, name="hc-about"), - url(r'^welcome/timer/$', views.welcome_timer, name="hc-welcome-timer"), ] diff --git a/hc/front/views.py b/hc/front/views.py index 5b71a053..e008f881 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -1,9 +1,5 @@ -import json - -from django.conf import settings from django.contrib.auth.decorators import login_required -from django.contrib.humanize.templatetags.humanize import naturaltime -from django.http import HttpResponse, HttpResponseForbidden +from django.http import HttpResponseForbidden from django.shortcuts import redirect, render from django.utils import timezone @@ -52,27 +48,8 @@ def about(request): return render(request, "about.html", {"page": "about"}) -def welcome_timer(request): - code = request.session["welcome_code"] - - check = Check.objects.get(code=code) - if check.last_ping and check.alert_after: - duration = check.alert_after - timezone.now() - response = { - "last_ping": check.last_ping.isoformat(), - "last_ping_human": naturaltime(check.last_ping), - "timer": int(duration.total_seconds()) - } - else: - response = {"last_ping": None, "timer": None} - - return HttpResponse(json.dumps(response), - content_type="application/javascript") - - @login_required def checks(request): - checks = Check.objects.filter(user=request.user).order_by("created") ctx = { diff --git a/static/js/index.js b/static/js/index.js index 0b897837..312232c0 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -1,6 +1,7 @@ $(function () { $('[data-toggle="tooltip"]').tooltip(); + var code = $("#check-code").text(); var url = $("#pitch-url").text(); var lastPing = null; var lastPingHuman = null; @@ -10,10 +11,10 @@ $(function () { }); function checkLastPing() { - $.getJSON("/welcome/timer/", function(data) { + $.getJSON("/status/" + code + "/", function(data) { if (data.last_ping != lastPing) { lastPing = data.last_ping; - $("#timer").data("timer", data.timer); + $("#timer").data("timer", data.secs_to_alert); } var lph = data.last_ping_human; diff --git a/templates/index.html b/templates/index.html index d6bbcf08..92a5dc73 100644 --- a/templates/index.html +++ b/templates/index.html @@ -66,7 +66,7 @@

Status - {{ check.code }} + {{ check.code }}