| 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.pinterest.views import PinterestOAuth2Adapter |
| 6 |
|
| 7 |
|
| 8 |
class PinterestAccount(ProviderAccount): |
| 9 |
def get_username(self): |
| 10 |
return self.account.extra_data.get("username") |
| 11 |
|
| 12 |
def get_profile_url(self): |
| 13 |
# v5 extra_data not same as v1 |
| 14 |
username = self.get_username() |
| 15 |
if username: |
| 16 |
return f"https://www.pinterest.com/{username}/" |
| 17 |
return self.account.extra_data.get("url") |
| 18 |
|
| 19 |
def get_avatar_url(self): |
| 20 |
return self.account.extra_data.get("profile_image") |
| 21 |
|
| 22 |
|
| 23 |
class PinterestProvider(OAuth2Provider): |
| 24 |
id = "pinterest" |
| 25 |
name = "Pinterest" |
| 26 |
account_class = PinterestAccount |
| 27 |
oauth2_adapter_class = PinterestOAuth2Adapter |
| 28 |
|
| 29 |
@property |
| 30 |
def api_version(self): |
| 31 |
return self.get_settings().get("API_VERSION", "v1") |
| 32 |
|
| 33 |
def get_default_scope(self): |
| 34 |
# See: https://developers.pinterest.com/docs/api/overview/#scopes |
| 35 |
if self.api_version == "v5": |
| 36 |
# See: https://developers.pinterest.com/docs/getting-started/scopes/ |
| 37 |
return ["user_accounts:read"] |
| 38 |
elif self.api_version == "v3": |
| 39 |
return ["read_users"] |
| 40 |
return ["read_public"] |
| 41 |
|
| 42 |
def extract_extra_data(self, data): |
| 43 |
if self.api_version == "v5": |
| 44 |
return data |
| 45 |
return data.get("data", {}) |
| 46 |
|
| 47 |
def extract_uid(self, data): |
| 48 |
if self.api_version == "v5": |
| 49 |
return data["username"] |
| 50 |
return str(data["data"]["id"]) |
| 51 |
|
| 52 |
def extract_common_fields(self, data): |
| 53 |
if self.api_version == "v5": |
| 54 |
return dict(username=data["username"]) |
| 55 |
return dict( |
| 56 |
first_name=data.get("data", {}).get("first_name"), |
| 57 |
last_name=data.get("data", {}).get("last_name"), |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
provider_classes = [PinterestProvider] |