endpoints/api: Allow null fields in user metadata

The user metadata fields are nullable in the database, but were not in
the json sechema.  This prevented users from updating some of their
information on the site if they hadn't set the metadata fields.
This commit is contained in:
Brad Ison 2018-06-26 16:07:59 -04:00
parent f32bbf1fdc
commit 73cb7f3228
No known key found for this signature in database
GPG key ID: 972D14B0BE6DE287
6 changed files with 71 additions and 27 deletions

View file

@ -236,19 +236,19 @@ class User(ApiResource):
'description': 'Custom email address for receiving invoices',
},
'given_name': {
'type': 'string',
'type': ['string', 'null'],
'description': 'The optional entered given name for the user',
},
'family_name': {
'type': 'string',
'type': ['string', 'null'],
'description': 'The optional entered family name for the user',
},
'company': {
'type': 'string',
'type': ['string', 'null'],
'description': 'The optional entered company for the user',
},
'location': {
'type': 'string',
'type': ['string', 'null'],
'description': 'The optional entered location for the user',
},
},
@ -367,22 +367,16 @@ class User(ApiResource):
model.user.update_email(user, new_email, auto_verify=not features.MAILING)
if features.USER_METADATA:
metadata_fields = ('given_name', 'family_name', 'company', 'location')
if any(field in user_data for field in metadata_fields):
model.user.update_user_metadata(user, {
'given_name': user_data.get('given_name'),
'family_name': user_data.get('family_name'),
'company': user_data.get('company'),
'location': user_data.get('location'),
})
metadata = {}
ua_mdata_future = user_analytics.change_metadata(
user.email,
user_data.get('given_name'),
user_data.get('family_name'),
user_data.get('company'),
user_data.get('location'),
)
for field in ('given_name', 'family_name', 'company', 'location'):
if field in user_data:
metadata[field] = user_data.get(field)
if len(metadata) > 0:
model.user.update_user_metadata(user, metadata)
ua_mdata_future = user_analytics.change_metadata(user.email, **metadata)
ua_mdata_future.add_done_callback(build_error_callback('Change metadata failed'))
# Check for username rename. A username can be renamed if the feature is enabled OR the user