diff --git a/config/settings/base.py b/config/settings/base.py index e79480a..16a8608 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -281,3 +281,4 @@ INSTALLED_APPS += [ "teamsnap.dashboard", ] SOCIALACCOUNT_PROVIDERS = {"teamsnap": {"SCOPE": ["read", "write"]}} +SESSION_SERIALIZER = "django.contrib.sessions.serializers.PickleSerializer" diff --git a/teamsnap/dashboard/views.py b/teamsnap/dashboard/views.py index 13163f1..bcb9d55 100644 --- a/teamsnap/dashboard/views.py +++ b/teamsnap/dashboard/views.py @@ -25,7 +25,7 @@ def dashboard(request, team_id=None): team_id=request.user.teamsnap_preferences.managed_team_id, ) - from pyteamsnap.api import AvailabilitySummary, Event + from pyteamsnap.objects import AvailabilitySummary, Event client = get_teamsnap_client(request) ts_events = Event.search(client, team_id=team_id) diff --git a/teamsnap/lineup/views.py b/teamsnap/lineup/views.py index ae5a8a5..a1bd75b 100644 --- a/teamsnap/lineup/views.py +++ b/teamsnap/lineup/views.py @@ -17,7 +17,7 @@ def teamsnap_event_redirect(request, event_id, team_id): def edit_lineup(request, event_ids, team_id): import re - from pyteamsnap.api import ( + from pyteamsnap.objects import ( Availability, AvailabilitySummary, Event, @@ -25,7 +25,6 @@ def edit_lineup(request, event_ids, team_id): EventLineupEntry, Member, ) - from teamsnap.forms import LineupEntryFormset client = get_teamsnap_client(request) @@ -200,8 +199,7 @@ def edit_lineup(request, event_ids, team_id): def submit_lineup(request, team_id, event_id): - from pyteamsnap.api import Event, EventLineup, EventLineupEntry - + from pyteamsnap.objects import Event, EventLineup, EventLineupEntry from teamsnap.forms import LineupEntryFormset client = get_teamsnap_client(request) @@ -260,7 +258,8 @@ def multi_lineup_choose(request, team_id=None): team_id=request.user.teamsnap_preferences.managed_team_id, ) from django.forms import formset_factory - from pyteamsnap.api import Event + + from pyteamsnap.objects import Event from .forms import EventChooseForm diff --git a/teamsnap/utils/__init__.py b/teamsnap/utils/__init__.py index 6b31ac6..01add0d 100644 --- a/teamsnap/utils/__init__.py +++ b/teamsnap/utils/__init__.py @@ -1,14 +1,26 @@ -import pyteamsnap +import logging + +from pyteamsnap.client import TeamSnap + +# This retrieves a Python logging instance (or creates it) +logger = logging.getLogger(__name__) def get_teamsnap_client(request): - request.user.socialaccount_set.filter(provider="teamsnap").first() - current_teamsnap_user = request.user.socialaccount_set.filter( - provider="teamsnap" - ).first() + client = request.session.get("teamsnap_client") + if client: + logger.info("TeamSnap client found saved in session, loading.") + return client + elif not client: + logger.info("No TeamSnap client saved in session, getting one.") + 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) + ts_token = ( + current_teamsnap_user.socialtoken_set.order_by("-expires_at").first().token + ) + client = TeamSnap(token=ts_token) + request.session["teamsnap_client"] = client + return client diff --git a/teamsnap/views.py b/teamsnap/views.py index 8e97d43..c5ed20d 100644 --- a/teamsnap/views.py +++ b/teamsnap/views.py @@ -116,7 +116,7 @@ def schedule_view(request, team_id=None): 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 + from pyteamsnap.objects import Event ts_events = Event.search(client, team_id=team_id) if no_past: @@ -143,7 +143,7 @@ def view_event(request, event_id, team_id=None): "teamsnap_event", team_id=request.user.teamsnap_preferences.managed_team_id ) - from pyteamsnap.api import ( + from pyteamsnap.objects import ( AvailabilitySummary, Event, EventLineup, @@ -183,7 +183,8 @@ def view_event(request, event_id, team_id=None): def multi_lineup_choose(request, team_id): from django.forms import formset_factory - from pyteamsnap.api import Event + + from pyteamsnap.objects import Event from .forms import EventChooseForm