| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.account.models import EmailAddress |
| 4 |
from allauth.socialaccount import app_settings |
| 5 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 6 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 7 |
from allauth.socialaccount.providers.yandex.views import YandexOAuth2Adapter |
| 8 |
|
| 9 |
|
| 10 |
class YandexAccount(ProviderAccount): |
| 11 |
def to_str(self): |
| 12 |
email = self.account.extra_data.get("default_email") |
| 13 |
if email: |
| 14 |
return email |
| 15 |
return super().to_str() |
| 16 |
|
| 17 |
|
| 18 |
class YandexProvider(OAuth2Provider): |
| 19 |
id = "yandex" |
| 20 |
name = "Yandex" |
| 21 |
account_class = YandexAccount |
| 22 |
oauth2_adapter_class = YandexOAuth2Adapter |
| 23 |
|
| 24 |
def get_default_scope(self): |
| 25 |
scope = ["login:info"] |
| 26 |
if app_settings.QUERY_EMAIL: |
| 27 |
scope.append("login:email") |
| 28 |
return scope |
| 29 |
|
| 30 |
def extract_uid(self, data): |
| 31 |
return str(data["id"]) |
| 32 |
|
| 33 |
def get_user_email(self, data): |
| 34 |
email = data.get("default_email") |
| 35 |
if not email: |
| 36 |
emails = data.get("emails") |
| 37 |
email = emails[0] if emails else "" |
| 38 |
return email |
| 39 |
|
| 40 |
def extract_common_fields(self, data): |
| 41 |
email = self.get_user_email(data) |
| 42 |
return dict( |
| 43 |
email=email, |
| 44 |
last_name=data.get("last_name"), |
| 45 |
username=data.get("display_name"), |
| 46 |
first_name=data.get("first_name"), |
| 47 |
) |
| 48 |
|
| 49 |
def extract_email_addresses(self, data): |
| 50 |
ret = [] |
| 51 |
email = self.get_user_email(data) |
| 52 |
if email: |
| 53 |
ret.append(EmailAddress(email=email, verified=True, primary=True)) |
| 54 |
return ret |
| 55 |
|
| 56 |
|
| 57 |
provider_classes = [YandexProvider] |