From 21d24ae4bc35bdbee4dfe8c6a24597df66997886 Mon Sep 17 00:00:00 2001 From: "Jiangjie.Bai" Date: Wed, 16 Nov 2022 16:23:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E8=A7=84=E5=88=99=E6=97=B6=E4=B8=8D=E5=8C=85?= =?UTF-8?q?=E5=90=ABactions=E6=8A=A5=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 | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/common/drf/fields.py b/apps/common/drf/fields.py index c666d9b76..9742f639c 100644 --- a/apps/common/drf/fields.py +++ b/apps/common/drf/fields.py @@ -4,7 +4,7 @@ import six from django.core.exceptions import ObjectDoesNotExist from django.utils.translation import gettext_lazy as _ from rest_framework import serializers -from rest_framework.fields import ChoiceField +from rest_framework.fields import ChoiceField, empty from common.db.fields import BitChoices from common.utils import decrypt_password @@ -153,3 +153,15 @@ class BitChoicesField(TreeChoicesMixin, serializers.MultipleChoiceField): raise serializers.ValidationError(_("Invalid choice: {}").format(name)) value |= name_value_map[name] return value + + def run_validation(self, data=empty): + """ + 备注: + 创建授权规则不包含 actions 字段时, 会使用默认值(AssetPermission 中设置), + 会直接使用 ['connect', '...'] 等字段保存到数据库,导致类型错误 + 这里将获取到的值再执行一下 to_internal_value 方法, 转化为内部值 + """ + data = super().run_validation(data) + value = self.to_internal_value(data) + self.run_validators(value) + return value From ec462e09e0846f8a9b1fb01114c14f728978de15 Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Wed, 16 Nov 2022 19:35:16 +0800 Subject: [PATCH 2/2] fix: action --- apps/common/drf/fields.py | 2 ++ apps/common/utils/integer.py | 7 +++---- apps/perms/const.py | 10 +++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/apps/common/drf/fields.py b/apps/common/drf/fields.py index 9742f639c..270c8d91a 100644 --- a/apps/common/drf/fields.py +++ b/apps/common/drf/fields.py @@ -162,6 +162,8 @@ class BitChoicesField(TreeChoicesMixin, serializers.MultipleChoiceField): 这里将获取到的值再执行一下 to_internal_value 方法, 转化为内部值 """ data = super().run_validation(data) + if isinstance(data, int): + return data value = self.to_internal_value(data) self.run_validators(value) return value diff --git a/apps/common/utils/integer.py b/apps/common/utils/integer.py index c87e939f0..9e657a6dd 100644 --- a/apps/common/utils/integer.py +++ b/apps/common/utils/integer.py @@ -1,5 +1,4 @@ def bit(x): - if x == 0: - return 0 - else: - return 2 ** (x - 1) + if x < 1: + raise ValueError("x must be greater than 1") + return 2 ** (x - 1) diff --git a/apps/perms/const.py b/apps/perms/const.py index 99495369c..cecaa5e3a 100644 --- a/apps/perms/const.py +++ b/apps/perms/const.py @@ -10,11 +10,11 @@ __all__ = ["ActionChoices"] class ActionChoices(BitChoices): - connect = bit(0), _("Connect") - upload = bit(1), _("Upload") - download = bit(2), _("Download") - copy = bit(3), _("Copy") - paste = bit(4), _("Paste") + connect = bit(1), _("Connect") + upload = bit(2), _("Upload") + download = bit(3), _("Download") + copy = bit(4), _("Copy") + paste = bit(5), _("Paste") @classmethod def is_tree(cls):