60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
from django.views.generic import View
|
|
from django.http.response import JsonResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from ..models import App
|
|
from frontend.models import Panic
|
|
|
|
import json
|
|
|
|
|
|
@method_decorator(csrf_exempt, name="dispatch")
|
|
class HeartbeatView(View):
|
|
"""A view that processes a heartbeat request from the app installation"""
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
"""Process a heartbeat request from the app installation
|
|
|
|
Args:
|
|
request (HttpRequest): The request
|
|
*args: Additional arguments
|
|
**kwargs: Additional keyword arguments
|
|
|
|
Returns:
|
|
JsonResponse: The response
|
|
"""
|
|
|
|
try:
|
|
data = json.loads(request.body)
|
|
key = data["key"]
|
|
|
|
app = App.get_by_key(key)
|
|
app.heartbeat()
|
|
|
|
responseData = {"success": True}
|
|
|
|
if panic := Panic.active():
|
|
responseData["panic"] = True
|
|
responseData["triggered_by"] = panic.created_by_user.username
|
|
responseData["triggered_at"] = panic.created_at
|
|
responseData["reason"] = panic.create_reason
|
|
|
|
else:
|
|
responseData["panic"] = False
|
|
|
|
return JsonResponse(responseData)
|
|
|
|
except json.JSONDecodeError:
|
|
return JsonResponse({"success": False, "error": "Invalid JSON"})
|
|
|
|
except KeyError:
|
|
return JsonResponse(
|
|
{"success": False, "error": "Missing app installation key"}
|
|
)
|
|
|
|
except App.DoesNotExist:
|
|
return JsonResponse(
|
|
{"success": False, "error": "Invalid app installation key"}
|
|
)
|