
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
from .. import models , serializers
from django.db import models as dj_models

class GtfsArchivoViewSet(viewsets.ModelViewSet):
    queryset = models.GtfsArchivo.objects.all()
    serializer_class = serializers.GtfsArchivoSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['id_gtfs', 'id_red', 'archivo','valida_from','created','usuario','vigente','status']
    

    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_gtfs', None):
            max_id = models.GtfsArchivo.objects.aggregate(dj_models.Max('id_gtfs'))['id_gtfs__max']
            new_id = max_id + 1 if max_id is not None else 1
            request.data['id_gtfs'] = new_id
        
        return super().create(request, *args, **kwargs)