| 1 |
"""Customise Provider classes for MailChimp API v3.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 6 |
from allauth.socialaccount.providers.mailchimp.views import MailChimpOAuth2Adapter |
| 7 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 8 |
|
| 9 |
|
| 10 |
class MailChimpAccount(ProviderAccount): |
| 11 |
"""ProviderAccount subclass for MailChimp.""" |
| 12 |
|
| 13 |
def get_profile_url(self): |
| 14 |
"""Return base profile url.""" |
| 15 |
return self.account.extra_data["api_endpoint"] |
| 16 |
|
| 17 |
def get_avatar_url(self): |
| 18 |
"""Return avatar url.""" |
| 19 |
return self.account.extra_data["login"]["avatar"] |
| 20 |
|
| 21 |
def to_str(self): |
| 22 |
dflt = super().to_str() |
| 23 |
login_data = self.account.extra_data.get("login", {}) |
| 24 |
return login_data.get("login_email") or login_data.get("email") or dflt |
| 25 |
|
| 26 |
|
| 27 |
class MailChimpProvider(OAuth2Provider): |
| 28 |
"""OAuth2Provider subclass for MailChimp v3.""" |
| 29 |
|
| 30 |
id = "mailchimp" |
| 31 |
name = "MailChimp" |
| 32 |
account_class = MailChimpAccount |
| 33 |
oauth2_adapter_class = MailChimpOAuth2Adapter |
| 34 |
|
| 35 |
def extract_uid(self, data): |
| 36 |
"""Extract uid ('user_id') and ensure it's a str.""" |
| 37 |
return str(data["user_id"]) |
| 38 |
|
| 39 |
def get_default_scope(self): |
| 40 |
"""Ensure scope is null to fit their API.""" |
| 41 |
return [""] |
| 42 |
|
| 43 |
def extract_common_fields(self, data): |
| 44 |
"""Extract fields from a metadata query.""" |
| 45 |
return dict( |
| 46 |
dc=data.get("dc"), |
| 47 |
role=data.get("role"), |
| 48 |
account_name=data.get("accountname"), |
| 49 |
user_id=data.get("user_id"), |
| 50 |
login=data.get("login"), |
| 51 |
login_url=data.get("login_url"), |
| 52 |
api_endpoint=data.get("api_endpoint"), |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
provider_classes = [MailChimpProvider] |