add schedule, view_event

This commit is contained in:
2022-06-02 10:33:34 -05:00
parent db020ee153
commit 598ebd6910
10 changed files with 344 additions and 9 deletions

View File

@@ -1,9 +1,12 @@
import datetime
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from django.shortcuts import redirect, render
from django.views.generic.edit import FormView
from .forms import PreferencesForm
@@ -103,3 +106,93 @@ class PreferencesFormView(FormView):
form.fields["managed_team_id"].widget.choices = choices
return form
def schedule_view(request, team_id=None):
if not team_id:
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
)
no_past = bool(request.GET.get("no_past", 0))
games_only = bool(request.GET.get("games_only", 0))
from pyteamsnap.api import Event, TeamSnap
client = TeamSnap(token=ts_token)
ts_events = Event.search(client, team_id=team_id)
if no_past:
ts_events = [
e
for e in ts_events
if e.data["start_date"] > datetime.datetime.now(datetime.timezone.utc)
]
if games_only:
ts_events = [e for e in ts_events if e.data["is_game"]]
ts_events = {e.data["id"]: e for e in ts_events}
pass
return render(
request,
"schedule.html",
context={"events": ts_events.values(), "team_id": team_id},
)
def view_event(request, event_id, team_id=None):
if not team_id:
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,
Event,
EventLineup,
EventLineupEntry,
Member,
TeamSnap,
)
client = TeamSnap(token=ts_token)
ts_bulkload = client.bulk_load(
team_id=team_id,
types=[Event, EventLineup, EventLineupEntry, AvailabilitySummary, Member],
event__id=event_id,
)
ts_event = [i for i in ts_bulkload if isinstance(i, Event)][0]
ts_availability_summary = [
i
for i in ts_bulkload
if isinstance(i, AvailabilitySummary) and i.data["event_id"] == event_id
][0]
ts_lineup_entries = [
i
for i in ts_bulkload
if isinstance(i, EventLineupEntry) and i.data["event_id"] == event_id
]
return render(
request,
"event/view_event.html",
context={
"availability_summary": ts_availability_summary,
"event": ts_event,
"availablities": [],
"lineup_entries": ts_lineup_entries,
},
)