From 4910cfa1808a00fac08f296fb57dbb69a16d51bd Mon Sep 17 00:00:00 2001 From: Matt Tuchfarber Date: Thu, 7 May 2020 10:10:42 -0400 Subject: [PATCH] Strip non-numeric characters from phone number Allows user to type in phone number in whichever format they prefer. --- .../core/djangoapps/user_api/accounts/serializers.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/openedx/core/djangoapps/user_api/accounts/serializers.py b/openedx/core/djangoapps/user_api/accounts/serializers.py index 3d9a6975b2..0caa1314a6 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