from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

from ces_fall_rest_api.models.semester import Semester
from ces_fall_rest_api.serializers.semester_serializer import SemesterSerializer


class SemesterViewSet(viewsets.ModelViewSet):
    """A simple viewset to get the all the Semester objects
    This class has a token authentication.

    Args:
        viewsets (_type_): viewset class
    """
    queryset = Semester.objects.all()
    serializer_class = SemesterSerializer
    permission_classes = [IsAuthenticated]

    def get_object(self):
        lookup_args = ['year', 'month']

        if all(arg in self.kwargs for arg in lookup_args):
            return Semester.objects.get(year=self.kwargs['year'], month=self.kwargs['month'])
        else:
            return super().get_object()
