| 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.trainingpeaks.views import ( |
| 6 |
TrainingPeaksOAuth2Adapter, |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
class TrainingPeaksAccount(ProviderAccount): |
| 11 |
def get_profile_url(self): |
| 12 |
return "https://app.trainingpeaks.com" |
| 13 |
|
| 14 |
def get_avatar_url(self): |
| 15 |
return None |
| 16 |
|
| 17 |
|
| 18 |
class TrainingPeaksProvider(OAuth2Provider): |
| 19 |
id = "trainingpeaks" |
| 20 |
name = "TrainingPeaks" |
| 21 |
account_class = TrainingPeaksAccount |
| 22 |
oauth2_adapter_class = TrainingPeaksOAuth2Adapter |
| 23 |
|
| 24 |
def extract_uid(self, data): |
| 25 |
return str(data["Id"]) |
| 26 |
|
| 27 |
def extract_common_fields(self, data): |
| 28 |
extra_common = super().extract_common_fields(data) |
| 29 |
firstname = data.get("FirstName") |
| 30 |
lastname = data.get("LastName") |
| 31 |
# fallback username as there is actually no Username in response |
| 32 |
username = f"{firstname.strip().lower()}.{lastname.strip().lower()}" |
| 33 |
name = " ".join(part for part in (firstname, lastname) if part) |
| 34 |
extra_common.update( |
| 35 |
username=data.get("username", username), |
| 36 |
email=data.get("Email"), |
| 37 |
first_name=firstname, |
| 38 |
last_name=lastname, |
| 39 |
name=name.strip(), |
| 40 |
) |
| 41 |
return extra_common |
| 42 |
|
| 43 |
def get_default_scope(self): |
| 44 |
return ["athlete:profile"] |
| 45 |
|
| 46 |
|
| 47 |
provider_classes = [TrainingPeaksProvider] |