from rest_framework import viewsets from .models import Employee, WorkTimeLog, Leave, OvertimeReport, Report from .serializers import ( EmployeeSerializer, WorkTimeLogSerializer, LeaveSerializer, OvertimeReportSerializer, ReportSerializer ) from django.http import FileResponse from django.shortcuts import get_object_or_404 from rest_framework import viewsets from rest_framework.filters import OrderingFilter from django_filters.rest_framework import DjangoFilterBackend class WorkTimeLogViewSet(viewsets.ModelViewSet): queryset = WorkTimeLog.objects.all() serializer_class = WorkTimeLogSerializer filter_backends = [DjangoFilterBackend, OrderingFilter] filterset_fields = { 'employee__id': ['exact'], 'date': ['gte', 'lte'], } ordering_fields = ['date'] class LeaveViewSet(viewsets.ModelViewSet): queryset = Leave.objects.all() serializer_class = LeaveSerializer filter_backends = [DjangoFilterBackend, OrderingFilter] filterset_fields = { 'employee__id': ['exact'], 'start_date': ['gte', 'lte'], 'end_date': ['gte', 'lte'], } ordering_fields = ['start_date', 'end_date'] class OvertimeReportViewSet(viewsets.ModelViewSet): queryset = OvertimeReport.objects.all() serializer_class = OvertimeReportSerializer filter_backends = [DjangoFilterBackend, OrderingFilter] filterset_fields = { 'employee__id': ['exact'], 'date': ['gte', 'lte'], } ordering_fields = ['date'] def generate_employee_report(request, report_id): """ Генерация отчета с фильтрацией по диапазону дат. """ report = get_object_or_404(Report, id=report_id) pdf_buffer = report.generate_pdf() return FileResponse(pdf_buffer, as_attachment=True, filename=f"отчет_{report.employee.last_name}.pdf") class EmployeeViewSet(viewsets.ModelViewSet): queryset = Employee.objects.all() serializer_class = EmployeeSerializer # ViewSet для модели WorkTimeLog class WorkTimeLogViewSet(viewsets.ModelViewSet): queryset = WorkTimeLog.objects.all() serializer_class = WorkTimeLogSerializer # ViewSet для модели Leave class LeaveViewSet(viewsets.ModelViewSet): queryset = Leave.objects.all() serializer_class = LeaveSerializer # ViewSet для модели OvertimeReport class OvertimeReportViewSet(viewsets.ModelViewSet): queryset = OvertimeReport.objects.all() serializer_class = OvertimeReportSerializer # ViewSet для модели Report class ReportViewSet(viewsets.ModelViewSet): queryset = Report.objects.all() serializer_class = ReportSerializer