64 lines
1.8 KiB
Python
64 lines
1.8 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 django.utils import timezone
|
|
|
|
from ..models import App
|
|
from frontend.models import Panic
|
|
|
|
import json
|
|
|
|
|
|
@method_decorator(csrf_exempt, name="dispatch")
|
|
class SolveView(View):
|
|
"""A view that processes a solving 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)
|
|
|
|
assert (panic := Panic.active())
|
|
|
|
panic.resolved_at = timezone.now()
|
|
panic.resolved_by_user = app.owner
|
|
panic.resolved_by_app = app
|
|
panic.resolve_reason = data["reason"]
|
|
panic.save()
|
|
|
|
responseData = {"success": True}
|
|
|
|
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 AssertionError:
|
|
return JsonResponse(
|
|
{"success": False, "error": "No panic is currently active"}
|
|
)
|
|
|
|
except App.DoesNotExist:
|
|
return JsonResponse(
|
|
{"success": False, "error": "Invalid app installation key"}
|
|
)
|