Skip to content

Commit

Permalink
Refactor fullname, first name and last name generation. Fixes #240
Browse files Browse the repository at this point in the history
  • Loading branch information
omab committed Apr 24, 2014
1 parent a3eb8e6 commit c3bd434
Show file tree
Hide file tree
Showing 48 changed files with 322 additions and 170 deletions.
6 changes: 2 additions & 4 deletions social/backends/amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
4 changes: 2 additions & 2 deletions social/backends/angel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 6 additions & 2 deletions social/backends/appsfuel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions social/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions social/backends/behance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 7 additions & 4 deletions social/backends/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
7 changes: 6 additions & 1 deletion social/backends/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
9 changes: 7 additions & 2 deletions social/backends/clef.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')
}

Expand Down
7 changes: 3 additions & 4 deletions social/backends/coinbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
7 changes: 6 additions & 1 deletion social/backends/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')
}

Expand Down
7 changes: 6 additions & 1 deletion social/backends/douban.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 12 additions & 2 deletions social/backends/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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"""
Expand Down
11 changes: 8 additions & 3 deletions social/backends/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
7 changes: 6 additions & 1 deletion social/backends/flickr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
16 changes: 10 additions & 6 deletions social/backends/foursquare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 12 additions & 2 deletions social/backends/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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"""
Expand Down
12 changes: 9 additions & 3 deletions social/backends/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 6 additions & 2 deletions social/backends/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 5 additions & 2 deletions social/backends/jawbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', ''),
Expand Down
7 changes: 4 additions & 3 deletions social/backends/lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 5 additions & 9 deletions social/backends/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit c3bd434

Please sign in to comment.