ticket_list.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from django.shortcuts import get_object_or_404
  2. from rest_framework import generics, permissions
  3. from tickets.models import Ticket, TicketList
  4. from tickets.api.serializers import TicketListSerializer, TicketSerializer
  5. from tickets.api.permissions import UserCanReadTicketListPermission
  6. class TicketListListAPIView(generics.ListAPIView):
  7. permission_classes = [permissions.IsAuthenticated]
  8. serializer_class = TicketListSerializer
  9. def get_queryset(self):
  10. user = self.request.user
  11. user_groups_ids = user.groups.all().values_list("pk", flat=True)
  12. ticket_lists = TicketList.objects.select_related("group").order_by("group__name", "name")
  13. if not user.is_superuser:
  14. if user_groups_ids:
  15. ticket_lists = ticket_lists.filter(group__id__in=user_groups_ids)
  16. else:
  17. raise NotFound("You do not yet belong to any groups. Ask your administrator to add you to one.")
  18. return ticket_lists
  19. class TicketListDetailAPIView(generics.ListAPIView):
  20. permission_classes = [permissions.IsAuthenticated & UserCanReadTicketListPermission]
  21. serializer_class = TicketSerializer
  22. def get_queryset(self):
  23. tickets = Ticket.objects.all()
  24. if self.kwargs.get("my"):
  25. tickets = tickets.filter(assigned_to=self.request.user)
  26. else:
  27. tickets = tickets.filter(ticket_list=self.get_object())
  28. return tickets
  29. # FIXME: По какой-то причине появляется ошибка после загрузки страницы redoc
  30. def get_object(self):
  31. obj = get_object_or_404(TicketList.objects.filter(pk=self.kwargs['pk']))
  32. self.check_object_permissions(self.request, obj)
  33. return obj