From d3d89b0853d96f2afc9b4639eca564942b5473c2 Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Thu, 28 Mar 2024 18:28:49 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=8E=BB=E6=8E=89=E5=B7=A5=E5=8D=95?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=89=93=E5=BC=80=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/tickets/const.py | 2 -- apps/tickets/filters.py | 10 ++++-- apps/tickets/handlers/base.py | 2 +- .../migrations/0016_auto_20220609_1758.py | 6 ++-- ...23_alter_applyassetticket_apply_actions.py | 9 ++---- .../migrations/0024_auto_20221121_1800.py | 31 ++++++++++++++----- apps/tickets/models/ticket/general.py | 5 +-- apps/tickets/serializers/ticket/ticket.py | 2 +- 8 files changed, 39 insertions(+), 28 deletions(-) diff --git a/apps/tickets/const.py b/apps/tickets/const.py index 09c1b39e4..45424d883 100644 --- a/apps/tickets/const.py +++ b/apps/tickets/const.py @@ -16,7 +16,6 @@ class TicketType(TextChoices): class TicketState(TextChoices): pending = 'pending', _('Open') closed = 'closed', _("Cancel") - reopen = 'reopen', _("Reopen") approved = 'approved', _('Approved') rejected = 'rejected', _('Rejected') @@ -29,7 +28,6 @@ class TicketStatus(TextChoices): class StepState(TextChoices): pending = 'pending', _('Pending') closed = 'closed', _("Closed") - reopen = 'reopen', _("Reopen") approved = 'approved', _('Approved') rejected = 'rejected', _('Rejected') diff --git a/apps/tickets/filters.py b/apps/tickets/filters.py index a90f88316..0c1cea8ba 100644 --- a/apps/tickets/filters.py +++ b/apps/tickets/filters.py @@ -1,4 +1,4 @@ -from django.db.models import Subquery, OuterRef, Value, F, Q +from django.db.models import Subquery, OuterRef, Value, F, Q from django_filters import rest_framework as filters from django.db.models.functions import Concat @@ -11,10 +11,9 @@ from tickets.models import ( class TicketFilter(BaseFilterSet): + applicant = filters.CharFilter(method='filter_applicant') assignees__id = filters.UUIDFilter(method='filter_assignees_id') - relevant_app = filters.CharFilter(method='filter_relevant_app') relevant_asset = filters.CharFilter(method='filter_relevant_asset') - relevant_system_user = filters.CharFilter(method='filter_relevant_system_user') relevant_command = filters.CharFilter(method='filter_relevant_command') class Meta: @@ -64,6 +63,11 @@ class TicketFilter(BaseFilterSet): ).values_list('id', flat=True) return queryset.filter(id__in=list(command_ids)) + def filter_applicant(self, queryset, name, value): + return queryset.filter( + Q(applicant__username__icontains=value) | Q(applicant__name__icontains=value) + ) + class ApplyAssetTicketFilter(BaseFilterSet): class Meta: diff --git a/apps/tickets/handlers/base.py b/apps/tickets/handlers/base.py index fbf6a8e46..c74ca7a0c 100644 --- a/apps/tickets/handlers/base.py +++ b/apps/tickets/handlers/base.py @@ -87,7 +87,7 @@ class BaseHandler: def _create_state_change_comment(self, state): # 打开或关闭工单,备注显示是自己,其他是受理人 - if state in [TicketState.reopen, TicketState.pending, TicketState.closed]: + if state in [TicketState.pending, TicketState.closed]: user = self.ticket.applicant else: user = self.ticket.processor diff --git a/apps/tickets/migrations/0016_auto_20220609_1758.py b/apps/tickets/migrations/0016_auto_20220609_1758.py index d7f079e5d..19275f8f4 100644 --- a/apps/tickets/migrations/0016_auto_20220609_1758.py +++ b/apps/tickets/migrations/0016_auto_20220609_1758.py @@ -61,7 +61,7 @@ class Migration(migrations.Migration): model_name='ticket', name='state', field=models.CharField(choices=[('pending', 'Open'), ('approved', 'Approved'), ('rejected', 'Rejected'), - ('closed', 'Cancel'), ('reopen', 'Reopen')], default='pending', + ('closed', 'Cancel')], default='pending', max_length=16, verbose_name='State'), ), migrations.AlterField( @@ -74,14 +74,14 @@ class Migration(migrations.Migration): model_name='ticketassignee', name='state', field=models.CharField(choices=[('pending', 'Open'), ('approved', 'Approved'), ('rejected', 'Rejected'), - ('closed', 'Cancel'), ('reopen', 'Reopen')], default='pending', + ('closed', 'Cancel')], default='pending', max_length=64), ), migrations.AlterField( model_name='ticketstep', name='state', field=models.CharField(choices=[('pending', 'Pending'), ('approved', 'Approved'), ('rejected', 'Rejected'), - ('closed', 'Closed'), ('reopen', 'Reopen')], default='pending', + ('closed', 'Closed')], default='pending', max_length=64, verbose_name='State'), ), migrations.CreateModel( diff --git a/apps/tickets/migrations/0023_alter_applyassetticket_apply_actions.py b/apps/tickets/migrations/0023_alter_applyassetticket_apply_actions.py index b4121ce51..871628ff9 100644 --- a/apps/tickets/migrations/0023_alter_applyassetticket_apply_actions.py +++ b/apps/tickets/migrations/0023_alter_applyassetticket_apply_actions.py @@ -30,8 +30,7 @@ class Migration(migrations.Migration): field=models.CharField( choices=[ ('pending', 'Open'), ('closed', 'Cancel'), - ('reopen', 'Reopen'), ('approved', 'Approved'), - ('rejected', 'Rejected') + ('approved', 'Approved'), ('rejected', 'Rejected') ], default='pending', max_length=16, verbose_name='State'), ), migrations.AlterField( @@ -51,8 +50,7 @@ class Migration(migrations.Migration): field=models.CharField( choices=[ ('pending', 'Open'), ('closed', 'Cancel'), - ('reopen', 'Reopen'), ('approved', 'Approved'), - ('rejected', 'Rejected') + ('approved', 'Approved'), ('rejected', 'Rejected') ], default='pending', max_length=64), ), migrations.AlterField( @@ -72,8 +70,7 @@ class Migration(migrations.Migration): field=models.CharField( choices=[ ('pending', 'Pending'), ('closed', 'Closed'), - ('reopen', 'Reopen'), ('approved', 'Approved'), - ('rejected', 'Rejected') + ('approved', 'Approved'), ('rejected', 'Rejected') ], default='pending', max_length=64, verbose_name='State'), ), migrations.AlterField( diff --git a/apps/tickets/migrations/0024_auto_20221121_1800.py b/apps/tickets/migrations/0024_auto_20221121_1800.py index acd203ef6..92f676615 100644 --- a/apps/tickets/migrations/0024_auto_20221121_1800.py +++ b/apps/tickets/migrations/0024_auto_20221121_1800.py @@ -4,7 +4,6 @@ from django.db import migrations, models class Migration(migrations.Migration): - dependencies = [ ('tickets', '0023_alter_applyassetticket_apply_actions'), ] @@ -13,36 +12,52 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='approvalrule', name='strategy', - field=models.CharField(choices=[('org_admin', 'Org admin'), ('custom_user', 'Custom user'), ('super_admin', 'Super admin'), ('super_org_admin', 'Super admin and org admin')], default='super_admin', max_length=64, verbose_name='Approve strategy'), + field=models.CharField( + choices=[('org_admin', 'Org admin'), ('custom_user', 'Custom user'), ('super_admin', 'Super admin'), + ('super_org_admin', 'Super admin and org admin')], default='super_admin', max_length=64, + verbose_name='Approve strategy'), ), migrations.AlterField( model_name='ticket', name='state', - field=models.CharField(choices=[('pending', 'Open'), ('closed', 'Cancel'), ('reopen', 'Reopen'), ('approved', 'Approved'), ('rejected', 'Rejected')], default='pending', max_length=16, verbose_name='State'), + field=models.CharField( + choices=[('pending', 'Open'), ('closed', 'Cancel'), ('approved', 'Approved'), ('rejected', 'Rejected')], + default='pending', max_length=16, verbose_name='State'), ), migrations.AlterField( model_name='ticket', name='type', - field=models.CharField(choices=[('general', 'General'), ('apply_asset', 'Apply for asset'), ('login_confirm', 'Login confirm'), ('command_confirm', 'Command confirm'), ('login_asset_confirm', 'Login asset confirm')], default='general', max_length=64, verbose_name='Type'), + field=models.CharField( + choices=[('general', 'General'), ('apply_asset', 'Apply for asset'), ('login_confirm', 'Login confirm'), + ('command_confirm', 'Command confirm'), ('login_asset_confirm', 'Login asset confirm')], + default='general', max_length=64, verbose_name='Type'), ), migrations.AlterField( model_name='ticketassignee', name='state', - field=models.CharField(choices=[('pending', 'Open'), ('closed', 'Cancel'), ('reopen', 'Reopen'), ('approved', 'Approved'), ('rejected', 'Rejected')], default='pending', max_length=64), + field=models.CharField( + choices=[('pending', 'Open'), ('closed', 'Cancel'), ('approved', 'Approved'), ('rejected', 'Rejected')], + default='pending', max_length=64), ), migrations.AlterField( model_name='ticketflow', name='type', - field=models.CharField(choices=[('general', 'General'), ('apply_asset', 'Apply for asset'), ('login_confirm', 'Login confirm'), ('command_confirm', 'Command confirm'), ('login_asset_confirm', 'Login asset confirm')], default='general', max_length=64, verbose_name='Type'), + field=models.CharField( + choices=[('general', 'General'), ('apply_asset', 'Apply for asset'), ('login_confirm', 'Login confirm'), + ('command_confirm', 'Command confirm'), ('login_asset_confirm', 'Login asset confirm')], + default='general', max_length=64, verbose_name='Type'), ), migrations.AlterField( model_name='ticketstep', name='state', - field=models.CharField(choices=[('pending', 'Pending'), ('closed', 'Closed'), ('reopen', 'Reopen'), ('approved', 'Approved'), ('rejected', 'Rejected')], default='pending', max_length=64, verbose_name='State'), + field=models.CharField(choices=[('pending', 'Pending'), ('closed', 'Closed'), ('approved', 'Approved'), + ('rejected', 'Rejected')], default='pending', max_length=64, + verbose_name='State'), ), migrations.AlterField( model_name='ticketstep', name='status', - field=models.CharField(choices=[('active', 'Active'), ('closed', 'Closed'), ('pending', 'Pending')], default='pending', max_length=16), + field=models.CharField(choices=[('active', 'Active'), ('closed', 'Closed'), ('pending', 'Pending')], + default='pending', max_length=16), ), ] diff --git a/apps/tickets/models/ticket/general.py b/apps/tickets/models/ticket/general.py index 656a8322a..52cf925c1 100644 --- a/apps/tickets/models/ticket/general.py +++ b/apps/tickets/models/ticket/general.py @@ -143,16 +143,13 @@ class StatusMixin: def reject(self, processor): self._change_state(StepState.rejected, processor) - def reopen(self): - self._change_state_by_applicant(TicketState.reopen) - def close(self): self._change_state(TicketState.closed, self.applicant) def _change_state_by_applicant(self, state): if state == TicketState.closed: self.status = TicketStatus.closed - elif state in [TicketState.reopen, TicketState.pending]: + elif state == TicketState.pending: self.status = TicketStatus.open else: raise ValueError("Not supported state: {}".format(state)) diff --git a/apps/tickets/serializers/ticket/ticket.py b/apps/tickets/serializers/ticket/ticket.py index 607063367..442a03109 100644 --- a/apps/tickets/serializers/ticket/ticket.py +++ b/apps/tickets/serializers/ticket/ticket.py @@ -18,7 +18,7 @@ __all__ = [ class TicketSerializer(OrgResourceModelSerializerMixin): type = LabeledChoiceField(choices=TicketType.choices, read_only=True, label=_('Type')) status = LabeledChoiceField(choices=TicketStatus.choices, read_only=True, label=_('Status')) - state = LabeledChoiceField(choices=TicketState.choices, read_only=True, label=_("State")) + state = LabeledChoiceField(choices=TicketState.choices, read_only=True, label=_("Action")) class Meta: model = Ticket