2023-03-29 18:35:23 -03:00
|
|
|
from json import JSONDecodeError
|
|
|
|
from django.http import JsonResponse
|
|
|
|
from .serializers import ContactSerializer
|
|
|
|
from rest_framework.parsers import JSONParser
|
|
|
|
from rest_framework import views, status
|
|
|
|
from rest_framework.response import Response
|
2023-03-29 14:06:11 -03:00
|
|
|
|
2023-03-29 18:35:23 -03:00
|
|
|
class ContactAPIView(views.APIView):
|
|
|
|
"""
|
|
|
|
Simple APIView para creación de entradas de contacto.
|
|
|
|
"""
|
|
|
|
serializer_class = ContactSerializer
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
return { 'request': self.request,
|
|
|
|
'format': self.format_kwarg,
|
|
|
|
'view': self }
|
|
|
|
|
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
|
|
kwargs['context'] = self.get_serializer_context()
|
|
|
|
return self.serializer_class(*args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
try:
|
|
|
|
data = JSONParser().parse(request)
|
|
|
|
serializer = ContactSerializer(data=data)
|
|
|
|
if serializer.is_valid(raise_exception=True):
|
|
|
|
serializer.save()
|
|
|
|
return Response(serializer.data)
|
|
|
|
else:
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
except JSONDecodeError:
|
2023-03-29 21:15:04 -03:00
|
|
|
return JsonResponse({"result": "error", "message": "Json decoding error"},
|
2023-03-29 18:35:23 -03:00
|
|
|
status=400)
|