api.py 1.37 KB
from rest_framework import viewsets
from rest_framework import status
from rest_framework.response import Response
# from rest_framework.decorators import action

from .models import Edicto
from .serializer import EdictoSerializer
from .utils import contador


class EdictoView(viewsets.ModelViewSet):
    queryset = Edicto.objects.all().order_by('id')
    serializer_class = EdictoSerializer

    def create(self, request, *args, **kwargs):
        edicto = request.data.get('edicto')
        cantidad_palabras = contador(edicto)

        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save(cantidad_palabras=cantidad_palabras)

        return Response(serializer.data, status=status.HTTP_201_CREATED)


''' class EdictoView(viewsets.ModelViewSet):
    queryset = Edicto.objects.all()
    serializer_class = EdictoSerializer

    @action(detail=True, methods='post')
    def post(self, request):

        edicto = request.data.get('edicto')
        cantidad_palabras = contador(edicto)
        serializer = self.get_serializer(data=request.data)
        if not serializer.is_valid(raise_exception=True):
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        serializer.save(cantidad_palabras=cantidad_palabras)
        return Response(serializer.data, status=status.HTTP_201_CREATED)
 '''