devicehub-django/evidence/serializers.py

44 lines
1.4 KiB
Python
Raw Normal View History

2024-06-12 07:32:49 +00:00
from rest_framework import serializers
2024-07-26 15:59:34 +00:00
from evidence.models import EvidenceJson
2024-07-01 10:17:23 +00:00
import json
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
2024-07-26 15:59:34 +00:00
from evidence.parse import Parse
2024-06-12 07:32:49 +00:00
2024-07-26 15:59:34 +00:00
class EvidenceSerializer(serializers.ModelSerializer):
2024-06-12 07:32:49 +00:00
class Meta:
2024-07-26 15:59:34 +00:00
model = EvidenceJson
2024-06-12 07:32:49 +00:00
fields = ['id', 'title', 'content']
2024-07-01 10:17:23 +00:00
@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)