from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

from .models import WorkRecord
from .serializers import WorkRecordSerializer


class WorkRecordViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = WorkRecordSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        queryset = WorkRecord.objects.select_related(
            'delivery_trip',
            'delivery_trip__delivery_plan',
            'spreading_session',
            'levee_work_session',
        )
        year = self.request.query_params.get('year')
        if year:
            queryset = queryset.filter(year=year)
        return queryset
