2014-05-08 18:47:16 +00:00
|
|
|
from rest_framework import viewsets, mixins
|
2015-04-23 14:34:04 +00:00
|
|
|
from rest_framework.decorators import detail_route
|
2014-05-08 18:47:16 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2015-04-05 18:02:36 +00:00
|
|
|
from orchestra.api import router, LogApiMixin
|
2014-05-08 18:47:16 +00:00
|
|
|
|
|
|
|
from .models import Ticket, Queue
|
|
|
|
from .serializers import TicketSerializer, QueueSerializer
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-05 18:02:36 +00:00
|
|
|
class TicketViewSet(LogApiMixin, viewsets.ModelViewSet):
|
2015-04-23 14:34:04 +00:00
|
|
|
queryset = Ticket.objects.all()
|
2014-05-08 18:47:16 +00:00
|
|
|
serializer_class = TicketSerializer
|
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
@detail_route()
|
2014-05-08 18:47:16 +00:00
|
|
|
def mark_as_read(self, request, pk=None):
|
|
|
|
ticket = self.get_object()
|
2014-05-13 13:46:40 +00:00
|
|
|
ticket.mark_as_read_by(request.user)
|
|
|
|
return Response({'status': 'Ticket marked as read'})
|
2014-05-08 18:47:16 +00:00
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
@detail_route()
|
2014-05-08 18:47:16 +00:00
|
|
|
def mark_as_unread(self, request, pk=None):
|
|
|
|
ticket = self.get_object()
|
2014-05-13 13:46:40 +00:00
|
|
|
ticket.mark_as_unread_by(request.user)
|
|
|
|
return Response({'status': 'Ticket marked as unread'})
|
2014-05-08 18:47:16 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super(TicketViewSet, self).get_queryset()
|
2014-05-13 13:46:40 +00:00
|
|
|
qs = qs.select_related('creator', 'queue')
|
|
|
|
qs = qs.prefetch_related('messages__author')
|
2014-09-30 16:39:47 +00:00
|
|
|
return qs.filter(creator=self.request.user)
|
2014-05-08 18:47:16 +00:00
|
|
|
|
|
|
|
|
2015-04-05 18:02:36 +00:00
|
|
|
class QueueViewSet(LogApiMixin,
|
|
|
|
mixins.ListModelMixin,
|
2014-05-08 18:47:16 +00:00
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
viewsets.GenericViewSet):
|
2015-04-23 14:34:04 +00:00
|
|
|
queryset = Queue.objects.all()
|
2014-05-08 18:47:16 +00:00
|
|
|
serializer_class = QueueSerializer
|
|
|
|
|
|
|
|
|
|
|
|
router.register(r'tickets', TicketViewSet)
|
|
|
|
router.register(r'ticket-queues', QueueViewSet)
|