-
Notifications
You must be signed in to change notification settings - Fork 113
/
pipeline.py
executable file
·85 lines (67 loc) · 2.8 KB
/
pipeline.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import requests
import simplejson as json
from datetime import datetime
from django.utils.translation import ugettext_lazy as _
from social.apps.django_app.default.models import UserSocialAuth
from social.exceptions import AuthFailed
from ..settings import EMAIL_CONFIRMATION
def create_user(backend, details, response, uid, username, user=None,
*args, **kwargs):
"""Create user. Depends on get_username pipeline."""
if user:
return {'user': user}
if not username:
return None
# Avoid hitting field max length
email = details.get('email')
original_email = None
if not email:
message = _("""your social account needs to have a verified email address in order to proceed.""")
raise AuthFailed(backend, message)
if email and len(email) <= 54:
original_email = email
email = ''
return {
'user': UserSocialAuth.create_user(username=username, email=email,
sync_emailaddress=False),
'original_email': original_email,
'is_new': True
}
def load_extra_data(backend, details, response, uid, user, social_user=None,
*args, **kwargs):
"""Load extra data from provider and store it on current UserSocialAuth
extra_data field.
"""
social_user = social_user or UserSocialAuth.get_social_auth(backend.name, uid)
if kwargs['is_new'] and EMAIL_CONFIRMATION:
if user.email:
from ..models import EmailAddress
emailaddress = EmailAddress(**{
'user': user,
'email': user.email,
'verified': True,
'primary': True
})
emailaddress.save()
if social_user:
extra_data = backend.extra_data(user, uid, response, details)
if kwargs.get('original_email') and 'email' not in extra_data:
extra_data['email'] = kwargs.get('original_email')
if extra_data and social_user.extra_data != extra_data:
if social_user.extra_data:
social_user.extra_data.update(extra_data)
else:
social_user.extra_data = extra_data
social_user.save()
if backend.name == 'facebook' and kwargs['is_new']:
response = json.loads(requests.get('https://graph.facebook.com/%s?access_token=%s' % (extra_data['id'], extra_data['access_token'])).content)
try:
user.city, user.country = response.get('hometown').get('name').split(', ')
except AttributeError:
pass
try:
user.birth_date = datetime.strptime(response.get('birthday'), '%m/%d/%Y').date()
except AttributeError:
pass
user.save()
return {'social_user': social_user}