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 .models import Preferences
from .provider import TeamsnapProvider
import pyteamsnap.api
class TeamsnapAdapter(OAuth2Adapter):
provider_id = TeamsnapProvider.id
@@ -42,16 +42,21 @@ class TeamsnapAdapter(OAuth2Adapter):
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)
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):
template_name = "preferences.html"
@@ -114,19 +119,11 @@ def schedule_view(request, team_id=None):
return redirect(
"teamsnap_schedule", 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
)
client = get_teamsnap_client(request)
no_past = bool(request.GET.get("no_past", 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)
if no_past:
ts_events = [
@@ -151,14 +148,6 @@ def view_event(request, event_id, team_id=None):
return redirect(
"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 (
AvailabilitySummary,
@@ -166,10 +155,9 @@ def view_event(request, event_id, team_id=None):
EventLineup,
EventLineupEntry,
Member,
TeamSnap,
)
client = TeamSnap(token=ts_token)
client = get_teamsnap_client(request)
ts_bulkload = client.bulk_load(
team_id=team_id,
types=[Event, EventLineup, EventLineupEntry, AvailabilitySummary, Member],
@@ -204,14 +192,6 @@ def edit_lineup(request, event_ids, team_id):
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 (
Availability,
AvailabilitySummary,
@@ -222,7 +202,7 @@ def edit_lineup(request, event_ids, team_id):
TeamSnap,
)
client = TeamSnap(token=ts_token)
client = get_teamsnap_client(request)
event_ids = str(event_ids).split(",")
@@ -376,17 +356,10 @@ def dashboard(request, team_id=None):
return redirect(
"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 = (
current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token
)
from pyteamsnap.api import AvailabilitySummary, Event, TeamSnap
from pyteamsnap.api import AvailabilitySummary, Event
client = TeamSnap(token=ts_token)
client = get_teamsnap_client(request)
ts_events = Event.search(client, team_id=team_id)
ts_availability_summaries_d = {
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):
from teamsnap.forms import LineupEntryFormset
request.user.socialaccount_set.filter(provider="teamsnap").first()
from pyteamsnap.api import EventLineup, TeamSnap, EventLineupEntry, Event
current_teamsnap_user = request.user.socialaccount_set.filter(
provider="teamsnap"
).first()
ts_token = (
current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token
)
client = TeamSnap(token=ts_token)
client = get_teamsnap_client(request)
ts_event = Event.get(client,event_id)
ts_lineup = EventLineup.search(client, event_id=event_id)
event_lineup_id = ts_lineup[0].data['id']