diff --git a/openedx/core/djangoapps/user_api/accounts/serializers.py b/openedx/core/djangoapps/user_api/accounts/serializers.py index 3d9a6975b290f09f87a0e895141d4849404da5d3..0caa1314a6ad17bfd5dd621fa6d5f1d6f36d26ec 100644 --- a/openedx/core/djangoapps/user_api/accounts/serializers.py +++ b/openedx/core/djangoapps/user_api/accounts/serializers.py @@ -5,6 +5,7 @@ Django REST Framework serializers for the User API Accounts sub-application import json import logging +import re from django.conf import settings from django.contrib.auth.models import User @@ -37,6 +38,15 @@ PROFILE_IMAGE_KEY_PREFIX = 'image_url' LOGGER = logging.getLogger(__name__) +class PhoneNumberSerializer(serializers.BaseSerializer): + """ + Class to serialize phone number into a digit only representation + """ + def to_internal_value(self, data): + """Remove all non numeric characters in phone number""" + return re.sub("[^0-9]", "", data) or None + + class LanguageProficiencySerializer(serializers.ModelSerializer): """ Class that serializes the LanguageProficiency model for account @@ -223,6 +233,7 @@ class AccountLegacyProfileSerializer(serializers.HyperlinkedModelSerializer, Rea requires_parental_consent = serializers.SerializerMethodField() language_proficiencies = LanguageProficiencySerializer(many=True, required=False) social_links = SocialLinkSerializer(many=True, required=False) + phone_number = PhoneNumberSerializer(required=False) class Meta(object): model = UserProfile