| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.socialaccount import providers |
| 4 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 5 |
from allauth.socialaccount.providers.klaviyo.views import KlaviyoOAuth2Adapter |
| 6 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 7 |
|
| 8 |
|
| 9 |
class KlaviyoAccount(ProviderAccount): |
| 10 |
def to_str(self): |
| 11 |
if name := self.account.extra_data.get("contact_information", {}).get( |
| 12 |
"organization_name" |
| 13 |
): |
| 14 |
return name |
| 15 |
return super().to_str() |
| 16 |
|
| 17 |
|
| 18 |
class KlaviyoProvider(OAuth2Provider): |
| 19 |
id = "klaviyo" |
| 20 |
name = "Klaviyo" |
| 21 |
account_class = KlaviyoAccount |
| 22 |
oauth2_adapter_class = KlaviyoOAuth2Adapter |
| 23 |
|
| 24 |
def extract_uid(self, data): |
| 25 |
return data["data"][0]["id"] |
| 26 |
|
| 27 |
def extract_common_fields(self, data: dict) -> dict: |
| 28 |
ret: dict = {} |
| 29 |
contact_information = self.extract_extra_data(data).get( |
| 30 |
"contact_information", {} |
| 31 |
) |
| 32 |
if email := contact_information.get("default_sender_email"): |
| 33 |
ret["email"] = email |
| 34 |
if name := contact_information.get("organization_name"): |
| 35 |
ret["name"] = name |
| 36 |
return ret |
| 37 |
|
| 38 |
def extract_extra_data(self, data: dict) -> dict: |
| 39 |
return data["data"][0]["attributes"] |
| 40 |
|
| 41 |
def get_default_scope(self): |
| 42 |
return ["accounts:read"] |
| 43 |
|
| 44 |
|
| 45 |
provider_classes = [KlaviyoProvider] |
| 46 |
providers.registry.register(KlaviyoProvider) |