django-orchestra-test/orchestra/contrib/issues/api.py

45 lines
1.4 KiB
Python
Raw Permalink Normal View History

from rest_framework import viewsets, mixins
2015-04-23 14:34:04 +00:00
from rest_framework.decorators import detail_route
from rest_framework.response import Response
2015-04-05 18:02:36 +00:00
from orchestra.api import router, LogApiMixin
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()
serializer_class = TicketSerializer
2015-04-23 14:34:04 +00:00
@detail_route()
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'})
2015-04-23 14:34:04 +00:00
@detail_route()
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'})
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)
2015-04-05 18:02:36 +00:00
class QueueViewSet(LogApiMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
2015-04-23 14:34:04 +00:00
queryset = Queue.objects.all()
serializer_class = QueueSerializer
router.register(r'tickets', TicketViewSet)
router.register(r'ticket-queues', QueueViewSet)