perf: ticket serializer (#9075)

Co-authored-by: feng <1304903146@qq.com>
pull/9094/head
fit2bot 2022-11-17 13:52:10 +08:00 committed by GitHub
parent 8a1a7d9e13
commit 73c2155864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 19 deletions

View File

@ -22,18 +22,14 @@ class ApplyAssetSerializer(BaseApplyAssetSerializer, TicketApplySerializer):
class Meta(TicketApplySerializer.Meta):
model = ApplyAssetTicket
fields_mini = ['id', 'title']
writeable_fields = [
'id', 'title', 'apply_nodes', 'apply_assets',
'apply_accounts', 'apply_actions', 'org_id', 'comment',
'apply_date_start', 'apply_date_expired'
'apply_nodes', 'apply_assets', 'apply_accounts',
'apply_actions', 'apply_date_start', 'apply_date_expired'
]
fields = TicketApplySerializer.Meta.fields + writeable_fields + ['apply_permission_name', ]
read_only_fields = list(set(fields) - set(writeable_fields))
read_only_fields = TicketApplySerializer.Meta.read_only_fields + ['apply_permission_name', ]
fields = TicketApplySerializer.Meta.fields_small + writeable_fields + read_only_fields
ticket_extra_kwargs = TicketApplySerializer.Meta.extra_kwargs
extra_kwargs = {
'apply_nodes': {'required': False},
'apply_assets': {'required': False},
'apply_accounts': {'required': False},
}
extra_kwargs.update(ticket_extra_kwargs)

View File

@ -75,10 +75,11 @@ class BaseApplyAssetSerializer(serializers.Serializer):
def create(self, validated_data):
instance = super().create(validated_data)
name = _('Created by ticket ({}-{})').format(instance.title, str(instance.id)[:4])
with tmp_to_org(instance.org_id):
org_id = instance.org_id
with tmp_to_org(org_id):
if not self.permission_model.objects.filter(name=name).exists():
instance.apply_permission_name = name
instance.save()
instance.save(update_fields=['apply_permission_name'])
return instance
raise serializers.ValidationError(_('Permission named `{}` already exists'.format(name)))

View File

@ -22,13 +22,12 @@ class TicketSerializer(OrgResourceModelSerializerMixin):
class Meta:
model = Ticket
fields_mini = ['id', 'title']
fields_small = fields_mini + [
'type', 'status', 'state', 'approval_step', 'comment',
'date_created', 'date_updated', 'org_id', 'rel_snapshot',
'process_map', 'org_name', 'serial_num'
fields_small = fields_mini + ['org_id', 'comment']
read_only_fields = [
'serial_num', 'process_map', 'approval_step', 'type', 'state', 'applicant',
'status', 'date_created', 'date_updated', 'org_name', 'rel_snapshot'
]
fields_fk = ['applicant', ]
fields = fields_small + fields_fk
fields = fields_small + read_only_fields
extra_kwargs = {
'type': {'required': True}
}
@ -72,8 +71,6 @@ class TicketApplySerializer(TicketSerializer):
if self.instance:
return attrs
print("Attrs: ", attrs)
ticket_type = attrs.get('type')
org_id = attrs.get('org_id')
flow = TicketFlow.get_org_related_flows(org_id=org_id) \
@ -81,7 +78,7 @@ class TicketApplySerializer(TicketSerializer):
if flow:
attrs['flow'] = flow
return attrs
else:
error = _('The ticket flow `{}` does not exist'.format(ticket_type))
raise serializers.ValidationError(error)
return attrs