from ces_fall_rest_api.models.course import Course
from ces_fall_rest_api.serializers.course_serializer import CourseSerializer
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated


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

    Args:
        viewsets (_type_): viewsets class
    """
    queryset = Course.objects.all()
    serializer_class = CourseSerializer
    permission_classes = [IsAuthenticated]

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

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