jumpserver/apps/tickets/api/ticket.py

126 lines
4.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.exceptions import MethodNotAllowed
from common.const.http import POST, PUT, PATCH
from common.mixins.api import CommonApiMixin
from orgs.utils import tmp_to_root_org
2021-01-01 23:47:43 +00:00
2022-03-16 06:43:20 +00:00
from rbac.permissions import RBACPermission
2021-01-01 23:47:43 +00:00
from tickets import serializers
from tickets import filters
from tickets.permissions.ticket import IsAssignee, IsApplicant
from tickets.models import (
2022-08-17 07:44:59 +00:00
Ticket, ApplyAssetTicket, ApplyLoginTicket,
ApplyLoginAssetTicket, ApplyCommandTicket
)
__all__ = [
2022-08-16 08:05:08 +00:00
'TicketViewSet', 'ApplyAssetTicketViewSet',
'ApplyLoginTicketViewSet', 'ApplyLoginAssetTicketViewSet',
'ApplyCommandTicketViewSet'
]
class TicketViewSet(CommonApiMixin, viewsets.ModelViewSet):
serializer_class = serializers.TicketDisplaySerializer
serializer_classes = {
'list': serializers.TicketListSerializer,
'open': serializers.TicketApplySerializer,
'approve': serializers.TicketApproveSerializer
}
model = Ticket
perm_model = Ticket
filterset_class = filters.TicketFilter
search_fields = [
'title', 'type', 'status'
]
2022-01-13 03:42:53 +00:00
ordering_fields = (
'title', 'status', 'state',
'action_display', 'date_created', 'serial_num',
2022-01-13 03:42:53 +00:00
)
ordering = ('-date_created',)
2022-03-16 06:43:20 +00:00
rbac_perms = {
2022-03-16 08:21:00 +00:00
'open': 'tickets.view_ticket',
2022-03-16 06:43:20 +00:00
}
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):
with tmp_to_root_org():
queryset = self.model.get_user_related_tickets(self.request.user)
return queryset
def perform_create(self, serializer):
instance = serializer.save()
instance.applicant = self.request.user
instance.save(update_fields=['applicant'])
instance.open()
2022-03-16 06:43:20 +00:00
@action(detail=False, methods=[POST], permission_classes=[RBACPermission, ])
def open(self, request, *args, **kwargs):
with tmp_to_root_org():
return super().create(request, *args, **kwargs)
@action(detail=True, methods=[PUT, PATCH], permission_classes=[IsAssignee, ])
def approve(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
with tmp_to_root_org():
serializer.is_valid(raise_exception=True)
instance = serializer.save()
instance.approve(processor=request.user)
return Response('ok')
2021-08-16 10:55:33 +00:00
@action(detail=True, methods=[PUT], permission_classes=[IsAssignee, ])
def reject(self, request, *args, **kwargs):
instance = self.get_object()
instance.reject(processor=request.user)
return Response('ok')
@action(detail=True, methods=[PUT], permission_classes=[IsApplicant, ])
def close(self, request, *args, **kwargs):
instance = self.get_object()
instance.close()
return Response('ok')
class ApplyAssetTicketViewSet(TicketViewSet):
serializer_class = serializers.ApplyAssetDisplaySerializer
serializer_classes = {
'open': serializers.ApplyAssetSerializer,
'approve': serializers.ApproveAssetSerializer
}
model = ApplyAssetTicket
filterset_class = filters.ApplyAssetTicketFilter
class ApplyLoginTicketViewSet(TicketViewSet):
serializer_class = serializers.LoginConfirmSerializer
model = ApplyLoginTicket
filterset_class = filters.ApplyLoginTicketFilter
class ApplyLoginAssetTicketViewSet(TicketViewSet):
serializer_class = serializers.LoginAssetConfirmSerializer
model = ApplyLoginAssetTicket
filterset_class = filters.ApplyLoginAssetTicketFilter
class ApplyCommandTicketViewSet(TicketViewSet):
serializer_class = serializers.ApplyCommandConfirmSerializer
model = ApplyCommandTicket
filterset_class = filters.ApplyCommandTicketFilter