|
@ -1,5 +1,4 @@ |
|
|
from datetime import timedelta as td |
|
|
from datetime import timedelta as td |
|
|
from datetime import datetime |
|
|
|
|
|
import time |
|
|
import time |
|
|
import uuid |
|
|
import uuid |
|
|
|
|
|
|
|
@ -21,6 +20,7 @@ from django.views.decorators.http import require_POST |
|
|
from hc.accounts.models import Profile |
|
|
from hc.accounts.models import Profile |
|
|
from hc.api import schemas |
|
|
from hc.api import schemas |
|
|
from hc.api.decorators import authorize, authorize_read, cors, validate_json |
|
|
from hc.api.decorators import authorize, authorize_read, cors, validate_json |
|
|
|
|
|
from hc.api.forms import FlipsFiltersForm |
|
|
from hc.api.models import MAX_DELTA, Flip, Channel, Check, Notification, Ping |
|
|
from hc.api.models import MAX_DELTA, Flip, Channel, Check, Notification, Ping |
|
|
from hc.lib.badges import check_signature, get_badge_svg |
|
|
from hc.lib.badges import check_signature, get_badge_svg |
|
|
|
|
|
|
|
@ -292,42 +292,38 @@ def pings(request, code): |
|
|
|
|
|
|
|
|
return JsonResponse({"pings": dicts}) |
|
|
return JsonResponse({"pings": dicts}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def flips(request, check): |
|
|
def flips(request, check): |
|
|
if check.project_id != request.project.id: |
|
|
if check.project_id != request.project.id: |
|
|
return HttpResponseForbidden() |
|
|
return HttpResponseForbidden() |
|
|
|
|
|
|
|
|
if all(x not in request.GET for x in ('start','end','seconds')): |
|
|
|
|
|
flips = Flip.objects.filter( |
|
|
|
|
|
owner=check, new_status__in=("down","up"), |
|
|
|
|
|
).order_by("created") |
|
|
|
|
|
else: |
|
|
|
|
|
if "seconds" in request.GET and ("start" in request.GET or "end" in request.GET): |
|
|
|
|
|
return HttpResponseBadRequest() |
|
|
|
|
|
|
|
|
form = FlipsFiltersForm(request.GET) |
|
|
|
|
|
if not form.is_valid(): |
|
|
|
|
|
return HttpResponseBadRequest() |
|
|
|
|
|
|
|
|
flips = Flip.objects.filter( |
|
|
|
|
|
owner=check, new_status__in=("down","up")) |
|
|
|
|
|
|
|
|
flips = Flip.objects.filter(owner=check).order_by("-id") |
|
|
|
|
|
|
|
|
if 'start' in request.GET: |
|
|
|
|
|
flips = flips.filter(created_gt=datetime.fromtimestamp(int(request.GET['start']))) |
|
|
|
|
|
|
|
|
|
|
|
if 'end' in request.GET: |
|
|
|
|
|
flips = flips.filter(created__lt=datetime.fromtimestamp(int(request.GET['end']))) |
|
|
|
|
|
|
|
|
if form.cleaned_data["start"]: |
|
|
|
|
|
flips = flips.filter(created__gte=form.cleaned_data["start"]) |
|
|
|
|
|
|
|
|
if 'seconds' in request.GET: |
|
|
|
|
|
flips = flips.filter(created_gt=datetime.now()-td(seconds=int(request.GET['seconds']))) |
|
|
|
|
|
|
|
|
if form.cleaned_data["end"]: |
|
|
|
|
|
flips = flips.filter(created__lt=form.cleaned_data["end"]) |
|
|
|
|
|
|
|
|
flips = flips.order_by("created") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if form.cleaned_data["seconds"]: |
|
|
|
|
|
threshold = timezone.now() - td(seconds=form.cleaned_data["seconds"]) |
|
|
|
|
|
flips = flips.filter(created__gte=threshold) |
|
|
|
|
|
|
|
|
return JsonResponse({"flips": [flip.to_dict() for flip in flips]}) |
|
|
return JsonResponse({"flips": [flip.to_dict() for flip in flips]}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@cors("GET") |
|
|
@cors("GET") |
|
|
@csrf_exempt |
|
|
@csrf_exempt |
|
|
@validate_json() |
|
|
@validate_json() |
|
|
@authorize_read |
|
|
@authorize_read |
|
|
def flips_by_uuid(request,code): |
|
|
|
|
|
|
|
|
def flips_by_uuid(request, code): |
|
|
check = get_object_or_404(Check, code=code) |
|
|
check = get_object_or_404(Check, code=code) |
|
|
return flips(request,check) |
|
|
|
|
|
|
|
|
return flips(request, check) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@cors("GET") |
|
|
@cors("GET") |
|
|
@csrf_exempt |
|
|
@csrf_exempt |
|
@ -337,9 +333,10 @@ def flips_by_unique_key(request, unique_key): |
|
|
checks = Check.objects.filter(project=request.project.id) |
|
|
checks = Check.objects.filter(project=request.project.id) |
|
|
for check in checks: |
|
|
for check in checks: |
|
|
if check.unique_key == unique_key: |
|
|
if check.unique_key == unique_key: |
|
|
return flips(request,check) |
|
|
|
|
|
|
|
|
return flips(request, check) |
|
|
return HttpResponseNotFound() |
|
|
return HttpResponseNotFound() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@never_cache |
|
|
@never_cache |
|
|
@cors("GET") |
|
|
@cors("GET") |
|
|
def badge(request, badge_key, signature, tag, fmt="svg"): |
|
|
def badge(request, badge_key, signature, tag, fmt="svg"): |
|
|