From ea4dccbab8baf2f5a528b01e7af2bcbead664da8 Mon Sep 17 00:00:00 2001 From: feng626 <1304903146@qq.com> Date: Sun, 12 Sep 2021 21:00:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=B7=A5=E5=8D=95bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/applications/api/application.py | 14 +++++-------- apps/applications/serializers/application.py | 8 +++++++- apps/assets/api/asset.py | 13 +++--------- apps/assets/api/system_user.py | 4 +++- apps/assets/serializers/system_user.py | 9 ++++++++- apps/common/mixins/views.py | 21 ++++++++++++++++++-- apps/tickets/serializers/ticket/ticket.py | 2 +- 7 files changed, 46 insertions(+), 25 deletions(-) diff --git a/apps/applications/api/application.py b/apps/applications/api/application.py index 1090e0095..245379630 100644 --- a/apps/applications/api/application.py +++ b/apps/applications/api/application.py @@ -6,14 +6,15 @@ from rest_framework.decorators import action from rest_framework.response import Response from common.tree import TreeNodeSerializer -from ..hands import IsOrgAdminOrAppUser, IsValidUser +from common.mixins.views import SuggestionMixin +from ..hands import IsOrgAdminOrAppUser from .. import serializers from ..models import Application __all__ = ['ApplicationViewSet'] -class ApplicationViewSet(OrgBulkModelViewSet): +class ApplicationViewSet(SuggestionMixin, OrgBulkModelViewSet): model = Application filterset_fields = { 'name': ['exact'], @@ -24,7 +25,8 @@ class ApplicationViewSet(OrgBulkModelViewSet): permission_classes = (IsOrgAdminOrAppUser,) serializer_classes = { 'default': serializers.AppSerializer, - 'get_tree': TreeNodeSerializer + 'get_tree': TreeNodeSerializer, + 'suggestion': serializers.MiniAppSerializer } @action(methods=['GET'], detail=False, url_path='tree') @@ -34,9 +36,3 @@ class ApplicationViewSet(OrgBulkModelViewSet): tree_nodes = Application.create_tree_nodes(queryset, show_count=show_count) serializer = self.get_serializer(tree_nodes, many=True) return Response(serializer.data) - - @action(methods=['get'], detail=False, permission_classes=(IsValidUser,)) - def suggestion(self, request): - queryset = self.filter_queryset(self.get_queryset())[:3] - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) diff --git a/apps/applications/serializers/application.py b/apps/applications/serializers/application.py index 79cbdd3bc..0eba3e3e9 100644 --- a/apps/applications/serializers/application.py +++ b/apps/applications/serializers/application.py @@ -15,7 +15,7 @@ from .. import models from .. import const __all__ = [ - 'AppSerializer', 'AppSerializerMixin', + 'AppSerializer', 'MiniAppSerializer', 'AppSerializerMixin', 'AppAccountSerializer', 'AppAccountSecretSerializer' ] @@ -78,6 +78,12 @@ class AppSerializer(AppSerializerMixin, BulkOrgResourceModelSerializer): return _attrs +class MiniAppSerializer(serializers.ModelSerializer): + class Meta: + model = models.Application + fields = AppSerializer.Meta.fields_mini + + class AppAccountSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer): category = serializers.ChoiceField(label=_('Category'), choices=const.AppCategory.choices, read_only=True) category_display = serializers.SerializerMethodField(label=_('Category display')) diff --git a/apps/assets/api/asset.py b/apps/assets/api/asset.py index 35023c39d..5fe0152d7 100644 --- a/apps/assets/api/asset.py +++ b/apps/assets/api/asset.py @@ -1,14 +1,13 @@ # -*- coding: utf-8 -*- # from assets.api import FilterAssetByNodeMixin -from rest_framework.decorators import action from rest_framework.viewsets import ModelViewSet from rest_framework.generics import RetrieveAPIView -from rest_framework.response import Response from django.shortcuts import get_object_or_404 from common.utils import get_logger, get_object_or_none -from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser, IsSuperUser, IsValidUser +from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser, IsSuperUser +from common.mixins.views import SuggestionMixin from orgs.mixins.api import OrgBulkModelViewSet from orgs.mixins import generics from ..models import Asset, Node, Platform @@ -27,7 +26,7 @@ __all__ = [ ] -class AssetViewSet(FilterAssetByNodeMixin, OrgBulkModelViewSet): +class AssetViewSet(SuggestionMixin, FilterAssetByNodeMixin, OrgBulkModelViewSet): """ API endpoint that allows Asset to be viewed or edited. """ @@ -64,12 +63,6 @@ class AssetViewSet(FilterAssetByNodeMixin, OrgBulkModelViewSet): assets = serializer.save() self.set_assets_node(assets) - @action(methods=['get'], detail=False, permission_classes=(IsValidUser,)) - def suggestion(self, request): - queryset = self.filter_queryset(self.get_queryset())[:3] - serializer = self.get_serializer(queryset, many=True) - return Response(serializer.data) - class AssetPlatformRetrieveApi(RetrieveAPIView): queryset = Platform.objects.all() diff --git a/apps/assets/api/system_user.py b/apps/assets/api/system_user.py index 03a506640..674ff2208 100644 --- a/apps/assets/api/system_user.py +++ b/apps/assets/api/system_user.py @@ -6,6 +6,7 @@ from common.utils import get_logger from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser, IsValidUser from orgs.mixins.api import OrgBulkModelViewSet from orgs.mixins import generics +from common.mixins.views import SuggestionMixin from orgs.utils import tmp_to_root_org from ..models import SystemUser, Asset from .. import serializers @@ -24,7 +25,7 @@ __all__ = [ ] -class SystemUserViewSet(OrgBulkModelViewSet): +class SystemUserViewSet(SuggestionMixin, OrgBulkModelViewSet): """ System user api set, for add,delete,update,list,retrieve resource """ @@ -39,6 +40,7 @@ class SystemUserViewSet(OrgBulkModelViewSet): serializer_class = serializers.SystemUserSerializer serializer_classes = { 'default': serializers.SystemUserSerializer, + 'suggestion': serializers.MiniSystemUserSerializer } permission_classes = (IsOrgAdminOrAppUser,) diff --git a/apps/assets/serializers/system_user.py b/apps/assets/serializers/system_user.py index 69848ce20..8c4ebf98d 100644 --- a/apps/assets/serializers/system_user.py +++ b/apps/assets/serializers/system_user.py @@ -10,7 +10,7 @@ from .utils import validate_password_contains_left_double_curly_bracket from .base import AuthSerializerMixin __all__ = [ - 'SystemUserSerializer', + 'SystemUserSerializer', 'MiniSystemUserSerializer', 'SystemUserSimpleSerializer', 'SystemUserAssetRelationSerializer', 'SystemUserNodeRelationSerializer', 'SystemUserTaskSerializer', 'SystemUserUserRelationSerializer', 'SystemUserWithAuthInfoSerializer', @@ -185,6 +185,12 @@ class SystemUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer): return queryset +class MiniSystemUserSerializer(serializers.ModelSerializer): + class Meta: + model = SystemUser + fields = SystemUserSerializer.Meta.fields_mini + + class SystemUserWithAuthInfoSerializer(SystemUserSerializer): class Meta(SystemUserSerializer.Meta): fields_mini = ['id', 'name', 'username'] @@ -208,6 +214,7 @@ class SystemUserSimpleSerializer(serializers.ModelSerializer): """ 系统用户最基本信息的数据结构 """ + class Meta: model = SystemUser fields = ('id', 'name', 'username') diff --git a/apps/common/mixins/views.py b/apps/common/mixins/views.py index 1590f48ba..65950bdca 100644 --- a/apps/common/mixins/views.py +++ b/apps/common/mixins/views.py @@ -3,12 +3,14 @@ # coding: utf-8 from django.contrib.auth.mixins import UserPassesTestMixin from django.utils import timezone +from rest_framework.decorators import action +from rest_framework import permissions +from rest_framework.response import Response +from common.permissions import IsValidUser __all__ = ["DatetimeSearchMixin", "PermissionsMixin"] -from rest_framework import permissions - class DatetimeSearchMixin: date_format = '%Y-%m-%d' @@ -52,3 +54,18 @@ class PermissionsMixin(UserPassesTestMixin): if not permission_class().has_permission(self.request, self): return False return True + + +class SuggestionMixin: + + @action(methods=['get'], detail=False, permission_classes=(IsValidUser,)) + def suggestions(self, request, *args, **kwargs): + queryset = self.filter_queryset(self.get_queryset()) + queryset = queryset[:10] + page = self.paginate_queryset(queryset) + if page is not None: + serializer = self.get_serializer(page, many=True) + return self.get_paginated_response(serializer.data) + + serializer = self.get_serializer(queryset, many=True) + return Response(serializer.data) diff --git a/apps/tickets/serializers/ticket/ticket.py b/apps/tickets/serializers/ticket/ticket.py index ca5f6add1..b5565008b 100644 --- a/apps/tickets/serializers/ticket/ticket.py +++ b/apps/tickets/serializers/ticket/ticket.py @@ -149,7 +149,7 @@ class TicketFlowApproveSerializer(serializers.ModelSerializer): fields = fields_small + fields_m2m read_only_fields = ['level', 'assignees_display'] extra_kwargs = { - 'assignees': {'write_only': True, 'allow_empty': True} + 'assignees': {'write_only': True, 'allow_empty': True, 'required': False} } def get_assignees_read_only(self, obj):