Strip non-numeric characters from phone number

Allows user to type in phone number in whichever
format they prefer.
This commit is contained in:
Matt Tuchfarber
2020-05-07 10:10:42 -04:00
parent 5caf9b02fa
commit 4910cfa180

View File

@@ -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