devicehub-django/snapshot/serializers.py
Cayo Puigdefabregas 26589b1ee5 modify snapshots
2024-07-01 12:17:23 +02:00

44 lines
1.4 KiB
Python

from rest_framework import serializers
from snapshot.models import SnapshotJson
import json
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from snapshot.parse import Parse
class SnapshotSerializer(serializers.ModelSerializer):
class Meta:
model = SnapshotJson
fields = ['id', 'title', 'content']
@csrf_exempt
def webhook_verify(request):
if request.method == 'POST':
auth_header = request.headers.get('Authorization')
if not auth_header or not auth_header.startswith('Bearer '):
return JsonResponse({'error': 'Invalid authorization'}, status=401)
token = auth_header.split(' ')[1]
tk = Token.objects.filter(token=token).first()
if not tk:
return JsonResponse({'error': 'Invalid authorization'}, status=401)
try:
data = json.loads(request.body)
except json.JSONDecodeError:
return JsonResponse({'error': 'Invalid JSON'}, status=400)
try:
device = Parse(data)
except Exception:
return JsonResponse({'error': 'Invalid JSON'}, status=400)
if not device:
return JsonResponse({'error': 'Invalid JSON'}, status=400)
return JsonResponse({"result": "Ok"}, status=200)
return JsonResponse({'error': 'Invalid request method'}, status=400)