jumpserver/apps/tickets/api/ticket.py

120 lines
3.9 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 orgs.utils import tmp_to_root_org
2022-03-16 06:43:20 +00:00
from rbac.permissions import RBACPermission
from common.mixins.api import CommonApiMixin
from common.const.http import POST, PUT, PATCH
from tickets import filters
from tickets import serializers
from tickets.models import (
2022-08-17 07:44:59 +00:00
Ticket, ApplyAssetTicket, ApplyLoginTicket,
ApplyLoginAssetTicket, ApplyCommandTicket
)
from tickets.permissions.ticket import IsAssignee, IsApplicant
__all__ = [
2022-08-16 08:05:08 +00:00
'TicketViewSet', 'ApplyAssetTicketViewSet',
'ApplyLoginTicketViewSet', 'ApplyLoginAssetTicketViewSet',
'ApplyCommandTicketViewSet'
]
class TicketViewSet(CommonApiMixin, viewsets.ModelViewSet):
serializer_class = serializers.TicketSerializer
serializer_classes = {
'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.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):
model = ApplyAssetTicket
filterset_class = filters.ApplyAssetTicketFilter
serializer_class = serializers.ApplyAssetSerializer
serializer_classes = {
'open': serializers.ApplyAssetSerializer,
'approve': serializers.ApproveAssetSerializer
}
class ApplyLoginTicketViewSet(TicketViewSet):
model = ApplyLoginTicket
filterset_class = filters.ApplyLoginTicketFilter
2022-12-06 10:26:07 +00:00
serializer_class = serializers.LoginReviewSerializer
class ApplyLoginAssetTicketViewSet(TicketViewSet):
model = ApplyLoginAssetTicket
filterset_class = filters.ApplyLoginAssetTicketFilter
2022-12-06 10:26:07 +00:00
serializer_class = serializers.LoginAssetReviewSerializer
class ApplyCommandTicketViewSet(TicketViewSet):
model = ApplyCommandTicket
filterset_class = filters.ApplyCommandTicketFilter
2022-12-06 10:26:07 +00:00
serializer_class = serializers.ApplyCommandReviewSerializer