| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 4 |
from allauth.socialaccount.providers.douban.views import DoubanOAuth2Adapter |
| 5 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 6 |
|
| 7 |
|
| 8 |
class DoubanAccount(ProviderAccount): |
| 9 |
def get_profile_url(self): |
| 10 |
return self.account.extra_data.get("alt") |
| 11 |
|
| 12 |
def get_avatar_url(self): |
| 13 |
return self.account.extra_data.get("large_avatar") |
| 14 |
|
| 15 |
|
| 16 |
class DoubanProvider(OAuth2Provider): |
| 17 |
id = "douban" |
| 18 |
name = "Douban" |
| 19 |
account_class = DoubanAccount |
| 20 |
oauth2_adapter_class = DoubanOAuth2Adapter |
| 21 |
|
| 22 |
def extract_uid(self, data): |
| 23 |
return data["id"] |
| 24 |
|
| 25 |
def extract_common_fields(self, data): |
| 26 |
""" |
| 27 |
Extract data from profile json to populate user instance. |
| 28 |
|
| 29 |
In Douban profile API: |
| 30 |
|
| 31 |
- id: a digital string, will never change |
| 32 |
- uid: defaults to id, but can be changed once, used in profile |
| 33 |
url, like slug |
| 34 |
- name: display name, can be changed every 30 days |
| 35 |
|
| 36 |
So we should use `id` as username here, other than `uid`. |
| 37 |
Also use `name` as `first_name` for displaying purpose. |
| 38 |
""" |
| 39 |
return { |
| 40 |
"username": data["id"], |
| 41 |
"first_name": data.get("name", ""), |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
provider_classes = [DoubanProvider] |