2022-07-22 03:26:41 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from helpdesk.models import FollowUp, FollowUpAttachment, Ticket
|
2023-11-23 21:50:44 +01:00
|
|
|
from helpdesk.serializers import FollowUpAttachmentSerializer, FollowUpSerializer, TicketSerializer, UserSerializer, PublicTicketListingSerializer
|
2022-03-31 17:19:49 +02:00
|
|
|
from rest_framework import viewsets
|
2022-07-22 03:26:41 +02:00
|
|
|
from rest_framework.mixins import CreateModelMixin
|
2023-11-26 13:52:10 +01:00
|
|
|
from rest_framework.permissions import IsAdminUser, IsAuthenticated
|
2022-05-02 17:27:25 +02:00
|
|
|
from rest_framework.viewsets import GenericViewSet
|
2023-11-23 21:50:44 +01:00
|
|
|
from rest_framework.pagination import PageNumberPagination
|
|
|
|
|
2024-03-02 00:28:00 +01:00
|
|
|
from helpdesk import settings as helpdesk_settings
|
|
|
|
|
2023-11-23 21:50:44 +01:00
|
|
|
|
|
|
|
class ConservativePagination(PageNumberPagination):
|
|
|
|
page_size = 25
|
|
|
|
page_size_query_param = 'page_size'
|
|
|
|
|
|
|
|
|
|
|
|
class UserTicketViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
"""
|
|
|
|
A list of all the tickets submitted by the current user
|
|
|
|
|
|
|
|
The view is paginated by default
|
|
|
|
"""
|
|
|
|
serializer_class = PublicTicketListingSerializer
|
|
|
|
pagination_class = ConservativePagination
|
2023-11-26 13:52:10 +01:00
|
|
|
permission_classes = [IsAuthenticated]
|
2023-11-23 21:50:44 +01:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2024-01-16 23:19:32 +01:00
|
|
|
tickets = Ticket.objects.filter(submitter_email=self.request.user.email).order_by('-created')
|
|
|
|
for ticket in tickets:
|
|
|
|
ticket.set_custom_field_values()
|
|
|
|
return tickets
|
|
|
|
|
2022-03-31 17:19:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TicketViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
|
|
A viewset that provides the standard actions to handle Ticket
|
2024-03-02 00:28:00 +01:00
|
|
|
|
|
|
|
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.
|
2022-03-31 17:19:49 +02:00
|
|
|
"""
|
|
|
|
queryset = Ticket.objects.all()
|
|
|
|
serializer_class = TicketSerializer
|
2023-11-23 21:50:44 +01:00
|
|
|
pagination_class = ConservativePagination
|
2022-03-31 17:19:49 +02:00
|
|
|
permission_classes = [IsAdminUser]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
tickets = Ticket.objects.all()
|
2024-03-02 00:28:00 +01:00
|
|
|
|
|
|
|
# filter by status
|
|
|
|
status = self.request.query_params.get('status', None)
|
2024-04-22 18:33:58 +02:00
|
|
|
if status:
|
|
|
|
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 str(choice[0]) == status:
|
|
|
|
number_statuses.append(choice[0])
|
|
|
|
if number_statuses:
|
|
|
|
tickets = tickets.filter(status__in=number_statuses)
|
2024-03-02 00:28:00 +01:00
|
|
|
|
2022-03-31 17:19:49 +02:00
|
|
|
for ticket in tickets:
|
|
|
|
ticket.set_custom_field_values()
|
|
|
|
return tickets
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
ticket = super().get_object()
|
|
|
|
ticket.set_custom_field_values()
|
|
|
|
return ticket
|
2022-05-02 17:27:25 +02:00
|
|
|
|
|
|
|
|
2022-06-24 22:22:08 +02:00
|
|
|
class FollowUpViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = FollowUp.objects.all()
|
|
|
|
serializer_class = FollowUpSerializer
|
2023-11-23 21:50:44 +01:00
|
|
|
pagination_class = ConservativePagination
|
2022-06-24 22:22:08 +02:00
|
|
|
permission_classes = [IsAdminUser]
|
|
|
|
|
2023-11-29 23:03:09 +01:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
serializer.save(user=self.request.user)
|
|
|
|
|
2022-06-24 22:22:08 +02:00
|
|
|
|
2022-06-24 23:45:26 +02:00
|
|
|
class FollowUpAttachmentViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = FollowUpAttachment.objects.all()
|
|
|
|
serializer_class = FollowUpAttachmentSerializer
|
2023-11-23 21:50:44 +01:00
|
|
|
pagination_class = ConservativePagination
|
2022-06-24 23:45:26 +02:00
|
|
|
permission_classes = [IsAdminUser]
|
|
|
|
|
|
|
|
|
2022-05-02 17:27:25 +02:00
|
|
|
class CreateUserView(CreateModelMixin, GenericViewSet):
|
|
|
|
queryset = get_user_model().objects.all()
|
|
|
|
serializer_class = UserSerializer
|
2022-05-04 18:51:02 +02:00
|
|
|
permission_classes = [IsAdminUser]
|