1
0

ticket.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. from rest_framework import generics, permissions, status
  2. from rest_framework.views import APIView
  3. from rest_framework.response import Response
  4. from tickets.models import Ticket
  5. from tickets.api.serializers import TicketDetailSerializer, TicketSerializer, TicketStatusSerializer
  6. from tickets.api.permissions import UserTicketAccessPermission, UserTicketStatusAccessPermission
  7. class TicketDetailAPIView(generics.RetrieveUpdateDestroyAPIView):
  8. queryset = Ticket.objects.all()
  9. serializer_class = TicketDetailSerializer
  10. permission_classes = [permissions.IsAuthenticated & UserTicketAccessPermission]
  11. def destroy(self, request, *args, **kwargs):
  12. instance = self.get_object()
  13. instance.archive()
  14. return Response(status=status.HTTP_204_NO_CONTENT)
  15. class TicketCreateAPIView(generics.CreateAPIView):
  16. queryset = Ticket.objects.all()
  17. serializer_class = TicketSerializer
  18. permission_classes = [permissions.IsAuthenticated & UserTicketAccessPermission]
  19. def perform_create(self, serializer):
  20. if serializer.is_valid():
  21. serializer.save(created_by=self.request.user)
  22. class TicketStatusAPIView(generics.RetrieveUpdateAPIView):
  23. queryset = Ticket.objects.all()
  24. serializer_class = TicketStatusSerializer
  25. permission_classes = [permissions.IsAuthenticated & UserTicketStatusAccessPermission]