| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 4 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 5 |
from allauth.socialaccount.providers.stackexchange.views import ( |
| 6 |
StackExchangeOAuth2Adapter, |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
class StackExchangeAccount(ProviderAccount): |
| 11 |
def get_profile_url(self): |
| 12 |
return self.account.extra_data.get("html_url") |
| 13 |
|
| 14 |
def get_avatar_url(self): |
| 15 |
return self.account.extra_data.get("avatar_url") |
| 16 |
|
| 17 |
|
| 18 |
class StackExchangeProvider(OAuth2Provider): |
| 19 |
id = "stackexchange" |
| 20 |
name = "Stack Exchange" |
| 21 |
account_class = StackExchangeAccount |
| 22 |
oauth2_adapter_class = StackExchangeOAuth2Adapter |
| 23 |
|
| 24 |
def get_site(self): |
| 25 |
settings = self.get_settings() |
| 26 |
return settings.get("SITE", "stackoverflow") |
| 27 |
|
| 28 |
def extract_uid(self, data): |
| 29 |
# `user_id` varies if you use the same account for |
| 30 |
# e.g. StackOverflow and ServerFault. Therefore, we pick |
| 31 |
# `account_id`. |
| 32 |
uid = str(data["account_id"]) |
| 33 |
return uid |
| 34 |
|
| 35 |
def extract_common_fields(self, data): |
| 36 |
return dict(username=data.get("display_name")) |
| 37 |
|
| 38 |
|
| 39 |
provider_classes = [StackExchangeProvider] |