<s> from datetime import timedelta as td <EOL> from django . db . models import F <EOL> from django . http import HttpResponse , HttpResponseBadRequest , JsonResponse <EOL> from django . utils import timezone <EOL> from django . views . decorators . cache import never_cache <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from hc . api import schemas <EOL> from hc . api . decorators import check_api_key , uuid_or_400 , validate_json <EOL> from hc . api . models import Check , Ping <EOL> @ csrf_exempt <EOL> @ uuid_or_400 <EOL> @ never_cache <EOL> def ping ( request , code ) : <EOL> try : <EOL> check = Check . objects . get ( code = code ) <EOL> except Check . DoesNotExist : <EOL> return HttpResponseBadRequest ( ) <EOL> check . n_pings = F ( "<STR_LIT>" ) + <NUM_LIT:1> <EOL> check . last_ping = timezone . now ( ) <EOL> if check . status == "<STR_LIT>" : <EOL> check . status = "<STR_LIT>" <EOL> check . save ( ) <EOL> check . refresh_from_db ( ) <EOL> ping = Ping ( owner = check ) <EOL> headers = request . META <EOL> ping . n = check . n_pings <EOL> remote_addr = headers . get ( "<STR_LIT>" , headers [ "<STR_LIT>" ] ) <EOL> ping . remote_addr = remote_addr . split ( "<STR_LIT:U+002C>" ) [ <NUM_LIT:0> ] <EOL> ping . scheme = headers . get ( "<STR_LIT>" , "<STR_LIT:http>" ) <EOL> ping . method = headers [ "<STR_LIT>" ] <EOL> ping . ua = headers . get ( "<STR_LIT>" , "<STR_LIT>" ) [ : <NUM_LIT:200> ] <EOL> ping . save ( ) <EOL> response = HttpResponse ( "<STR_LIT:OK>" ) <EOL> response [ "<STR_LIT>" ] = "<STR_LIT:*>" <EOL> return response <EOL> @ csrf_exempt <EOL> @ check_api_key <EOL> @ validate_json ( schemas . check ) <EOL> def create_check ( request ) : <EOL> if request . method == "<STR_LIT:GET>" : <EOL> code = <NUM_LIT:200> <EOL> response = { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT:name>" : check . name , <EOL> "<STR_LIT>" : check . url ( ) , <EOL> "<STR_LIT>" : check . tags , <EOL> "<STR_LIT>" : int ( check . timeout . total_seconds ( ) ) , <EOL> "<STR_LIT>" : int ( check . grace . total_seconds ( ) ) , <EOL> } for check in Check . objects . filter ( user = request . user ) ] <EOL> } <EOL> elif request . method == "<STR_LIT:POST>" : <EOL> check = Check ( user = request . user ) <EOL> check . name = str ( request . json . get ( "<STR_LIT:name>" , "<STR_LIT>" ) ) <EOL> check . tags = str ( request . json . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> if "<STR_LIT>" in request . json : <EOL> check . timeout = td ( seconds = request . json [ "<STR_LIT>" ] ) <EOL> if "<STR_LIT>" in request . json : <EOL> check . grace = td ( seconds = request . json [ "<STR_LIT>" ] ) <EOL> check . save ( ) <EOL> if request . json . get ( "<STR_LIT>" ) == "<STR_LIT:*>" : <EOL> check . assign_all_channels ( ) <EOL> code = <NUM_LIT> <EOL> response = { <EOL> "<STR_LIT>" : check . url ( ) <EOL> } <EOL> else : <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> return JsonResponse ( response , status = code ) </s>
