From 128a465d44fa35cd07fb5a7da3cb5f85e44aa3ca Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Sat, 2 Mar 2024 00:28:00 +0100 Subject: [PATCH] Enabling filtering of tickets by status in API --- helpdesk/views/api.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/helpdesk/views/api.py b/helpdesk/views/api.py index fce0e816..657832e7 100644 --- a/helpdesk/views/api.py +++ b/helpdesk/views/api.py @@ -7,6 +7,8 @@ from rest_framework.permissions import IsAdminUser, IsAuthenticated from rest_framework.viewsets import GenericViewSet from rest_framework.pagination import PageNumberPagination +from helpdesk import settings as helpdesk_settings + class ConservativePagination(PageNumberPagination): page_size = 25 @@ -34,6 +36,10 @@ class UserTicketViewSet(viewsets.ReadOnlyModelViewSet): class TicketViewSet(viewsets.ModelViewSet): """ A viewset that provides the standard actions to handle Ticket + + You can filter the tickets by status using the `status` query parameter. For example: + + `/api/tickets/?status=Open,Resolved` will return all the tickets that are Open or Resolved. """ queryset = Ticket.objects.all() serializer_class = TicketSerializer @@ -42,6 +48,19 @@ class TicketViewSet(viewsets.ModelViewSet): def get_queryset(self): tickets = Ticket.objects.all() + + # filter by status + status = self.request.query_params.get('status', None) + statuses = status.split(',') if status else [] + status_choices = helpdesk_settings.TICKET_STATUS_CHOICES + number_statuses = [] + for status in statuses: + for choice in status_choices: + if choice[1] == status: + number_statuses.append(choice[0]) + if number_statuses: + tickets = tickets.filter(status__in=number_statuses) + for ticket in tickets: ticket.set_custom_field_values() return tickets