Add phone number to account API

This is so it's user editable in the Account MFE.
This commit is contained in:
Matt Tuchfarber
2020-02-26 15:06:38 -05:00
parent d93ef18907
commit 82024fbf77
6 changed files with 20 additions and 5 deletions

View File

@@ -503,7 +503,7 @@ class UserProfile(models.Model):
allow_certificate = models.BooleanField(default=1)
bio = models.CharField(blank=True, null=True, max_length=3000, db_index=False)
profile_image_uploaded_at = models.DateTimeField(null=True, blank=True)
phone_regex = RegexValidator(regex=r'^\+?1?\d*$', message="Phone number can only contain numbers.")
phone_regex = RegexValidator(regex=r'^\+?\d*$', message="Phone number can only contain numbers.")
phone_number = models.CharField(validators=[phone_regex], blank=True, null=True, max_length=50)
@property

View File

@@ -3264,6 +3264,7 @@ ACCOUNT_VISIBILITY_CONFIGURATION["admin_fields"] = (
"requires_parental_consent",
"secondary_email",
"year_of_birth",
"phone_number",
]
)

View File

@@ -140,6 +140,7 @@ class UserReadOnlySerializer(serializers.Serializer):
"account_privacy": self.configuration.get('default_visibility'),
"social_links": None,
"extended_profile_fields": None,
"phone_number": None,
}
if user_profile:
@@ -167,6 +168,7 @@ class UserReadOnlySerializer(serializers.Serializer):
user_profile.social_links.all().order_by('platform'), many=True
).data,
"extended_profile": get_extended_profile(user_profile),
"phone_number": user_profile.phone_number,
}
)
@@ -226,7 +228,8 @@ class AccountLegacyProfileSerializer(serializers.HyperlinkedModelSerializer, Rea
model = UserProfile
fields = (
"name", "gender", "goals", "year_of_birth", "level_of_education", "country", "social_links",
"mailing_address", "bio", "profile_image", "requires_parental_consent", "language_proficiencies"
"mailing_address", "bio", "profile_image", "requires_parental_consent", "language_proficiencies",
"phone_number"
)
# Currently no read-only field, but keep this so view code doesn't need to know.
read_only_fields = ()
@@ -292,6 +295,12 @@ class AccountLegacyProfileSerializer(serializers.HyperlinkedModelSerializer, Rea
"""
return AccountLegacyProfileSerializer.convert_empty_to_None(value)
def transform_phone_number(self, user_profile, value): # pylint: disable=unused-argument
"""
Converts empty string to None, to indicate not set. Replaced by to_representation in version 3.
"""
return AccountLegacyProfileSerializer.convert_empty_to_None(value)
@staticmethod
def convert_empty_to_None(value):
"""

View File

@@ -536,6 +536,7 @@ class AccountSettingsOnCreationTest(CreateAccountMixin, TestCase):
'secondary_email': None,
'time_zone': None,
'course_certificates': None,
'phone_number': None,
})
def test_normalize_password(self):

View File

@@ -115,6 +115,7 @@ class UserAPITestCase(APITestCase):
legacy_profile.bio = TEST_BIO_VALUE
legacy_profile.profile_image_uploaded_at = TEST_PROFILE_IMAGE_UPLOADED_AT
legacy_profile.language_proficiencies.create(code=TEST_LANGUAGE_PROFICIENCY_CODE)
legacy_profile.phone_number = "+18005555555"
legacy_profile.save()
def _verify_profile_image_data(self, data, has_profile_image):
@@ -265,7 +266,7 @@ class TestAccountsAPI(CacheIsolationTestCase, UserAPITestCase):
Verify that all account fields are returned (even those that are not shareable).
"""
data = response.data
self.assertEqual(22, len(data))
self.assertEqual(23, len(data))
# public fields (3)
expected_account_privacy = (
@@ -474,7 +475,7 @@ class TestAccountsAPI(CacheIsolationTestCase, UserAPITestCase):
with self.assertNumQueries(queries):
response = self.send_get(self.client)
data = response.data
self.assertEqual(22, len(data))
self.assertEqual(23, len(data))
self.assertEqual(self.user.username, data["username"])
self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"])
for empty_field in ("year_of_birth", "level_of_education", "mailing_address", "bio"):
@@ -885,7 +886,7 @@ class TestAccountsAPI(CacheIsolationTestCase, UserAPITestCase):
response = self.send_get(client)
if has_full_access:
data = response.data
self.assertEqual(22, len(data))
self.assertEqual(23, len(data))
self.assertEqual(self.user.username, data["username"])
self.assertEqual(self.user.first_name + " " + self.user.last_name, data["name"])
self.assertEqual(self.user.email, data["email"])

View File

@@ -227,6 +227,9 @@ class AccountViewSet(ViewSet):
* accomplishments_shared: Signals whether badges are enabled on the
platform and should be fetched.
* phone_number: The phone number for the user. String of numbers with
an optional `+` sign at the start.
For all text fields, plain text instead of HTML is supported. The
data is stored exactly as specified. Clients must HTML escape
rendered values to avoid script injections.