-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
45 lines (34 loc) · 1.4 KB
/
Copy pathpipeline.py
File metadata and controls
45 lines (34 loc) · 1.4 KB
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
from collections import OrderedDict
from datetime import datetime
from urllib.parse import urlencode, urlunparse
import requests
from django.utils import timezone
from social_core.exceptions import AuthForbidden
from authapp.models import ShopUserProfile
def save_user_profile(backend, user, response, *args, **kwargs):
if backend.name != 'vk-oauth2':
return
api_url = urlunparse(('https',
'api.vk.com',
'/method/users.get',
None,
urlencode(OrderedDict(fields=','.join(('bdate', 'sex', 'about')),
access_token=response['access_token'],
v='5.92')),
None
))
resp = requests.get(api_url)
if resp.status_code != 200:
return
data = resp.json()['response'][0]
if data['sex']:
user.shopuserprofile.gender = ShopUserProfile.MALE if data['sex'] == 2 else ShopUserProfile.FEMALE
if data['about']:
user.shopuserprofile.aboutMe = data['about']
if data['bdate']:
bdate = datetime.strptime(data['bdate'], '%d.%m.%Y').date()
age = timezone.now().date().year - bdate.year
if age < 18:
user.delete()
raise AuthForbidden('social_core.backends.vk.VKOAuth2')
user.save()