diff --git a/apps/tickets/api/ticket.py b/apps/tickets/api/ticket.py index 747066b0c..712d4b6fa 100644 --- a/apps/tickets/api/ticket.py +++ b/apps/tickets/api/ticket.py @@ -34,10 +34,10 @@ class TicketViewSet(CommonApiMixin, viewsets.ModelViewSet): model = Ticket filterset_class = filters.TicketFilter search_fields = [ - 'title', 'type', 'status', 'applicant_display' + 'title', 'type', 'status' ] ordering_fields = ( - 'title', 'applicant_display', 'status', 'state', + 'title', 'status', 'state', 'action_display', 'date_created', 'serial_num', ) ordering = ('-date_created',) @@ -67,7 +67,8 @@ class TicketViewSet(CommonApiMixin, viewsets.ModelViewSet): @action(detail=False, methods=[POST], permission_classes=[RBACPermission, ]) def open(self, request, *args, **kwargs): - return super().create(request, *args, **kwargs) + with tmp_to_root_org(): + return super().create(request, *args, **kwargs) @action(detail=True, methods=[PUT], permission_classes=[IsAssignee, ]) def approve(self, request, *args, **kwargs): diff --git a/apps/tickets/models/ticket/general.py b/apps/tickets/models/ticket/general.py index b8f192a9d..2e8f1d886 100644 --- a/apps/tickets/models/ticket/general.py +++ b/apps/tickets/models/ticket/general.py @@ -189,7 +189,7 @@ class StatusMixin: assignees_display.append(str(i.assignee)) if state != StepState.pending and state == i.state: processor = i.assignee - if state != StepState.closed: + if state == StepState.closed: processor = self.applicant step_info = { 'state': state, diff --git a/apps/tickets/serializers/ticket/apply_application.py b/apps/tickets/serializers/ticket/apply_application.py index b2774e420..1b5165bbd 100644 --- a/apps/tickets/serializers/ticket/apply_application.py +++ b/apps/tickets/serializers/ticket/apply_application.py @@ -26,15 +26,9 @@ class ApplyApplicationSerializer(BaseApplyAssetApplicationSerializer, TicketAppl extra_kwargs = {} extra_kwargs.update(ticket_extra_kwargs) - def validate_apply_applications(self, apply_applications): - type = self.initial_data.get('apply_type') - org_id = self.initial_data.get('org_id') - application_ids = [app.id for app in apply_applications] - with tmp_to_org(org_id): - applications = Application.objects.filter( - id__in=application_ids, type=type - ).values_list('id', flat=True) - return list(applications) + def validate_apply_applications(self, applications): + tp = self.initial_data.get('apply_type') + return self.filter_many_to_many_field(Application, applications, type=tp) class ApplyApplicationDisplaySerializer(ApplyApplicationSerializer): diff --git a/apps/tickets/serializers/ticket/apply_asset.py b/apps/tickets/serializers/ticket/apply_asset.py index 3c8e0aade..fe54eadeb 100644 --- a/apps/tickets/serializers/ticket/apply_asset.py +++ b/apps/tickets/serializers/ticket/apply_asset.py @@ -4,6 +4,7 @@ from rest_framework import serializers from perms.serializers.base import ActionsField from perms.models import AssetPermission from orgs.utils import tmp_to_org +from assets.models import Asset, Node from tickets.models import ApplyAssetTicket from .ticket import TicketApplySerializer @@ -34,6 +35,12 @@ class ApplyAssetSerializer(BaseApplyAssetApplicationSerializer, TicketApplySeria } extra_kwargs.update(ticket_extra_kwargs) + def validate_apply_nodes(self, nodes): + return self.filter_many_to_many_field(Node, nodes) + + def validate_apply_assets(self, assets): + return self.filter_many_to_many_field(Asset, assets) + def validate(self, attrs): attrs = super().validate(attrs) if not attrs.get('apply_nodes') and not attrs.get('apply_assets'): diff --git a/apps/tickets/serializers/ticket/common.py b/apps/tickets/serializers/ticket/common.py index ef52c14d3..4a375465e 100644 --- a/apps/tickets/serializers/ticket/common.py +++ b/apps/tickets/serializers/ticket/common.py @@ -3,6 +3,7 @@ from django.db.models import Model from django.utils.translation import ugettext as _ from rest_framework import serializers +from assets.models import SystemUser from orgs.utils import tmp_to_org from tickets.models import Ticket @@ -37,6 +38,16 @@ class DefaultPermissionName(object): class BaseApplyAssetApplicationSerializer(serializers.Serializer): permission_model: Model + def filter_many_to_many_field(self, model, values: list, **kwargs): + org_id = self.initial_data.get('org_id') + ids = [instance.id for instance in values] + with tmp_to_org(org_id): + qs = model.objects.filter(id__in=ids, **kwargs).values_list('id', flat=True) + return list(qs) + + def validate_apply_system_users(self, system_users): + return self.filter_many_to_many_field(SystemUser, system_users) + def validate(self, attrs): attrs = super().validate(attrs)