From 582a8e0c52e2b29a6f63642c5dde97fc67973f97 Mon Sep 17 00:00:00 2001 From: "Jiangjie.Bai" Date: Tue, 15 Nov 2022 14:59:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=20BitChoicesField=20t?= =?UTF-8?q?o=5Frepresentation=20swagger=20=E8=B0=83=E7=94=A8=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/drf/fields.py | 7 +++++++ apps/perms/serializers/permission.py | 2 +- apps/perms/urls/asset_permission.py | 7 +++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/apps/common/drf/fields.py b/apps/common/drf/fields.py index 9f2527d8a..c666d9b76 100644 --- a/apps/common/drf/fields.py +++ b/apps/common/drf/fields.py @@ -120,6 +120,13 @@ class BitChoicesField(TreeChoicesMixin, serializers.MultipleChoiceField): super().__init__(choices=choices, **kwargs) def to_representation(self, value): + if isinstance(value, list) and len(value) == 1: + # Swagger 会使用 field.choices.keys() 迭代传递进来 + return [ + {"value": c.name, "label": c.label} + for c in self._choice_cls + if c.name == value[0] + ] return [ {"value": c.name, "label": c.label} for c in self._choice_cls diff --git a/apps/perms/serializers/permission.py b/apps/perms/serializers/permission.py index 9a31058f6..fc2bf1cf3 100644 --- a/apps/perms/serializers/permission.py +++ b/apps/perms/serializers/permission.py @@ -16,7 +16,7 @@ __all__ = ["AssetPermissionSerializer", "ActionChoicesField"] class ActionChoicesField(BitChoicesField): def __init__(self, **kwargs): - super().__init__(ActionChoices, **kwargs) + super().__init__(choice_cls=ActionChoices, **kwargs) class AssetPermissionSerializer(BulkOrgResourceModelSerializer): diff --git a/apps/perms/urls/asset_permission.py b/apps/perms/urls/asset_permission.py index f402372df..cdf502c0f 100644 --- a/apps/perms/urls/asset_permission.py +++ b/apps/perms/urls/asset_permission.py @@ -65,12 +65,15 @@ user_permission_urlpatterns = [ # 收藏的资产 path('/nodes/favorite/assets/', api.UserFavoriteGrantedAssetsApi.as_view(), name='user-ungrouped-assets'), - path('nodes/favorite/assets/', api.MyFavoriteGrantedAssetsApi.as_view(), name='my-ungrouped-assets'), + path('nodes/favorite/assets/', api.MyFavoriteGrantedAssetsApi.as_view(), + name='my-ungrouped-assets'), # 获取授权给用户某个资产的所有账号 path('/assets//accounts/', api.UserGrantedAssetAccountsApi.as_view(), name='user-asset-accounts'), - path('assets//accounts/', api.MyGrantedAssetAccountsApi.as_view(), name='my-asset-accounts'), + path('/((?P[^/.]+)/)?assets//accounts/', + api.UserGrantedAssetAccountsApi.as_view(), + name='my-asset-accounts'), ] user_group_permission_urlpatterns = [