From bcde5fe9d248c886df12b16c7d7aac95ad7b2462 Mon Sep 17 00:00:00 2001
From: James Moore
Date: Thu, 22 Sep 2016 12:00:25 -0700
Subject: [PATCH 1/2] adds a unique parameter to the check creation API
It only checks for name uniqueness.
---
hc/api/views.py | 14 +++++++++++++-
templates/front/docs_api.html | 8 ++++++++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/hc/api/views.py b/hc/api/views.py
index 70b3f3ed..9139cb5f 100644
--- a/hc/api/views.py
+++ b/hc/api/views.py
@@ -55,8 +55,20 @@ def checks(request):
return JsonResponse(doc)
elif request.method == "POST":
+
+ unique = bool(request.json.get("unique", False))
+ name = str(request.json.get("name", ""))
+
+ if unique:
+ existing_checks = Check.objects.filter(user=request.user, name=name)
+
+ if existing_checks.count() > 0:
+ # There might be more than one check with the same name since name
+ # uniqueness isn't enforced in the model
+ return JsonResponse(existing_checks.first().to_dict(), status=200)
+
check = Check(user=request.user)
- check.name = str(request.json.get("name", ""))
+ check.name = name
check.tags = str(request.json.get("tags", ""))
if "timeout" in request.json:
check.timeout = td(seconds=request.json["timeout"])
diff --git a/templates/front/docs_api.html b/templates/front/docs_api.html
index b176b095..a680d46e 100644
--- a/templates/front/docs_api.html
+++ b/templates/front/docs_api.html
@@ -127,6 +127,14 @@ The response may contain a JSON document with additional data.
to automatically assign all existing notification channels.
+
+ unique |
+
+ bool, optional, default value: False.
+ When true a check won't be created if there exists a check with the
+ same name. If any existing checks match then the first one will be returned.
+ |
+
Example Request
From 96cb68d503e1b802295ee3f8034dd72e49063118 Mon Sep 17 00:00:00 2001
From: James Moore
Date: Thu, 22 Sep 2016 15:51:03 -0700
Subject: [PATCH 2/2] made the unique field more flexible
---
hc/api/views.py | 19 ++++++++++++++++---
templates/front/docs_api.html | 8 +++++---
2 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/hc/api/views.py b/hc/api/views.py
index 9139cb5f..c81ea2d1 100644
--- a/hc/api/views.py
+++ b/hc/api/views.py
@@ -1,5 +1,6 @@
from datetime import timedelta as td
+from django.core.exceptions import FieldError
from django.db.models import F
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
from django.utils import timezone
@@ -56,11 +57,23 @@ def checks(request):
elif request.method == "POST":
- unique = bool(request.json.get("unique", False))
+ unique_fields = request.json.get("unique", [])
name = str(request.json.get("name", ""))
- if unique:
- existing_checks = Check.objects.filter(user=request.user, name=name)
+ if len(unique_fields) > 0:
+ existing_checks = Check.objects.filter(user=request.user)
+
+ for unique_field in unique_fields:
+
+ field_value = request.json.get(unique_field)
+
+ if unique_field == "timeout" or unique_field == "grace":
+ field_value = td(seconds=field_value)
+
+ try:
+ existing_checks = existing_checks.filter(**{unique_field: field_value})
+ except FieldError:
+ return HttpResponse(status=400)
if existing_checks.count() > 0:
# There might be more than one check with the same name since name
diff --git a/templates/front/docs_api.html b/templates/front/docs_api.html
index a680d46e..2299eec4 100644
--- a/templates/front/docs_api.html
+++ b/templates/front/docs_api.html
@@ -130,9 +130,11 @@ The response may contain a JSON document with additional data.
unique |
- bool, optional, default value: False.
- When true a check won't be created if there exists a check with the
- same name. If any existing checks match then the first one will be returned.
+ array of strings, optional, default value: [].
+ Tells the API to only create a new check if the combination of fields
+ in unqiue is unique. The fields currently supported are
+ name, tags, timeout, and grace. If a new check is created the API returns
+ a 201 code, otherwise it returns a 200 code.
|