| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.socialaccount import app_settings |
| 4 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 5 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 6 |
from allauth.socialaccount.providers.vk.views import VKOAuth2Adapter |
| 7 |
|
| 8 |
|
| 9 |
class VKAccount(ProviderAccount): |
| 10 |
def get_profile_url(self): |
| 11 |
return f"https://vk.ru/id{self.account.extra_data.get('id')}" |
| 12 |
|
| 13 |
def get_avatar_url(self): |
| 14 |
ret = None |
| 15 |
photo_big_url = self.account.extra_data.get("photo_big") |
| 16 |
photo_medium_url = self.account.extra_data.get("photo_medium") |
| 17 |
if photo_big_url: |
| 18 |
return photo_big_url |
| 19 |
elif photo_medium_url: |
| 20 |
return photo_medium_url |
| 21 |
else: |
| 22 |
return ret |
| 23 |
|
| 24 |
|
| 25 |
class VKProvider(OAuth2Provider): |
| 26 |
id = "vk" |
| 27 |
name = "VK" |
| 28 |
account_class = VKAccount |
| 29 |
oauth2_adapter_class = VKOAuth2Adapter |
| 30 |
pkce_enabled_default = True |
| 31 |
|
| 32 |
def get_default_scope(self): |
| 33 |
scope = [] |
| 34 |
if app_settings.QUERY_EMAIL: |
| 35 |
scope.append("email") |
| 36 |
return scope |
| 37 |
|
| 38 |
def extract_uid(self, data): |
| 39 |
return str(data["user_id"]) |
| 40 |
|
| 41 |
def extract_common_fields(self, data): |
| 42 |
return dict( |
| 43 |
email=data.get("email"), |
| 44 |
last_name=data.get("last_name"), |
| 45 |
username=data.get("screen_name"), |
| 46 |
first_name=data.get("first_name"), |
| 47 |
phone=data.get("phone"), |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
provider_classes = [VKProvider] |