mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-22 11:58:29 +00:00
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
This commit is contained in:
139
apps/tickets/serializers/ticket/ticket.py
Normal file
139
apps/tickets/serializers/ticket/ticket.py
Normal file
@@ -0,0 +1,139 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
from common.fields.serializer import ReadableHiddenField
|
||||
from orgs.utils import get_org_by_id
|
||||
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
|
||||
from users.models import User
|
||||
from tickets import const
|
||||
from tickets.models import Ticket
|
||||
|
||||
__all__ = [
|
||||
'TicketSerializer', 'TicketDisplaySerializer',
|
||||
'TicketApplySerializer', 'TicketApproveSerializer',
|
||||
'TicketRejectSerializer', 'TicketCloseSerializer',
|
||||
]
|
||||
|
||||
|
||||
class TicketSerializer(OrgResourceModelSerializerMixin):
|
||||
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type'))
|
||||
status_display = serializers.ReadOnlyField(source='get_status_display', label=_('Status'))
|
||||
action_display = serializers.ReadOnlyField(source='get_action_display', label=_('Action'))
|
||||
|
||||
class Meta:
|
||||
model = Ticket
|
||||
fields = [
|
||||
'id', 'title', 'type', 'type_display',
|
||||
'meta', 'action', 'action_display', 'status', 'status_display',
|
||||
'applicant', 'applicant_display', 'processor', 'processor_display',
|
||||
'assignees', 'assignees_display',
|
||||
'date_created', 'date_updated',
|
||||
'org_id', 'org_name',
|
||||
'body'
|
||||
]
|
||||
|
||||
|
||||
class TicketDisplaySerializer(TicketSerializer):
|
||||
|
||||
class Meta(TicketSerializer.Meta):
|
||||
read_only_fields = TicketSerializer.Meta.fields
|
||||
|
||||
|
||||
class TicketActionSerializer(TicketSerializer):
|
||||
action = ReadableHiddenField(default=const.TicketActionChoices.apply.value)
|
||||
|
||||
class Meta(TicketSerializer.Meta):
|
||||
required_fields = ['action']
|
||||
read_only_fields = list(set(TicketDisplaySerializer.Meta.fields) - set(required_fields))
|
||||
|
||||
|
||||
class TicketApplySerializer(TicketActionSerializer):
|
||||
applicant = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
||||
org_id = serializers.CharField(
|
||||
max_length=36, allow_blank=True, required=True, label=_("Organization")
|
||||
)
|
||||
|
||||
class Meta(TicketActionSerializer.Meta):
|
||||
required_fields = TicketActionSerializer.Meta.required_fields + [
|
||||
'id', 'title', 'type', 'applicant', 'meta', 'assignees', 'org_id'
|
||||
]
|
||||
read_only_fields = list(set(TicketDisplaySerializer.Meta.fields) - set(required_fields))
|
||||
extra_kwargs = {
|
||||
'type': {'required': True}
|
||||
}
|
||||
|
||||
def validate_type(self, tp):
|
||||
request_type = self.context['request'].query_params.get('type')
|
||||
if tp != request_type:
|
||||
error = _(
|
||||
'The `type` in the submission data (`{}`) is different from the type '
|
||||
'in the request url (`{}`)'.format(tp, request_type)
|
||||
)
|
||||
raise serializers.ValidationError(error)
|
||||
return tp
|
||||
|
||||
@staticmethod
|
||||
def validate_org_id(org_id):
|
||||
org = get_org_by_id(org_id)
|
||||
if not org:
|
||||
error = _('The organization `{}` does not exist'.format(org_id))
|
||||
raise serializers.ValidationError(error)
|
||||
return org_id
|
||||
|
||||
def validate_assignees(self, assignees):
|
||||
org_id = self.initial_data.get('org_id')
|
||||
self.validate_org_id(org_id)
|
||||
org = get_org_by_id(org_id)
|
||||
admins = User.get_super_and_org_admins(org)
|
||||
valid_assignees = list(set(assignees) & set(admins))
|
||||
if not valid_assignees:
|
||||
error = _('None of the assignees belong to Organization `{}` admins'.format(org.name))
|
||||
raise serializers.ValidationError(error)
|
||||
return valid_assignees
|
||||
|
||||
@staticmethod
|
||||
def validate_action(action):
|
||||
return const.TicketActionChoices.apply.value
|
||||
|
||||
|
||||
class TicketProcessSerializer(TicketActionSerializer):
|
||||
processor = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
||||
|
||||
class Meta(TicketActionSerializer.Meta):
|
||||
required_fields = TicketActionSerializer.Meta.required_fields + ['processor']
|
||||
read_only_fields = list(set(TicketDisplaySerializer.Meta.fields) - set(required_fields))
|
||||
|
||||
|
||||
class TicketApproveSerializer(TicketProcessSerializer):
|
||||
|
||||
class Meta(TicketProcessSerializer.Meta):
|
||||
required_fields = TicketProcessSerializer.Meta.required_fields + ['meta']
|
||||
read_only_fields = list(set(TicketDisplaySerializer.Meta.fields) - set(required_fields))
|
||||
extra_kwargs = {
|
||||
'meta': {'read_only': True}
|
||||
}
|
||||
|
||||
def validate_meta(self, meta):
|
||||
meta.update(self.instance.meta)
|
||||
return meta
|
||||
|
||||
@staticmethod
|
||||
def validate_action(action):
|
||||
return const.TicketActionChoices.approve.value
|
||||
|
||||
|
||||
class TicketRejectSerializer(TicketProcessSerializer):
|
||||
|
||||
@staticmethod
|
||||
def validate_action(action):
|
||||
return const.TicketActionChoices.reject.value
|
||||
|
||||
|
||||
class TicketCloseSerializer(TicketProcessSerializer):
|
||||
|
||||
@staticmethod
|
||||
def validate_action(action):
|
||||
return const.TicketActionChoices.close.value
|
||||
|
||||
|
Reference in New Issue
Block a user