initial commit

This commit is contained in:
2022-06-01 15:30:52 -05:00
commit e1d47f345f
151 changed files with 4114 additions and 0 deletions

0
teamsnap/__init__.py Normal file
View File

1
teamsnap/admin.py Normal file
View File

@@ -0,0 +1 @@
# Register your models here.

6
teamsnap/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class TeamsnapConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "teamsnap"

View File

1
teamsnap/models.py Normal file
View File

@@ -0,0 +1 @@
# Create your models here.

32
teamsnap/provider.py Normal file
View File

@@ -0,0 +1,32 @@
from allauth.socialaccount import providers
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class TeamsnapAccount(ProviderAccount):
pass
class TeamsnapProvider(OAuth2Provider):
id = "teamsnap"
name = "TeamSnap"
account_class = TeamsnapAccount
def extract_uid(self, data):
return str(data["id"])
def extract_common_fields(self, data):
return dict(
username=data["email"],
email=data["email"],
first_name=data["first_name"],
last_name=data["last_name"],
)
def get_default_scope(self):
scope = ["read"]
return scope
providers.registry.register(TeamsnapProvider)

1
teamsnap/tests.py Normal file
View File

@@ -0,0 +1 @@
# Create your tests here.

5
teamsnap/urls.py Normal file
View File

@@ -0,0 +1,5 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import TeamsnapProvider
urlpatterns = default_urlpatterns(TeamsnapProvider)

46
teamsnap/views.py Normal file
View File

@@ -0,0 +1,46 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import TeamsnapProvider
class TeamsnapAdapter(OAuth2Adapter):
provider_id = TeamsnapProvider.id
# Fetched programmatically, must be reachable from container
access_token_url = "{}/oauth/token/".format("https://auth.teamsnap.com")
profile_url = "{}/me/".format("https://api.teamsnap.com/v3/")
# Accessed by the user browser, must be reachable by the host
authorize_url = "{}/oauth/authorize/".format("https://auth.teamsnap.com/")
# NOTE: trailing slashes in URLs are important, don't miss it
def complete_login(self, request, app, token, **kwargs):
headers = {"Authorization": f"Bearer {token.token}"}
resp = requests.get(self.profile_url, headers=headers)
j = resp.json()
if j.get("collection", {}).get("items"):
extra_data = {
i["name"]: i["value"] for i in j["collection"]["items"][0]["data"]
}
return self.get_provider().sociallogin_from_response(request, extra_data)
def populate_user(self, request, sociallogin, data):
user = super().populate_user(request, sociallogin, data)
user.username = user.email
return user
# def get_callback_url(self, request, app):
# callback_url = reverse(self.provider_id + "_callback")
# protocol = self.redirect_uri_protocol
# return build_absolute_uri(request, callback_url, protocol)
# return "urn:ietf:wg:oauth:2.0:oob"
oauth2_login = OAuth2LoginView.adapter_view(TeamsnapAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(TeamsnapAdapter)