| 1 |
"""Customise Provider classes for Eventbrite API v3.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from allauth.account.models import EmailAddress |
| 6 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 7 |
from allauth.socialaccount.providers.eventbrite.views import EventbriteOAuth2Adapter |
| 8 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 9 |
|
| 10 |
|
| 11 |
class EventbriteAccount(ProviderAccount): |
| 12 |
"""ProviderAccount subclass for Eventbrite.""" |
| 13 |
|
| 14 |
def get_avatar_url(self): |
| 15 |
"""Return avatar url.""" |
| 16 |
return self.account.extra_data["image_id"] |
| 17 |
|
| 18 |
def to_str(self): |
| 19 |
emails = self.account.extra_data.get("emails") |
| 20 |
if emails: |
| 21 |
return emails[0]["email"] |
| 22 |
return super().to_str() |
| 23 |
|
| 24 |
|
| 25 |
class EventbriteProvider(OAuth2Provider): |
| 26 |
"""OAuth2Provider subclass for Eventbrite.""" |
| 27 |
|
| 28 |
id = "eventbrite" |
| 29 |
name = "Eventbrite" |
| 30 |
account_class = EventbriteAccount |
| 31 |
oauth2_adapter_class = EventbriteOAuth2Adapter |
| 32 |
|
| 33 |
def extract_uid(self, data): |
| 34 |
"""Extract uid ('id') and ensure it's a str.""" |
| 35 |
return str(data["id"]) |
| 36 |
|
| 37 |
def get_default_scope(self): |
| 38 |
"""Ensure scope is null to fit their API.""" |
| 39 |
return [""] |
| 40 |
|
| 41 |
def extract_common_fields(self, data): |
| 42 |
"""Extract fields from a basic user query.""" |
| 43 |
email = None |
| 44 |
for curr_email in data.get("emails", []): |
| 45 |
email = email or curr_email.get("email") |
| 46 |
if curr_email.get("verified", False) and curr_email.get("primary", False): |
| 47 |
email = curr_email.get("email") |
| 48 |
|
| 49 |
return dict( |
| 50 |
email=email, |
| 51 |
id=data.get("id"), |
| 52 |
name=data.get("name"), |
| 53 |
first_name=data.get("first_name"), |
| 54 |
last_name=data.get("last_name"), |
| 55 |
image_url=data.get("image_url"), |
| 56 |
) |
| 57 |
|
| 58 |
def extract_email_addresses(self, data): |
| 59 |
addresses = [] |
| 60 |
for email in data.get("emails", []): |
| 61 |
addresses.append( |
| 62 |
EmailAddress( |
| 63 |
email=email.get("email"), |
| 64 |
verified=email.get("verified"), |
| 65 |
) |
| 66 |
) |
| 67 |
|
| 68 |
return addresses |
| 69 |
|
| 70 |
|
| 71 |
provider_classes = [EventbriteProvider] |