2020-12-29 16:19:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from rest_framework import viewsets
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.exceptions import MethodNotAllowed
|
2021-01-13 09:49:03 +00:00
|
|
|
from rest_framework.response import Response
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2021-01-01 23:47:43 +00:00
|
|
|
from common.const.http import POST, PUT
|
2020-12-29 16:19:59 +00:00
|
|
|
from common.mixins.api import CommonApiMixin
|
|
|
|
from common.permissions import IsValidUser, IsOrgAdmin
|
2021-01-01 23:47:43 +00:00
|
|
|
|
|
|
|
from tickets import serializers
|
2020-12-29 16:19:59 +00:00
|
|
|
from tickets.models import Ticket
|
2021-01-18 10:03:40 +00:00
|
|
|
from tickets.permissions.ticket import IsAssignee, IsAssigneeOrApplicant, NotClosed
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['TicketViewSet']
|
|
|
|
|
|
|
|
|
2021-01-01 23:25:23 +00:00
|
|
|
class TicketViewSet(CommonApiMixin, viewsets.ModelViewSet):
|
2020-12-29 16:19:59 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
2021-01-12 10:06:42 +00:00
|
|
|
serializer_class = serializers.TicketDisplaySerializer
|
2020-12-29 16:19:59 +00:00
|
|
|
serializer_classes = {
|
2020-12-30 10:14:06 +00:00
|
|
|
'open': serializers.TicketApplySerializer,
|
2020-12-29 16:19:59 +00:00
|
|
|
'approve': serializers.TicketApproveSerializer,
|
|
|
|
}
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = [
|
2020-12-29 16:19:59 +00:00
|
|
|
'id', 'title', 'type', 'action', 'status', 'applicant', 'applicant_display', 'processor',
|
|
|
|
'processor_display', 'assignees__id'
|
|
|
|
]
|
|
|
|
search_fields = [
|
|
|
|
'title', 'action', 'type', 'status', 'applicant_display', 'processor_display'
|
|
|
|
]
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
raise MethodNotAllowed(self.action)
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
raise MethodNotAllowed(self.action)
|
|
|
|
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
raise MethodNotAllowed(self.action)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = Ticket.get_user_related_tickets(self.request.user)
|
|
|
|
return queryset
|
|
|
|
|
2021-01-13 09:49:03 +00:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
instance = serializer.save()
|
|
|
|
instance.open(applicant=self.request.user)
|
|
|
|
|
2020-12-29 16:19:59 +00:00
|
|
|
@action(detail=False, methods=[POST])
|
2020-12-30 10:14:06 +00:00
|
|
|
def open(self, request, *args, **kwargs):
|
2020-12-29 16:19:59 +00:00
|
|
|
return super().create(request, *args, **kwargs)
|
|
|
|
|
|
|
|
@action(detail=True, methods=[PUT], permission_classes=[IsOrgAdmin, IsAssignee, NotClosed])
|
|
|
|
def approve(self, request, *args, **kwargs):
|
2021-01-13 09:49:03 +00:00
|
|
|
response = super().update(request, *args, **kwargs)
|
|
|
|
instance = self.get_object()
|
|
|
|
instance.approve(processor=self.request.user)
|
|
|
|
return response
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@action(detail=True, methods=[PUT], permission_classes=[IsOrgAdmin, IsAssignee, NotClosed])
|
|
|
|
def reject(self, request, *args, **kwargs):
|
2021-01-13 09:49:03 +00:00
|
|
|
instance = self.get_object()
|
|
|
|
serializer = self.get_serializer(instance)
|
|
|
|
instance.reject(processor=request.user)
|
|
|
|
return Response(serializer.data)
|
2020-12-29 16:19:59 +00:00
|
|
|
|
2021-01-18 10:03:40 +00:00
|
|
|
@action(detail=True, methods=[PUT], permission_classes=[IsAssigneeOrApplicant, NotClosed])
|
2020-12-29 16:19:59 +00:00
|
|
|
def close(self, request, *args, **kwargs):
|
2021-01-13 09:49:03 +00:00
|
|
|
instance = self.get_object()
|
|
|
|
serializer = self.get_serializer(instance)
|
|
|
|
instance.close(processor=request.user)
|
|
|
|
return Response(serializer.data)
|