From c3bd434c2f399e252c6c0eee6230f17c3819a280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Aguirre?= Date: Wed, 23 Apr 2014 21:55:05 -0300 Subject: [PATCH] Refactor fullname, first name and last name generation. Fixes #240 --- social/backends/amazon.py | 6 ++---- social/backends/angel.py | 4 ++-- social/backends/appsfuel.py | 8 ++++++-- social/backends/base.py | 14 ++++++++++++++ social/backends/behance.py | 9 ++++++--- social/backends/bitbucket.py | 11 +++++++---- social/backends/box.py | 7 ++++++- social/backends/clef.py | 9 +++++++-- social/backends/coinbase.py | 7 +++---- social/backends/docker.py | 7 ++++++- social/backends/douban.py | 7 ++++++- social/backends/dropbox.py | 14 ++++++++++++-- social/backends/facebook.py | 11 ++++++++--- social/backends/flickr.py | 7 ++++++- social/backends/foursquare.py | 16 ++++++++++------ social/backends/github.py | 14 ++++++++++++-- social/backends/google.py | 12 +++++++++--- social/backends/instagram.py | 8 ++++++-- social/backends/jawbone.py | 7 +++++-- social/backends/lastfm.py | 7 ++++--- social/backends/legacy.py | 14 +++++--------- social/backends/linkedin.py | 8 +++++--- social/backends/live.py | 9 +++++++-- social/backends/mailru.py | 17 +++++++++-------- social/backends/mixcloud.py | 7 ++++--- social/backends/odnoklassniki.py | 22 ++++++++++++++++------ social/backends/orkut.py | 12 +++++++++--- social/backends/pixelpin.py | 8 +++++++- social/backends/podio.py | 5 +++-- social/backends/rdio.py | 11 ++++++++--- social/backends/readability.py | 9 +++++++-- social/backends/runkeeper.py | 7 ++++++- social/backends/skyrock.py | 10 +++++++--- social/backends/soundcloud.py | 9 +++------ social/backends/stackoverflow.py | 7 ++++++- social/backends/stocktwits.py | 10 ++++------ social/backends/strava.py | 6 +++++- social/backends/thisismyjam.py | 12 ++++++++---- social/backends/trello.py | 7 ++++++- social/backends/tripit.py | 8 ++------ social/backends/twitter.py | 8 ++------ social/backends/vimeo.py | 28 ++++++++++------------------ social/backends/vk.py | 21 ++++++++++++++------- social/backends/weibo.py | 7 ++++++- social/backends/xing.py | 7 +++++-- social/backends/yahoo.py | 12 +++++++----- social/backends/yammer.py | 10 ++++++---- social/backends/yandex.py | 16 ++++++++-------- 48 files changed, 322 insertions(+), 170 deletions(-) diff --git a/social/backends/amazon.py b/social/backends/amazon.py index fd2d207a6..c97b8a09f 100644 --- a/social/backends/amazon.py +++ b/social/backends/amazon.py @@ -22,12 +22,10 @@ class AmazonOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from amazon account""" name = response.get('name') or '' - first_name, last_name = name, '' - if name and ' ' in name: - first_name, last_name = name.split(' ', 1) + fullname, first_name, last_name = self.get_user_names(name) return {'username': name, 'email': response.get('email'), - 'fullname': name, + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name} diff --git a/social/backends/angel.py b/social/backends/angel.py index 738bef3a3..ad364972f 100644 --- a/social/backends/angel.py +++ b/social/backends/angel.py @@ -16,10 +16,10 @@ class AngelOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Angel account""" username = response['angellist_url'].split('/')[-1] - first_name = response['name'].split(' ')[0] - last_name = response['name'].split(' ')[-1] email = response.get('email', '') + fullname, first_name, last_name = self.get_user_names(response['name']) return {'username': username, + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name, 'email': email} diff --git a/social/backends/appsfuel.py b/social/backends/appsfuel.py index 691e69a2c..fbe086d64 100644 --- a/social/backends/appsfuel.py +++ b/social/backends/appsfuel.py @@ -15,12 +15,16 @@ class AppsfuelOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Appsfuel account""" - fullname = response.get('display_name', '') email = response.get('email', '') username = email.split('@')[0] if email else '' + fullname, first_name, last_name = self.get_user_names( + response.get('display_name', '') + ) return { 'username': username, - 'first_name': fullname, + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name, 'email': email } diff --git a/social/backends/base.py b/social/backends/base.py index 8edd0b1e4..f24a83f0a 100644 --- a/social/backends/base.py +++ b/social/backends/base.py @@ -150,6 +150,20 @@ def get_user_details(self, response): """ raise NotImplementedError('Implement in subclass') + def get_user_names(self, fullname='', first_name='', last_name=''): + # Avoid None values + fullname = fullname or '' + first_name = first_name or '' + last_name = last_name or '' + if fullname and not (first_name or last_name): + try: + first_name, last_name = fullname.split(' ', 1) + except ValueError: + first_name = first_name or fullname or '' + last_name = last_name or '' + fullname = fullname or ' '.join((first_name, last_name)) + return fullname.strip(), first_name.strip(), last_name.strip() + def get_user(self, user_id): """ Return user with given ID from the User model used by this backend. diff --git a/social/backends/behance.py b/social/backends/behance.py index 8f915820a..bf57de001 100644 --- a/social/backends/behance.py +++ b/social/backends/behance.py @@ -21,10 +21,13 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from Behance account""" user = response['user'] + fullname, first_name, last_name = self.get_user_names( + user['display_name'], user['first_name'], user['last_name'] + ) return {'username': user['username'], - 'last_name': user['last_name'], - 'first_name': user['first_name'], - 'fullname': user['display_name'], + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name, 'email': ''} def extra_data(self, user, uid, response, details): diff --git a/social/backends/bitbucket.py b/social/backends/bitbucket.py index 162c32716..e0d18e2e2 100644 --- a/social/backends/bitbucket.py +++ b/social/backends/bitbucket.py @@ -23,12 +23,15 @@ class BitbucketOAuth(BaseOAuth1): def get_user_details(self, response): """Return user details from Bitbucket account""" + fullname, first_name, last_name = self.get_user_names( + first_name=response.get('first_name', ''), + last_name=response.get('last_name', '') + ) return {'username': response.get('username') or '', 'email': response.get('email') or '', - 'fullname': ' '.join((response.get('first_name') or '', - response.get('last_name') or '')), - 'first_name': response.get('first_name') or '', - 'last_name': response.get('last_name') or ''} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token): """Return user data provided""" diff --git a/social/backends/box.py b/social/backends/box.py index 5b82f5d91..a13eaa26b 100644 --- a/social/backends/box.py +++ b/social/backends/box.py @@ -31,9 +31,14 @@ def do_auth(self, access_token, response=None, *args, **kwargs): def get_user_details(self, response): """Return user details Box.net account""" + fullname, first_name, last_name = self.get_user_names( + response.get('name') + ) return {'username': response.get('login'), 'email': response.get('login') or '', - 'first_name': response.get('name')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/clef.py b/social/backends/clef.py index f7e09bdd0..7a034db2c 100644 --- a/social/backends/clef.py +++ b/social/backends/clef.py @@ -26,11 +26,16 @@ def auth_params(self, *args, **kwargs): def get_user_details(self, response): """Return user details from Github account""" info = response.get('info') + fullname, first_name, last_name = self.get_user_names( + first_name=info.get('first_name'), + last_name=info.get('last_name') + ) return { 'username': response.get('clef_id'), 'email': info.get('email', ''), - 'first_name': info.get('first_name'), - 'last_name': info.get('last_name'), + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name, 'phone_number': info.get('phone_number', '') } diff --git a/social/backends/coinbase.py b/social/backends/coinbase.py index d7fd594ce..ae9106f92 100644 --- a/social/backends/coinbase.py +++ b/social/backends/coinbase.py @@ -20,12 +20,11 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from Coinbase account""" user_data = response['users'][0]['user'] - name = user_data['name'] - name_split = name.split() - first_name = name_split[0] - last_name = name_split[1] email = user_data.get('email', '') + name = user_data['name'] + fullname, first_name, last_name = self.get_user_names(name) return {'username': name, + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name, 'email': email} diff --git a/social/backends/docker.py b/social/backends/docker.py index b91512a7a..ea73dbd6a 100644 --- a/social/backends/docker.py +++ b/social/backends/docker.py @@ -26,9 +26,14 @@ class DockerOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Docker.io account""" + fullname, first_name, last_name = self.get_user_names( + response.get('full_name') or response.get('username') or '' + ) return { 'username': response.get('username'), - 'first_name': response.get('full_name', response.get('username')), + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name, 'email': response.get('email', '') } diff --git a/social/backends/douban.py b/social/backends/douban.py index 3f274534a..f5f1ae613 100644 --- a/social/backends/douban.py +++ b/social/backends/douban.py @@ -42,8 +42,13 @@ class DoubanOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Douban""" + fullname, first_name, last_name = self.get_user_names( + response.get('name', '') + ) return {'username': response.get('uid', ''), - 'fullname': response.get('name', ''), + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name, 'email': ''} def user_data(self, access_token, *args, **kwargs): diff --git a/social/backends/dropbox.py b/social/backends/dropbox.py index 698636334..a4e4baf5f 100644 --- a/social/backends/dropbox.py +++ b/social/backends/dropbox.py @@ -22,9 +22,14 @@ class DropboxOAuth(BaseOAuth1): def get_user_details(self, response): """Return user details from Dropbox account""" + fullname, first_name, last_name = self.get_user_names( + response.get('display_name') + ) return {'username': str(response.get('uid')), 'email': response.get('email'), - 'first_name': response.get('display_name')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" @@ -45,9 +50,14 @@ class DropboxOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Dropbox account""" + fullname, first_name, last_name = self.get_user_names( + response.get('display_name') + ) return {'username': str(response.get('uid')), 'email': response.get('email'), - 'first_name': response.get('display_name')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/facebook.py b/social/backends/facebook.py index c823c2624..01d8784dd 100644 --- a/social/backends/facebook.py +++ b/social/backends/facebook.py @@ -30,11 +30,16 @@ class FacebookOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Facebook account""" + fullname, first_name, last_name = self.get_user_names( + response.get('name', ''), + response.get('first_name', ''), + response.get('last_name', '') + ) return {'username': response.get('username', response.get('name')), 'email': response.get('email', ''), - 'fullname': response.get('name', ''), - 'first_name': response.get('first_name', ''), - 'last_name': response.get('last_name', '')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/flickr.py b/social/backends/flickr.py index 56aa4f6e8..cae8975c3 100644 --- a/social/backends/flickr.py +++ b/social/backends/flickr.py @@ -19,9 +19,14 @@ class FlickrOAuth(BaseOAuth1): def get_user_details(self, response): """Return user details from Flickr account""" + fullname, first_name, last_name = self.get_user_names( + response.get('fullname') + ) return {'username': response.get('username') or response.get('id'), 'email': '', - 'first_name': response.get('fullname')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/foursquare.py b/social/backends/foursquare.py index 84203720e..f6a5fba08 100644 --- a/social/backends/foursquare.py +++ b/social/backends/foursquare.py @@ -17,12 +17,16 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from Foursquare account""" - firstName = response['response']['user']['firstName'] - lastName = response['response']['user'].get('lastName', '') - email = response['response']['user']['contact']['email'] - return {'username': firstName + ' ' + lastName, - 'first_name': firstName, - 'last_name': lastName, + info = response['response']['user'] + email = info['contact']['email'] + fullname, first_name, last_name = self.get_user_names( + first_name=info.get('firstName', ''), + last_name=info.get('lastName', '') + ) + return {'username': first_name + ' ' + last_name, + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name, 'email': email} def user_data(self, access_token, *args, **kwargs): diff --git a/social/backends/github.py b/social/backends/github.py index 8d452296b..b41c0a4a4 100644 --- a/social/backends/github.py +++ b/social/backends/github.py @@ -21,9 +21,14 @@ class GithubOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Github account""" + fullname, first_name, last_name = self.get_user_names( + response.get('name') + ) return {'username': response.get('login'), 'email': response.get('email') or '', - 'first_name': response.get('name')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" @@ -50,9 +55,14 @@ class GithubOrganizationOAuth2(GithubOAuth2): def get_user_details(self, response): """Return user details from Github account""" + fullname, first_name, last_name = self.get_user_names( + response.get('name') + ) return {'username': response.get('login'), 'email': response.get('email') or '', - 'first_name': response.get('name')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/google.py b/social/backends/google.py index a9582d180..e1a03574c 100644 --- a/social/backends/google.py +++ b/social/backends/google.py @@ -20,11 +20,17 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from Orkut account""" email = response.get('email', '') + + fullname, first_name, last_name = self.get_user_names( + response.get('name', ''), + response.get('given_name', ''), + response.get('family_name', '') + ) return {'username': email.split('@', 1)[0], 'email': email, - 'fullname': response.get('name', ''), - 'first_name': response.get('given_name', ''), - 'last_name': response.get('family_name', '')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} class BaseGoogleOAuth2API(BaseGoogleAuth): diff --git a/social/backends/instagram.py b/social/backends/instagram.py index a6752ea4a..63eaf12ab 100644 --- a/social/backends/instagram.py +++ b/social/backends/instagram.py @@ -17,10 +17,14 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from Instagram account""" username = response['user']['username'] - fullname = response['user'].get('full_name', '') email = response['user'].get('email', '') + fullname, first_name, last_name = self.get_user_names( + response['user'].get('full_name', '') + ) return {'username': username, - 'first_name': fullname, + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name, 'email': email} def user_data(self, access_token, *args, **kwargs): diff --git a/social/backends/jawbone.py b/social/backends/jawbone.py index 34243429a..572dd162e 100644 --- a/social/backends/jawbone.py +++ b/social/backends/jawbone.py @@ -19,10 +19,13 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from Jawbone account""" data = response['data'] - first_name = data.get('first', '') - last_name = data.get('last', '') + fullname, first_name, last_name = self.get_user_names( + first_name=data.get('first', ''), + last_name=data.get('last', '') + ) return { 'username': first_name + ' ' + last_name, + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name, 'dob': data.get('dob', ''), diff --git a/social/backends/lastfm.py b/social/backends/lastfm.py index 565855089..c9ea837bc 100644 --- a/social/backends/lastfm.py +++ b/social/backends/lastfm.py @@ -47,10 +47,11 @@ def get_user_id(self, details, response): return response.get('name') def get_user_details(self, response): + fullname, first_name, last_name = self.get_user_names(response['name']) return { 'username': response['name'], 'email': '', - 'fullname': response['name'], - 'first_name': '', - 'last_name': '', + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name } diff --git a/social/backends/legacy.py b/social/backends/legacy.py index 3e67f3d04..0ea474060 100644 --- a/social/backends/legacy.py +++ b/social/backends/legacy.py @@ -28,15 +28,11 @@ def get_user_details(self, response): """Return user details""" email = response.get('email', '') username = response.get('username', '') - fullname = response.get('fullname', '') - first_name = response.get('first_name', '') - last_name = response.get('last_name', '') - if fullname and not (first_name or last_name): - try: - first_name, last_name = fullname.split(' ', 1) - except ValueError: - first_name = fullname - last_name = last_name or '' + fullname, first_name, last_name = self.get_user_names( + response.get('fullname', ''), + response.get('first_name', ''), + response.get('last_name', '') + ) if email and not username: username = email.split('@', 1)[0] return { diff --git a/social/backends/linkedin.py b/social/backends/linkedin.py index 634820cad..8180a6679 100644 --- a/social/backends/linkedin.py +++ b/social/backends/linkedin.py @@ -15,11 +15,13 @@ class BaseLinkedinAuth(object): def get_user_details(self, response): """Return user details from Linkedin account""" - first_name = response['firstName'] - last_name = response['lastName'] + fullname, first_name, last_name = self.get_user_names( + first_name=response['firstName'], + last_name=response['lastName'] + ) email = response.get('emailAddress', '') return {'username': first_name + last_name, - 'fullname': first_name + ' ' + last_name, + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name, 'email': email} diff --git a/social/backends/live.py b/social/backends/live.py index 497daebe3..35af9203b 100644 --- a/social/backends/live.py +++ b/social/backends/live.py @@ -26,10 +26,15 @@ class LiveOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Live Connect account""" + fullname, first_name, last_name = self.get_user_names( + first_name=response.get('first_name'), + last_name=response.get('last_name') + ) return {'username': response.get('name'), 'email': response.get('emails', {}).get('account', ''), - 'first_name': response.get('first_name'), - 'last_name': response.get('last_name')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/mailru.py b/social/backends/mailru.py index acce475af..7f9e17d4c 100644 --- a/social/backends/mailru.py +++ b/social/backends/mailru.py @@ -20,14 +20,15 @@ class MailruOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Mail.ru request""" - values = {'username': unquote(response['nick']), - 'email': unquote(response['email']), - 'first_name': unquote(response['first_name']), - 'last_name': unquote(response['last_name'])} - if values['first_name'] and values['last_name']: - values['fullname'] = ' '.join((values['first_name'], - values['last_name'])) - return values + fullname, first_name, last_name = self.get_user_names( + first_name=unquote(response['first_name']), + last_name=unquote(response['last_name']) + ) + return {'username': unquote(response['nick']), + 'email': unquote(response['email']), + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Return user data from Mail.ru REST API""" diff --git a/social/backends/mixcloud.py b/social/backends/mixcloud.py index 9cf9f84e6..61e1605bf 100644 --- a/social/backends/mixcloud.py +++ b/social/backends/mixcloud.py @@ -13,11 +13,12 @@ class MixcloudOAuth2(BaseOAuth2): ACCESS_TOKEN_METHOD = 'POST' def get_user_details(self, response): + fullname, first_name, last_name = self.get_user_names(response['name']) return {'username': response['username'], 'email': None, - 'fullname': response['name'], - 'first_name': None, - 'last_name': None} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): return self.get_json('https://api.mixcloud.com/me/', diff --git a/social/backends/odnoklassniki.py b/social/backends/odnoklassniki.py index 153db1362..f1422ee57 100644 --- a/social/backends/odnoklassniki.py +++ b/social/backends/odnoklassniki.py @@ -22,12 +22,17 @@ class OdnoklassnikiOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Odnoklassniki request""" + fullname, first_name, last_name = self.get_user_names( + fullname=unquote(response['name']), + first_name=unquote(response['first_name']), + last_name=unquote(response['last_name']) + ) return { 'username': response['uid'], 'email': '', - 'fullname': unquote(response['name']), - 'first_name': unquote(response['first_name']), - 'last_name': unquote(response['last_name']) + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name } def user_data(self, access_token, *args, **kwargs): @@ -49,12 +54,17 @@ def extra_data(self, user, uid, response, details): if key in response['extra_data_list']]) def get_user_details(self, response): + fullname, first_name, last_name = self.get_user_names( + fullname=unquote(response['name']), + first_name=unquote(response['first_name']), + last_name=unquote(response['last_name']) + ) return { 'username': response['uid'], 'email': '', - 'fullname': unquote(response['name']), - 'first_name': unquote(response['first_name']), - 'last_name': unquote(response['last_name']) + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name } def auth_complete(self, request, user, *args, **kwargs): diff --git a/social/backends/orkut.py b/social/backends/orkut.py index 00b4d6001..aa5053c14 100644 --- a/social/backends/orkut.py +++ b/social/backends/orkut.py @@ -16,11 +16,17 @@ def get_user_details(self, response): emails = response['emails'][0]['value'] except (KeyError, IndexError): emails = '' + + fullname, first_name, last_name = self.get_user_names( + fullname=response['displayName'], + first_name=response['name']['givenName'], + last_name=response['name']['familyName'] + ) return {'username': response['displayName'], 'email': emails, - 'fullname': response['displayName'], - 'first_name': response['name']['givenName'], - 'last_name': response['name']['familyName']} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from Orkut service""" diff --git a/social/backends/pixelpin.py b/social/backends/pixelpin.py index b2e224145..3a1ed2b3f 100644 --- a/social/backends/pixelpin.py +++ b/social/backends/pixelpin.py @@ -15,9 +15,15 @@ class PixelPinOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from PixelPin account""" + fullname, first_name, last_name = self.get_user_names( + first_name=response.get('firstName'), + last_name=response.get('lastName') + ) return {'username': response.get('firstName'), 'email': response.get('email') or '', - 'first_name': response.get('firstName')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/podio.py b/social/backends/podio.py index 46af3068c..1c4b0df44 100644 --- a/social/backends/podio.py +++ b/social/backends/podio.py @@ -22,8 +22,9 @@ def get_user_id(self, details, response): return response['ref']['id'] def get_user_details(self, response): - fullname = response['profile']['name'] - first_name, _, last_name = fullname.partition(' ') + fullname, first_name, last_name = self.get_user_names( + response['profile']['name'] + ) return { 'username': 'user_%d' % response['user']['user_id'], 'email': response['user']['mail'], diff --git a/social/backends/rdio.py b/social/backends/rdio.py index 51e475b7a..bce5fd648 100644 --- a/social/backends/rdio.py +++ b/social/backends/rdio.py @@ -12,11 +12,16 @@ class BaseRdio(OAuthAuth): ID_KEY = 'key' def get_user_details(self, response): + fullname, first_name, last_name = self.get_user_names( + fullname=response['displayName'], + first_name=response['firstName'], + last_name=response['lastName'] + ) return { 'username': response['username'], - 'first_name': response['firstName'], - 'last_name': response['lastName'], - 'fullname': response['displayName'], + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name } diff --git a/social/backends/readability.py b/social/backends/readability.py index 72f7f785a..76e241f15 100644 --- a/social/backends/readability.py +++ b/social/backends/readability.py @@ -21,9 +21,14 @@ class ReadabilityOAuth(BaseOAuth1): ('email_into_address', 'email_into_address')] def get_user_details(self, response): + fullname, first_name, last_name = self.get_user_names( + first_name=response['first_name'], + last_name=response['last_name'] + ) return {'username': response['username'], - 'first_name': response['first_name'], - 'last_name': response['last_name']} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token): return self.get_json(READABILITY_API + '/users/_current', diff --git a/social/backends/runkeeper.py b/social/backends/runkeeper.py index 2ecb0a06a..d7a2f3bf5 100644 --- a/social/backends/runkeeper.py +++ b/social/backends/runkeeper.py @@ -26,9 +26,14 @@ def get_user_details(self, response): profile_url_parts = profile_url.split('http://runkeeper.com/user/') if len(profile_url_parts) > 1 and len(profile_url_parts[1]): username = profile_url_parts[1] + fullname, first_name, last_name = self.get_user_names( + fullname=response.get('name') + ) return {'username': username, 'email': response.get('email') or '', - 'first_name': response.get('name')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): # We need to use the /user endpoint to get the user id, the /profile diff --git a/social/backends/skyrock.py b/social/backends/skyrock.py index 3492e3155..b027d94ac 100644 --- a/social/backends/skyrock.py +++ b/social/backends/skyrock.py @@ -16,11 +16,15 @@ class SkyrockOAuth(BaseOAuth1): def get_user_details(self, response): """Return user details from Skyrock account""" + fullname, first_name, last_name = self.get_user_names( + first_name=response['firstname'], + last_name=response['name'] + ) return {'username': response['username'], 'email': response['email'], - 'fullname': response['firstname'] + ' ' + response['name'], - 'first_name': response['firstname'], - 'last_name': response['name']} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token): """Return user data provided""" diff --git a/social/backends/soundcloud.py b/social/backends/soundcloud.py index 2c43234cd..ab41abd44 100644 --- a/social/backends/soundcloud.py +++ b/social/backends/soundcloud.py @@ -22,12 +22,9 @@ class SoundcloudOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Soundcloud account""" - fullname = response.get('full_name') - full_name = fullname.split(' ') - first_name = full_name[0] - last_name = '' - if len(full_name) > 1: - last_name = full_name[-1] + fullname, first_name, last_name = self.get_user_names( + response.get('full_name') + ) return {'username': response.get('username'), 'email': response.get('email') or '', 'fullname': fullname, diff --git a/social/backends/stackoverflow.py b/social/backends/stackoverflow.py index 5ddd0d103..1da1fa9ca 100644 --- a/social/backends/stackoverflow.py +++ b/social/backends/stackoverflow.py @@ -20,8 +20,13 @@ class StackoverflowOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from Stackoverflow account""" + fullname, first_name, last_name = self.get_user_names( + response.get('display_name') + ) return {'username': response.get('link').rsplit('/', 1)[-1], - 'full_name': response.get('display_name')} + 'full_name': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/stocktwits.py b/social/backends/stocktwits.py index 601272f31..55fee95ab 100644 --- a/social/backends/stocktwits.py +++ b/social/backends/stocktwits.py @@ -20,14 +20,12 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from Stocktwits account""" - try: - first_name, last_name = response['user']['name'].split(' ', 1) - except: - first_name = response['user']['name'] - last_name = '' + fullname, first_name, last_name = self.get_user_names( + response['user']['name'] + ) return {'username': response['user']['username'], 'email': '', # not supplied - 'fullname': response['user']['name'], + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name} diff --git a/social/backends/strava.py b/social/backends/strava.py index e80c8a632..1c903d21f 100644 --- a/social/backends/strava.py +++ b/social/backends/strava.py @@ -18,10 +18,14 @@ def get_user_details(self, response): """Return user details from Strava account""" # because there is no usernames on strava username = response['athlete']['id'] - first_name = response['athlete'].get('first_name', '') email = response['athlete'].get('email', '') + fullname, first_name, last_name = self.get_user_names( + first_name=response['athlete'].get('first_name', '') + ) return {'username': str(username), + 'fullname': fullname, 'first_name': first_name, + 'last_name': last_name, 'email': email} def user_data(self, access_token, *args, **kwargs): diff --git a/social/backends/thisismyjam.py b/social/backends/thisismyjam.py index 9ee5094f8..994691f7d 100644 --- a/social/backends/thisismyjam.py +++ b/social/backends/thisismyjam.py @@ -15,12 +15,16 @@ class ThisIsMyJamOAuth1(BaseOAuth1): def get_user_details(self, response): """Return user details from ThisIsMyJam account""" + info = response.get('person') + fullname, first_name, last_name = self.get_user_names( + info.get('fullname') + ) return { - 'username': response.get('person').get('name'), - 'fullname': response.get('person').get('fullname'), + 'username': info.get('name'), 'email': '', - 'first_name': '', - 'last_name': '' + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name } def user_data(self, access_token, *args, **kwargs): diff --git a/social/backends/trello.py b/social/backends/trello.py index cc069acf2..73eaa916d 100644 --- a/social/backends/trello.py +++ b/social/backends/trello.py @@ -22,9 +22,14 @@ class TrelloOAuth(BaseOAuth1): def get_user_details(self, response): """Return user details from Trello account""" + fullname, first_name, last_name = self.get_user_names( + response.get('fullName') + ) return {'username': response.get('username'), 'email': response.get('email'), - 'fullName': response.get('fullName')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token): """Return user data provided""" diff --git a/social/backends/tripit.py b/social/backends/tripit.py index 566400d00..ac09334be 100644 --- a/social/backends/tripit.py +++ b/social/backends/tripit.py @@ -17,14 +17,10 @@ class TripItOAuth(BaseOAuth1): def get_user_details(self, response): """Return user details from TripIt account""" - try: - first_name, last_name = response['name'].split(' ', 1) - except ValueError: - first_name = response['name'] - last_name = '' + fullname, first_name, last_name = self.get_user_names(response['name']) return {'username': response['screen_name'], 'email': response['email'], - 'fullname': response['name'], + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name} diff --git a/social/backends/twitter.py b/social/backends/twitter.py index ca351f813..c096a3390 100644 --- a/social/backends/twitter.py +++ b/social/backends/twitter.py @@ -22,14 +22,10 @@ def process_error(self, data): def get_user_details(self, response): """Return user details from Twitter account""" - try: - first_name, last_name = response['name'].split(' ', 1) - except: - first_name = response['name'] - last_name = '' + fullname, first_name, last_name = self.get_user_names(response['name']) return {'username': response['screen_name'], 'email': '', # not supplied - 'fullname': response['name'], + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name} diff --git a/social/backends/vimeo.py b/social/backends/vimeo.py index f08b637e9..85d236f6a 100644 --- a/social/backends/vimeo.py +++ b/social/backends/vimeo.py @@ -14,11 +14,9 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from Twitter account""" person = response.get('person', {}) - fullname = person.get('display_name', '') - if ' ' in fullname: - first_name, last_name = fullname.split(' ', 1) - else: - first_name, last_name = fullname, '' + fullname, first_name, last_name = self.get_user_names( + person.get('display_name', '') + ) return {'username': person.get('username', ''), 'email': '', 'fullname': fullname, @@ -38,12 +36,11 @@ class VimeoOAuth2(BaseOAuth2): """Vimeo OAuth2 authentication backend""" name = 'vimeo-oauth2' AUTHORIZATION_URL = 'https://api.vimeo.com/oauth/authorize' - ACCESS_TOKEN_URL = 'https://api.vimeo.com/oauth/access_token' + ACCESS_TOKEN_URL = 'https://api.vimeo.com/oauth/access_token' REFRESH_TOKEN_URL = 'https://api.vimeo.com/oauth/request_token' ACCESS_TOKEN_METHOD = 'POST' SCOPE_SEPARATOR = ',' - - API_ACCEPT_HEADER = {'Accept' : 'application/vnd.vimeo.*+json;version=3.0'} + API_ACCEPT_HEADER = {'Accept': 'application/vnd.vimeo.*+json;version=3.0'} def get_redirect_uri(self, state=None): """ @@ -65,23 +62,18 @@ def get_user_id(self, details, response): def get_user_details(self, response): """Return user details from account""" user = response.get('user', {}) - fullname = user.get('name', '') - - if ' ' in fullname: - first_name, last_name = fullname.split(' ', 1) - else: - first_name, last_name = fullname, '' - + fullname, first_name, last_name = self.get_user_names( + user.get('name', '') + ) return {'username': fullname, 'fullname': fullname, 'first_name': first_name, - 'last_name': last_name,} + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Return user data provided""" return self.get_json( 'https://api.vimeo.com/me', - params={'access_token' : access_token}, + params={'access_token': access_token}, headers=VimeoOAuth2.API_ACCEPT_HEADER, ) - diff --git a/social/backends/vk.py b/social/backends/vk.py index 526c12cb4..d27e29a88 100644 --- a/social/backends/vk.py +++ b/social/backends/vk.py @@ -20,14 +20,16 @@ class VKontakteOpenAPI(BaseAuth): def get_user_details(self, response): """Return user details from VK.com request""" nickname = response.get('nickname') or '' + fullname, first_name, last_name = self.get_user_names( + first_name=response.get('first_name', [''])[0], + last_name=response.get('last_name', [''])[0] + ) return { 'username': response['id'] if len(nickname) == 0 else nickname, 'email': '', - 'fullname': '', - 'first_name': response.get('first_name')[0] - if 'first_name' in response else '', - 'last_name': response.get('last_name')[0] - if 'last_name' in response else '' + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name } def user_data(self, access_token, *args, **kwargs): @@ -85,10 +87,15 @@ class VKOAuth2(BaseOAuth2): def get_user_details(self, response): """Return user details from VK.com account""" + fullname, first_name, last_name = self.get_user_names( + first_name=response.get('first_name'), + last_name=response.get('last_name') + ) return {'username': response.get('screen_name'), 'email': '', - 'first_name': response.get('first_name'), - 'last_name': response.get('last_name')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, response, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/weibo.py b/social/backends/weibo.py index 1e287c9ef..b779520fb 100644 --- a/social/backends/weibo.py +++ b/social/backends/weibo.py @@ -31,8 +31,13 @@ def get_user_details(self, response): username = response.get('domain', '') else: username = response.get('name', '') + fullname, first_name, last_name = self.get_user_names( + first_name=response.get('screen_name', '') + ) return {'username': username, - 'first_name': response.get('screen_name', '')} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): return self.get_json('https://api.weibo.com/2/users/show.json', diff --git a/social/backends/xing.py b/social/backends/xing.py index 574690190..915514207 100644 --- a/social/backends/xing.py +++ b/social/backends/xing.py @@ -19,10 +19,13 @@ class XingOAuth(BaseOAuth1): def get_user_details(self, response): """Return user details from Xing account""" - first_name, last_name = response['first_name'], response['last_name'] email = response.get('email', '') + fullname, first_name, last_name = self.get_user_names( + first_name=response['first_name'], + last_name=response['last_name'] + ) return {'username': first_name + last_name, - 'fullname': first_name + ' ' + last_name, + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name, 'email': email} diff --git a/social/backends/yahoo.py b/social/backends/yahoo.py index c1590a527..746b9b37f 100644 --- a/social/backends/yahoo.py +++ b/social/backends/yahoo.py @@ -28,16 +28,18 @@ class YahooOAuth(BaseOAuth1): def get_user_details(self, response): """Return user details from Yahoo Profile""" - fname = response.get('givenName') - lname = response.get('familyName') + fullname, first_name, last_name = self.get_user_names( + first_name=response.get('givenName'), + last_name=response.get('familyName') + ) emails = [email for email in response.get('emails', []) if email.get('handle')] emails.sort(key=lambda e: e.get('primary', False)) return {'username': response.get('nickname'), 'email': emails[0]['handle'] if emails else '', - 'fullname': '{0} {1}'.format(fname, lname), - 'first_name': fname, - 'last_name': lname} + 'fullname': fullname, + 'first_name': first_name, + 'last_name': last_name} def user_data(self, access_token, *args, **kwargs): """Loads user data from service""" diff --git a/social/backends/yammer.py b/social/backends/yammer.py index adcdde6f2..f52e28cf0 100644 --- a/social/backends/yammer.py +++ b/social/backends/yammer.py @@ -20,15 +20,17 @@ def get_user_id(self, details, response): def get_user_details(self, response): username = response['user']['name'] - first_name = response['user']['first_name'] - last_name = response['user']['last_name'] - full_name = response['user']['full_name'] + fullname, first_name, last_name = self.get_user_names( + fullname=response['user']['full_name'], + first_name=response['user']['first_name'], + last_name=response['user']['last_name'] + ) email = response['user']['contact']['email_addresses'][0]['address'] mugshot_url = response['user']['mugshot_url'] return { 'username': username, 'email': email, - 'fullname': full_name, + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name, 'picture_url': mugshot_url diff --git a/social/backends/yandex.py b/social/backends/yandex.py index 89cf3b5cb..144ee5cd7 100644 --- a/social/backends/yandex.py +++ b/social/backends/yandex.py @@ -38,13 +38,13 @@ class YandexOAuth2(BaseOAuth2): REDIRECT_STATE = False def get_user_details(self, response): - first_name = response.get('real_name') or response.get('display_name') - last_name = '' - if ' ' in first_name: - first_name, last_name = first_name.split(' ', 1) + fullname, first_name, last_name = self.get_user_names( + response.get('real_name') or response.get('display_name') or '' + ) return {'username': response.get('display_name'), 'email': response.get('default_email') or response.get('emails', [''])[0], + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name} @@ -62,13 +62,13 @@ class YaruOAuth2(BaseOAuth2): REDIRECT_STATE = False def get_user_details(self, response): - first_name = response.get('real_name') or response.get('display_name') - last_name = '' - if ' ' in first_name: - first_name, last_name = first_name.split(' ', 1) + fullname, first_name, last_name = self.get_user_names( + response.get('real_name') or response.get('display_name') or '' + ) return {'username': response.get('display_name'), 'email': response.get('default_email') or response.get('emails', [''])[0], + 'fullname': fullname, 'first_name': first_name, 'last_name': last_name}