forked from GithubBackups/healthchecks
made the unique field more flexible
This commit is contained in:
parent
bcde5fe9d2
commit
96cb68d503
@ -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
|
||||||
@ -56,11 +57,23 @@ def checks(request):
|
|||||||
|
|
||||||
elif request.method == "POST":
|
elif request.method == "POST":
|
||||||
|
|
||||||
unique = bool(request.json.get("unique", False))
|
unique_fields = request.json.get("unique", [])
|
||||||
name = str(request.json.get("name", ""))
|
name = str(request.json.get("name", ""))
|
||||||
|
|
||||||
if unique:
|
if len(unique_fields) > 0:
|
||||||
existing_checks = Check.objects.filter(user=request.user, name=name)
|
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:
|
if existing_checks.count() > 0:
|
||||||
# There might be more than one check with the same name since name
|
# There might be more than one check with the same name since name
|
||||||
|
@ -130,9 +130,11 @@ The response may contain a JSON document with additional data.
|
|||||||
<tr>
|
<tr>
|
||||||
<th>unique</th>
|
<th>unique</th>
|
||||||
<td>
|
<td>
|
||||||
<p>bool, optional, default value: False.</p>
|
<p>array of strings, optional, default value: [].</p>
|
||||||
<p>When true a check won't be created if there exists a check with the
|
<p>Tells the API to only create a new check if the combination of fields
|
||||||
same name. If any existing checks match then the first one will be returned.</p>
|
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>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user