mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-07-19 17:26:52 +00:00
fix: 修复手机号字段问题
This commit is contained in:
parent
34e846927b
commit
616e636837
@ -1,5 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
|
import phonenumbers
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
@ -17,6 +18,7 @@ __all__ = [
|
|||||||
"BitChoicesField",
|
"BitChoicesField",
|
||||||
"TreeChoicesField",
|
"TreeChoicesField",
|
||||||
"LabeledMultipleChoiceField",
|
"LabeledMultipleChoiceField",
|
||||||
|
"PhoneField",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -201,3 +203,11 @@ class BitChoicesField(TreeChoicesField):
|
|||||||
value = self.to_internal_value(data)
|
value = self.to_internal_value(data)
|
||||||
self.run_validators(value)
|
self.run_validators(value)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class PhoneField(serializers.CharField):
|
||||||
|
def to_representation(self, value):
|
||||||
|
if value:
|
||||||
|
phone = phonenumbers.parse(value, 'CN')
|
||||||
|
value = {'code': '+%s' % phone.country_code, 'phone': phone.national_number}
|
||||||
|
return value
|
||||||
|
@ -8,7 +8,9 @@ from django.utils.translation import ugettext_lazy as _
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from common.serializers import CommonBulkSerializerMixin
|
from common.serializers import CommonBulkSerializerMixin
|
||||||
from common.serializers.fields import EncryptedField, ObjectRelatedField, LabeledChoiceField
|
from common.serializers.fields import (
|
||||||
|
EncryptedField, ObjectRelatedField, LabeledChoiceField, PhoneField
|
||||||
|
)
|
||||||
from common.utils import pretty_string, get_logger
|
from common.utils import pretty_string, get_logger
|
||||||
from common.validators import PhoneValidator
|
from common.validators import PhoneValidator
|
||||||
from rbac.builtin import BuiltinRole
|
from rbac.builtin import BuiltinRole
|
||||||
@ -103,6 +105,9 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
|
|||||||
label=_("Password"), required=False, allow_blank=True,
|
label=_("Password"), required=False, allow_blank=True,
|
||||||
allow_null=True, max_length=1024,
|
allow_null=True, max_length=1024,
|
||||||
)
|
)
|
||||||
|
phone = PhoneField(
|
||||||
|
validators=[PhoneValidator()], required=False, allow_null=True, label=_("Phone")
|
||||||
|
)
|
||||||
custom_m2m_fields = {
|
custom_m2m_fields = {
|
||||||
"system_roles": [BuiltinRole.system_user],
|
"system_roles": [BuiltinRole.system_user],
|
||||||
"org_roles": [BuiltinRole.org_user],
|
"org_roles": [BuiltinRole.org_user],
|
||||||
@ -169,7 +174,6 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
|
|||||||
"created_by": {"read_only": True, "allow_blank": True},
|
"created_by": {"read_only": True, "allow_blank": True},
|
||||||
"role": {"default": "User"},
|
"role": {"default": "User"},
|
||||||
"is_otp_secret_key_bound": {"label": _("Is OTP bound")},
|
"is_otp_secret_key_bound": {"label": _("Is OTP bound")},
|
||||||
"phone": {"validators": [PhoneValidator()]},
|
|
||||||
'mfa_level': {'label': _("MFA level")},
|
'mfa_level': {'label': _("MFA level")},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,14 +228,6 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
|
|||||||
attrs.pop("password_strategy", None)
|
attrs.pop("password_strategy", None)
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def to_representation(self, instance):
|
|
||||||
data = super().to_representation(instance)
|
|
||||||
phone = phonenumbers.parse(data['phone'], 'CN')
|
|
||||||
data['phone'] = {
|
|
||||||
'code': '+%s' % phone.country_code, 'phone': phone.national_number
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
|
|
||||||
def save_and_set_custom_m2m_fields(self, validated_data, save_handler, created):
|
def save_and_set_custom_m2m_fields(self, validated_data, save_handler, created):
|
||||||
m2m_values = {}
|
m2m_values = {}
|
||||||
for f, default_roles in self.custom_m2m_fields.items():
|
for f, default_roles in self.custom_m2m_fields.items():
|
||||||
|
Loading…
Reference in New Issue
Block a user