From 925d34daad5b725d23fbd9b7c55e7499fa3c3685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 13 Dec 2018 16:53:26 +0200 Subject: [PATCH] Update Check.status field when user edits timeout & grace settings --- CHANGELOG.md | 1 + hc/front/tests/test_update_timeout.py | 18 ++++++++++++++++++ hc/front/views.py | 13 ++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66315bed..2c4d50a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. ### Bug Fixes - Fix after-login redirects (the "?next=" query parameter) +- Update Check.status field when user edits timeout & grace settings ## 1.3.0 - 2018-11-21 diff --git a/hc/front/tests/test_update_timeout.py b/hc/front/tests/test_update_timeout.py index 8d6a8e9a..7fbd8291 100644 --- a/hc/front/tests/test_update_timeout.py +++ b/hc/front/tests/test_update_timeout.py @@ -1,3 +1,5 @@ +from datetime import timedelta as td + from django.utils import timezone from hc.api.models import Check from hc.test import BaseTestCase @@ -27,6 +29,22 @@ class UpdateTimeoutTestCase(BaseTestCase): # alert_after should be updated too self.assertEqual(self.check.alert_after, self.check.get_alert_after()) + def test_it_updates_status(self): + self.check.last_ping = timezone.now() - td(days=2) + self.check.status = "down" + self.check.save() + + url = "/checks/%s/timeout/" % self.check.code + # 1 week: + payload = {"kind": "simple", "timeout": 3600 * 24 * 7, "grace": 60} + + self.client.login(username="alice@example.org", password="password") + r = self.client.post(url, data=payload) + self.assertRedirects(r, "/checks/") + + self.check.refresh_from_db() + self.assertEqual(self.check.status, "up") + def test_it_saves_cron_expression(self): url = "/checks/%s/timeout/" % self.check.code payload = { diff --git a/hc/front/views.py b/hc/front/views.py index 2ff7edf5..43e68e4d 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -18,7 +18,7 @@ from django.utils.crypto import get_random_string from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_POST from hc.api.models import (DEFAULT_GRACE, DEFAULT_TIMEOUT, Channel, Check, - Ping, Notification) + Flip, Ping, Notification) from hc.api.transports import Telegram from hc.front.forms import (AddWebhookForm, NameTagsForm, TimeoutForm, AddUrlForm, AddEmailForm, @@ -300,6 +300,17 @@ def update_timeout(request, code): if check.last_ping: check.alert_after = check.get_alert_after() + # Changing timeout can change check's status: + is_up = check.get_status() in ("up", "grace") + if is_up and check.status != "up": + flip = Flip(owner=check) + flip.created = timezone.now() + flip.old_status = check.status + flip.new_status = "up" + flip.save() + + check.status = "up" + check.save() if "/details/" in request.META.get("HTTP_REFERER", ""):