69 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Employee API viewset composition root."""
from rest_framework import filters, viewsets
from rest_framework.permissions import IsAuthenticated
from ...permissions import AdminOrActionOrModelPerms, IsSelfOrStaff
from ..serializers import EmployeeListSerializer, EmployeeSerializer
from .employee_mixins import (
EmployeeLinkedTasksMixin,
EmployeePersistenceMixin,
EmployeeProfileMixin,
EmployeeQueryMixin,
EmployeeStatisticsMixin,
)
class EmployeeViewSet(
EmployeeQueryMixin,
EmployeeLinkedTasksMixin,
EmployeePersistenceMixin,
EmployeeStatisticsMixin,
EmployeeProfileMixin,
viewsets.ModelViewSet,
):
"""
/api/v1/employees/
CRUD для сотрудников + профиль текущего пользователя.
Доступ:
- GET list/retrieve — только аутентифицированные пользователи.
- POST (create) — только staff/superuser.
- PATCH/PUT/DELETE {id} — staff/superuser ИЛИ пользователи
с модельными правами.
- GET/PATCH /employees/me/ — только аутентифицированные;
PATCH правит профиль текущего пользователя.
Поиск: last_name, first_name, patronymic, email, phone_number
LDAP синхронизация происходит через Django сигналы (signals_ldap.py).
"""
serializer_class = EmployeeSerializer
filter_backends = [filters.SearchFilter, filters.OrderingFilter]
search_fields = [
"last_name",
"first_name",
"patronymic",
"email",
"phone_number",
]
ordering_fields = ["last_name", "first_name", "created_at", "id"]
ordering = ["last_name", "first_name"]
def get_permissions(self):
if self.action in {"create", "destroy"}:
return [IsAuthenticated(), AdminOrActionOrModelPerms()]
if self.action in {"update", "partial_update"}:
return [
IsAuthenticated(),
(IsSelfOrStaff | AdminOrActionOrModelPerms)(),
]
return [IsAuthenticated()]
def get_serializer_class(self):
if self.action == "list":
return EmployeeListSerializer
return EmployeeSerializer