forked from GithubBackups/healthchecks
Merge branch 'foozmeat-issue-82'
This commit is contained in:
commit
1c5182278e
@ -1,5 +1,6 @@
|
|||||||
from datetime import timedelta as td
|
from datetime import timedelta as td
|
||||||
|
|
||||||
|
from django.core.exceptions import FieldError
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
|
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@ -55,8 +56,32 @@ def checks(request):
|
|||||||
return JsonResponse(doc)
|
return JsonResponse(doc)
|
||||||
|
|
||||||
elif request.method == "POST":
|
elif request.method == "POST":
|
||||||
|
|
||||||
|
unique_fields = request.json.get("unique", [])
|
||||||
|
name = str(request.json.get("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
|
||||||
|
# uniqueness isn't enforced in the model
|
||||||
|
return JsonResponse(existing_checks.first().to_dict(), status=200)
|
||||||
|
|
||||||
check = Check(user=request.user)
|
check = Check(user=request.user)
|
||||||
check.name = str(request.json.get("name", ""))
|
check.name = name
|
||||||
check.tags = str(request.json.get("tags", ""))
|
check.tags = str(request.json.get("tags", ""))
|
||||||
if "timeout" in request.json:
|
if "timeout" in request.json:
|
||||||
check.timeout = td(seconds=request.json["timeout"])
|
check.timeout = td(seconds=request.json["timeout"])
|
||||||
|
@ -127,6 +127,16 @@ The response may contain a JSON document with additional data.
|
|||||||
to automatically assign all existing notification channels.</p>
|
to automatically assign all existing notification channels.</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>unique</th>
|
||||||
|
<td>
|
||||||
|
<p>array of strings, optional, default value: [].</p>
|
||||||
|
<p>Tells the API to only create a new check if the combination of fields
|
||||||
|
in <code>unqiue</code> 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.</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<h3 class="api-section">Example Request</h3>
|
<h3 class="api-section">Example Request</h3>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user