cleaned up into get_teamsnap_client

This commit is contained in:
2022-06-07 08:10:19 -05:00
parent b72dfff140
commit 7dc99507e3

View File

@@ -13,7 +13,7 @@ from django.http import HttpResponseNotAllowed, HttpResponse, JsonResponse, Http
from .forms import PreferencesForm from .forms import PreferencesForm
from .models import Preferences from .models import Preferences
from .provider import TeamsnapProvider from .provider import TeamsnapProvider
import pyteamsnap.api
class TeamsnapAdapter(OAuth2Adapter): class TeamsnapAdapter(OAuth2Adapter):
provider_id = TeamsnapProvider.id provider_id = TeamsnapProvider.id
@@ -42,16 +42,21 @@ class TeamsnapAdapter(OAuth2Adapter):
user.username = user.email user.username = user.email
return user 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_login = OAuth2LoginView.adapter_view(TeamsnapAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(TeamsnapAdapter) oauth2_callback = OAuth2CallbackView.adapter_view(TeamsnapAdapter)
def get_teamsnap_client(request):
request.user.socialaccount_set.filter(provider="teamsnap").first()
current_teamsnap_user = request.user.socialaccount_set.filter(
provider="teamsnap"
).first()
ts_token = (
current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token
)
return pyteamsnap.api.TeamSnap(token=ts_token)
class PreferencesFormView(FormView): class PreferencesFormView(FormView):
template_name = "preferences.html" template_name = "preferences.html"
@@ -114,19 +119,11 @@ def schedule_view(request, team_id=None):
return redirect( return redirect(
"teamsnap_schedule", team_id=request.user.preferences.managed_team_id "teamsnap_schedule", team_id=request.user.preferences.managed_team_id
) )
request.user.socialaccount_set.filter(provider="teamsnap").first() client = get_teamsnap_client(request)
current_teamsnap_user = request.user.socialaccount_set.filter(
provider="teamsnap"
).first()
ts_token = (
current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token
)
no_past = bool(request.GET.get("no_past", 0)) no_past = bool(request.GET.get("no_past", 0))
games_only = bool(request.GET.get("games_only", 0)) games_only = bool(request.GET.get("games_only", 0))
from pyteamsnap.api import Event, TeamSnap from pyteamsnap.api import Event
client = TeamSnap(token=ts_token)
ts_events = Event.search(client, team_id=team_id) ts_events = Event.search(client, team_id=team_id)
if no_past: if no_past:
ts_events = [ ts_events = [
@@ -151,14 +148,6 @@ def view_event(request, event_id, team_id=None):
return redirect( return redirect(
"teamsnap_event", team_id=request.user.preferences.managed_team_id "teamsnap_event", team_id=request.user.preferences.managed_team_id
) )
request.user.socialaccount_set.filter(provider="teamsnap").first()
current_teamsnap_user = request.user.socialaccount_set.filter(
provider="teamsnap"
).first()
ts_token = (
current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token
)
from pyteamsnap.api import ( from pyteamsnap.api import (
AvailabilitySummary, AvailabilitySummary,
@@ -166,10 +155,9 @@ def view_event(request, event_id, team_id=None):
EventLineup, EventLineup,
EventLineupEntry, EventLineupEntry,
Member, Member,
TeamSnap,
) )
client = TeamSnap(token=ts_token) client = get_teamsnap_client(request)
ts_bulkload = client.bulk_load( ts_bulkload = client.bulk_load(
team_id=team_id, team_id=team_id,
types=[Event, EventLineup, EventLineupEntry, AvailabilitySummary, Member], types=[Event, EventLineup, EventLineupEntry, AvailabilitySummary, Member],
@@ -204,14 +192,6 @@ def edit_lineup(request, event_ids, team_id):
from teamsnap.forms import LineupEntryFormset from teamsnap.forms import LineupEntryFormset
request.user.socialaccount_set.filter(provider="teamsnap").first()
current_teamsnap_user = request.user.socialaccount_set.filter(
provider="teamsnap"
).first()
ts_token = (
current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token
)
from pyteamsnap.api import ( from pyteamsnap.api import (
Availability, Availability,
AvailabilitySummary, AvailabilitySummary,
@@ -222,7 +202,7 @@ def edit_lineup(request, event_ids, team_id):
TeamSnap, TeamSnap,
) )
client = TeamSnap(token=ts_token) client = get_teamsnap_client(request)
event_ids = str(event_ids).split(",") event_ids = str(event_ids).split(",")
@@ -376,17 +356,10 @@ def dashboard(request, team_id=None):
return redirect( return redirect(
"teamsnap_dashboard", team_id=request.user.preferences.managed_team_id "teamsnap_dashboard", team_id=request.user.preferences.managed_team_id
) )
request.user.socialaccount_set.filter(provider="teamsnap").first()
current_teamsnap_user = request.user.socialaccount_set.filter(
provider="teamsnap"
).first()
ts_token = ( from pyteamsnap.api import AvailabilitySummary, Event
current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token
)
from pyteamsnap.api import AvailabilitySummary, Event, TeamSnap
client = TeamSnap(token=ts_token) client = get_teamsnap_client(request)
ts_events = Event.search(client, team_id=team_id) ts_events = Event.search(client, team_id=team_id)
ts_availability_summaries_d = { ts_availability_summaries_d = {
a.data["id"]: a for a in AvailabilitySummary.search(client, team_id=team_id) a.data["id"]: a for a in AvailabilitySummary.search(client, team_id=team_id)
@@ -414,18 +387,13 @@ def dashboard(request, team_id=None):
}, },
) )
def submit_lineup(request, team_id, event_id): def submit_lineup(request, team_id, event_id):
from teamsnap.forms import LineupEntryFormset from teamsnap.forms import LineupEntryFormset
request.user.socialaccount_set.filter(provider="teamsnap").first()
from pyteamsnap.api import EventLineup, TeamSnap, EventLineupEntry, Event from pyteamsnap.api import EventLineup, TeamSnap, EventLineupEntry, Event
current_teamsnap_user = request.user.socialaccount_set.filter(
provider="teamsnap" client = get_teamsnap_client(request)
).first()
ts_token = (
current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token
)
client = TeamSnap(token=ts_token)
ts_event = Event.get(client,event_id) ts_event = Event.get(client,event_id)
ts_lineup = EventLineup.search(client, event_id=event_id) ts_lineup = EventLineup.search(client, event_id=event_id)
event_lineup_id = ts_lineup[0].data['id'] event_lineup_id = ts_lineup[0].data['id']