Skip to content

Commit

Permalink
refactor: cr调整
Browse files Browse the repository at this point in the history
  • Loading branch information
neronkl committed Nov 20, 2023
1 parent a6a2b6c commit c536ff1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
17 changes: 16 additions & 1 deletion src/bk-user/bkuser/apis/web/tenant_setting/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,22 @@ def _validate_options(options):
if not options:
raise serializers.ValidationError(_("枚举类型的自定义字段需要传递非空的<选项>字段"))
try:
TenantUserCustomFieldOptions(options=options)
option_objs = TenantUserCustomFieldOptions(options=options)

limit_count = 2

def determine_duplicate_values(values: List[str]) -> bool:
# 判断重复值
return any(values.count(item) >= limit_count for item in values)

option_ids = [obj.id for obj in option_objs.options]
if determine_duplicate_values(option_ids):
raise ValueError(_("枚举ID设置有重复值"))

option_values = [obj.value for obj in option_objs.options]
if determine_duplicate_values(option_values):
raise ValueError(_("枚举ID设置有重复值"))

except PDValidationError as e:
raise serializers.ValidationError(_("<选项>字段不合法: {}".format(e)))

Expand Down
19 changes: 0 additions & 19 deletions src/bk-user/bkuser/apps/tenant/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,3 @@ class TenantUserCustomFieldOptions(BaseModel):
"""用户自定义字段-options字段"""

options: List[Option]

@model_validator(mode="after")
def validate_attrs(self) -> "TenantUserCustomFieldOptions":
option_ids = [obj.id for obj in self.options]

limit_count = 2

def determine_duplicate_values(values: List[str]) -> bool:
# 判断重复值
return any(values.count(item) >= limit_count for item in values)

if determine_duplicate_values(option_ids):
raise ValueError(_("枚举ID设置有重复值"))

option_values = [obj.value for obj in self.options]
if determine_duplicate_values(option_values):
raise ValueError(_("枚举ID设置有重复值"))

return self

0 comments on commit c536ff1

Please sign in to comment.