-
Notifications
You must be signed in to change notification settings - Fork 12
/
functions.py
59 lines (48 loc) · 1.99 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
from urllib.parse import urlencode
from urllib.request import urlopen
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
from django.conf import settings
def get_token():
client = BackendApplicationClient(client_id=settings.SOCIAL_AUTH_OPENSTAX_KEY)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=settings.ACCESS_TOKEN_URL,
client_id=settings.SOCIAL_AUTH_OPENSTAX_KEY,
client_secret=settings.SOCIAL_AUTH_OPENSTAX_SECRET)
return token
def get_user_info(uid=None):
if uid:
token = get_token()
url = settings.USERS_QUERY + urlencode({
'q': 'id:{}'.format(uid),
'access_token': token['access_token']
})
with urlopen(url) as url:
data = json.loads(url.read().decode())
# update email address if possible
try:
contact_infos = data['items'][0]['contact_infos']
most_recent_email = max(contact_infos, key=lambda x: x['id'])
email = most_recent_email['value']
except (ValueError, IndexError, KeyError):
email = None # no saved emails
# update full name if possible
try:
fullname = data['items'][0]['full_name']
except (ValueError, IndexError, KeyError):
fullname = None
try:
user_data = {
'faculty_status': data['items'][0]['faculty_status'],
'email': email,
'self_reported_role': data['items'][0]['self_reported_role'],
'faculty_status': data['items'][0]['faculty_status'],
'applications': data['items'][0]['applications'],
'fullname': fullname
}
except IndexError:
return False
return user_data
else:
return False