incorporate new pyteamsnap, incorporate loading from sessions before creating new client.

This commit is contained in:
2022-06-23 07:24:07 -05:00
parent e027d3d6ae
commit 5edb233fb4
5 changed files with 32 additions and 19 deletions

View File

@@ -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