Files
jumpserver/apps/tickets/api/ticket/mixin.py
Jiangjie.Bai 3b056ff953 reactor&feat: 重构工单模块 & 支持申请应用工单 (#5352)
* reactor: 修改工单Model,添加工单迁移文件

* reactor: 修改工单Model,添加工单迁移文件

* reactor: 重构工单模块

* reactor: 重构工单模块2

* reactor: 重构工单模块3

* reactor: 重构工单模块4

* reactor: 重构工单模块5

* reactor: 重构工单模块6

* reactor: 重构工单模块7

* reactor: 重构工单模块8

* reactor: 重构工单模块9

* reactor: 重构工单模块10

* reactor: 重构工单模块11

* reactor: 重构工单模块12

* reactor: 重构工单模块13

* reactor: 重构工单模块14

* reactor: 重构工单模块15

* reactor: 重构工单模块16

* reactor: 重构工单模块17

* reactor: 重构工单模块18

* reactor: 重构工单模块19

* reactor: 重构工单模块20

* reactor: 重构工单模块21

* reactor: 重构工单模块22

* reactor: 重构工单模块23

* reactor: 重构工单模块24

* reactor: 重构工单模块25

* reactor: 重构工单模块26

* reactor: 重构工单模块27

* reactor: 重构工单模块28

* reactor: 重构工单模块29

* reactor: 重构工单模块30

* reactor: 重构工单模块31

* reactor: 重构工单模块32

* reactor: 重构工单模块33

* reactor: 重构工单模块34

* reactor: 重构工单模块35

* reactor: 重构工单模块36

* reactor: 重构工单模块37

* reactor: 重构工单模块38

* reactor: 重构工单模块39
2020-12-30 00:19:59 +08:00

67 lines
2.5 KiB
Python

from common.exceptions import JMSException
from tickets import const, serializers
__all__ = ['TicketMetaSerializerViewMixin']
class TicketMetaSerializerViewMixin:
apply_asset_meta_serializer_classes = {
'apply': serializers.TicketMetaApplyAssetApplySerializer,
'approve': serializers.TicketMetaApplyAssetApproveSerializer,
}
apply_application_meta_serializer_classes = {
'apply': serializers.TicketMetaApplyApplicationApplySerializer,
'approve': serializers.TicketMetaApplyApplicationApproveSerializer,
}
login_confirm_meta_serializer_classes = {
'apply': serializers.TicketMetaLoginConfirmApplySerializer,
}
meta_serializer_classes = {
const.TicketTypeChoices.apply_asset.value: apply_asset_meta_serializer_classes,
const.TicketTypeChoices.apply_application.value: apply_application_meta_serializer_classes,
const.TicketTypeChoices.login_confirm.value: login_confirm_meta_serializer_classes,
}
def get_serializer_meta_field_class(self):
tp = self.request.query_params.get('type')
if not tp:
return None
tp_choices = const.TicketTypeChoices.types()
if tp not in tp_choices:
raise JMSException(
'Invalid query parameter `type`, select from the following options: {}'
''.format(tp_choices)
)
meta_class = self.meta_serializer_classes.get(tp, {}).get(self.action)
return meta_class
def get_serializer_meta_field(self):
if self.action not in ['apply', 'approve']:
return None
meta_class = self.get_serializer_meta_field_class()
if not meta_class:
return None
return meta_class(required=True)
def reset_view_metadata_action(self):
if self.action not in ['metadata']:
return
view_action = self.request.query_params.get('action')
if not view_action:
raise JMSException('The `metadata` methods must carry parameter `action`')
setattr(self, 'action', view_action)
def get_serializer_class(self):
self.reset_view_metadata_action()
serializer_class = super().get_serializer_class()
if getattr(self, 'swagger_fake_view', False):
return serializer_class
meta_field = self.get_serializer_meta_field()
if not meta_field:
return serializer_class
serializer_class = type(
meta_field.__class__.__name__, (serializer_class,), {'meta': meta_field}
)
return serializer_class