59 lines
1.6 KiB
Python
59 lines
1.6 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 PanicView(View):
|
|
"""A view that processes a panic creation 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 not Panic.active()
|
|
|
|
panic = Panic.objects.create(
|
|
created_by_app=app,
|
|
created_by_user=app.owner,
|
|
create_reason=data["reason"],
|
|
)
|
|
|
|
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 App.DoesNotExist:
|
|
return JsonResponse(
|
|
{"success": False, "error": "Invalid app installation key"}
|
|
)
|