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 +1,6 @@
from django.contrib import admin
from .models import Preferences
# Register your models here.
admin.site.register(Preferences)

View File

@@ -0,0 +1,76 @@
<html lang="en">
{% extends "base.html" %}{% load static %}
{% block title %} {{ event.data.formatted_title }} - Instagenerator{% endblock %}
{% block page_heading %}
<div class="row d-inline-flex">
<div class="col">
<img src="{% static 'teamsnap/ig/graphics/hounds-insta.jpg' %}" class="mx-auto d-block img-fluid shadow-sm" style="height:30px;border-radius: 8px;">
</div>
<div class="col text-nowrap px-0">Hounds Instagenerator</div>
</div>
{% endblock %}
{% block page_subheading %}{{ event.data.formatted_title }}, {{ event.data.start_date }}, {{ event.data.location_name }}{% endblock %}
{% block content %}
<form method="get" action="generate">
<div class="mb-3">
<select hidden class="form-select" name="game_id" id="game_id">
<optgroup label="Events">
<option value="" disabled="disabled">Select an event...</option>
<option selected value="{{event.data.id}}">{{event.data.formatted_title}}</option>
</optgroup>
</select>
</div>
<div class="mb-3">
<legend class="">Background</legend>
<div class="form-check">
<input class="form-check-input" type="radio" name="background" id="backgroundLocation" checked value="location">
<label class="form-check-label" for="backgroundLocation">
Location
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="background" id="backgroundTransparent" value="transparent">
<label class="form-check-label" for="backgroundTransparent">
Transparent
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="background" id="badge_only" value="badge">
<label class="form-check-label" for="badge_only">
Badge only
</label>
</div>
</div>
<div class="mb-3">
<legend class="">Layout</legend>
<div class="form-check">
<input class="form-check-input" type="radio" name="dimensions" id="square" checked value="1080x1080">
<label class="form-check-label" for="square">
Square
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="dimensions" id="portrait" value="1080x1920">
<label class="form-check-label" for="portrait">
Portrait
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="dimensions" id="portrait" value="1920x1080">
<label class="form-check-label" for="portrait">
Landscape
</label>
</div>
</div>
<div class="d-lg-flex justify-content-lg-end align-items-lg-center mb-3">
<button type="submit" class="btn btn-primary btn-block">Get Image</button>
</div>
</form>
{% endblock %}

View File

@@ -0,0 +1,144 @@
{% extends "base.html" %}{% load static %}
{% block title %} {{ title }}{% endblock %}
{% block content %}
<div class="card mx-auto" style="max-width: 455px">
<div class="border-bottom p-2">
<h4 class="card-title text-nowrap">{{ event.data.formatted_title }}</h4>
<h6 class="text-muted card-subtitle text-nowrap">{{ event.data.start_date|date:"D, F j, Y g:i A" }}</h6>
<div class="row">
<div class="col text-end">
{# <form method="get"#}
{# action="{% url "teamsnap_image_generator_generate" team_id=request.user.teamsnapsettings.managed_team.id event_id=event.data.id %}">#}
{# <select hidden class="form-select" name="game_id" id="game_id">#}
{# <optgroup label="Events">#}
{# <option value="" disabled="disabled">Select an event...</option>#}
{# <option selected#}
{# value="{{ event.data.id }}">{{ event.data.formatted_title }}</option>#}
{# </optgroup>#}
{# </select>#}
{# <input hidden class="form-check-input" type="radio" name="background"#}
{# id="backgroundLocation" checked value="location">#}
{# <input hidden class="form-check-input" type="radio" name="dimensions" id="square" checked#}
{# value="1080x1080">#}
{# <button type="submit" class="btn btn-primary btn-sm py-0 m-1"><i class="bi bi-instagram"></i>#}
{# </button>#}
{# </form>#}
{# <a class="btn btn-primary btn-sm py-0 m-1"#}
{# href="{% url "teamsnap_image_generator" team_id=request.user.teamsnapsettings.managed_team.id event_id=event.data.id %}"#}
{# role="button">#}
{# <div class="d-inline-block"><i class="bi bi-instagram"></i> <i class="bi bi-three-dots"></i>#}
{# </div>#}
{# </a>#}
</div>
</div>
</div>
<div class="card-body p-0 m-0">
<div>
<div class="row m-0">
<div class="col border-bottom bg-light">
<span class="text-uppercase fw-bold small"></span>
</div>
</div>
<div class="row m-0">
<div class="table-responsive p-0">
<table class="table table-sm">
{# <thead>#}
{# </thead>#}
<tbody>
<tr>
<th scope="col">
Date
</th>
<td>
{{ event.data.start_date|date:"D, F j, Y g:i A" }}
</td>
</tr>
<tr>
<th scope="col">
Opponent
</th>
<td>
<a href="">{{ event.data.opponent_name }}</a>
</td>
</tr>
<tr>
<th scope="col">
Location
</th>
<td>
<a href="">{{ event.data.location_name }}</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<div class="row m-0">
<div class="col border-bottom border-top bg-light">
<span class="text-uppercase fw-bold small">Availabilities</span>
</div>
</div>
<div class="row m-0">
<div class="table-responsive p-0">
<table class="table table-sm">
<tbody>
<tr>
<th scope="col" class="col-1">
<i class="bi bi-check-circle-fill text-success"></i>
</th>
<td>
{{ availability_summary.data.player_going_count }}
</td>
<th scope="col" class="col-1">
<i class="bi bi-question-circle-fill text-info"></i>
</th>
<td>
{{ availability_summary.data.player_maybe_count }}
</td>
<th scope="col" class="col-1">
<i class="bi bi-x-circle-fill text-danger"></i>
</th>
<td>
{{ availability_summary.data.player_not_going_count }}
</td>
<th scope="col" class="col-1">
<i class="bi bi-question-circle"></i>
</th>
<td>
{{ availability_summary.data.player_unknown_count }}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<div class="row m-0">
<div class="col border-bottom border-top bg-light">
<span class="text-uppercase fw-bold small">Lineup</span>
</div>
</div>
<div class="row m-0">
<div class="flex-column m-2">
<a class="btn btn-primary btn-sm"
href=""
role="button">Edit</a>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@@ -35,7 +35,7 @@
{{ event.data.location_name }}
</td>
<td>
<a class="btn btn-outline-secondary btn-sm" href=""><i class="bi bi-three-dots"></i></a>
<a class="btn btn-outline-secondary btn-sm" href="{% url 'teamsnap_view_event' team_id=event.data.team_id event_id=event.data.id %}"><i class="bi bi-three-dots"></i></a>
</td>
</tr>
{% endfor %}

View File

@@ -2,8 +2,17 @@ from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from django.urls import path
from .provider import TeamsnapProvider
from .views import PreferencesFormView
from .views import PreferencesFormView, schedule_view, view_event
urlpatterns = default_urlpatterns(TeamsnapProvider)
urlpatterns += [path("preferences", PreferencesFormView.as_view(), name="preferences")]
urlpatterns += [
path("preferences/", PreferencesFormView.as_view(), name="preferences"),
path("<int:team_id>/schedule/", schedule_view, name="teamsnap_schedule"),
path("schedule/", schedule_view, name="teamsnap_schedule"),
path(
"<int:team_id>/schedule/view_event/<int:event_id>",
view_event,
name="teamsnap_view_event",
),
]

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,
},
)