from django.shortcuts import get_object_or_404 from rest_framework import generics, permissions from tickets.models import Ticket, TicketList from tickets.api.serializers import TicketListSerializer, TicketSerializer from tickets.api.permissions import UserCanReadTicketListPermission class TicketListListAPIView(generics.ListAPIView): permission_classes = [permissions.IsAuthenticated] serializer_class = TicketListSerializer def get_queryset(self): user = self.request.user user_groups_ids = user.groups.all().values_list("pk", flat=True) ticket_lists = TicketList.objects.select_related("group").order_by("group__name", "name") if not user.is_superuser: if user_groups_ids: ticket_lists = ticket_lists.filter(group__id__in=user_groups_ids) else: raise NotFound("You do not yet belong to any groups. Ask your administrator to add you to one.") return ticket_lists class TicketListDetailAPIView(generics.ListAPIView): permission_classes = [permissions.IsAuthenticated & UserCanReadTicketListPermission] serializer_class = TicketSerializer def get_queryset(self): tickets = Ticket.objects.all() if self.kwargs.get("my"): tickets = tickets.filter(assigned_to=self.request.user) else: tickets = tickets.filter(ticket_list=self.get_object()) return tickets # FIXME: По какой-то причине появляется ошибка после загрузки страницы redoc def get_object(self): obj = get_object_or_404(TicketList.objects.filter(pk=self.kwargs['pk'])) self.check_object_permissions(self.request, obj) return obj