from rest_framework import viewsets
from api import models, serializers
from django.db.models import Max

class AplicacionViewSet(viewsets.ModelViewSet):
    queryset = models.Aplicacion.objects.all()
    serializer_class = serializers.AplicacionSerializer
    ordering_fields = '__all__'

    def create(self, request, *args, **kwargs):
        # se indica que si no se indico el id, entonces sea el maximo + 1
        if not request.data.get('id_aplicacion', None):
            max_id = models.Aplicacion.objects.aggregate(Max('id_aplicacion'))['id_aplicacion__max']
            new_id = max_id + 1 if max_id is not None else 1
            request.data['id_aplicacion'] = new_id
        
        return super().create(request, *args, **kwargs)