add teamsnap preferences page.

This commit is contained in:
2022-06-02 08:52:38 -05:00
parent e1d47f345f
commit db020ee153
10 changed files with 171 additions and 2 deletions

View File

@@ -4,9 +4,12 @@ from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _
from benchcoach.users.forms import UserAdminChangeForm, UserAdminCreationForm
from teamsnap.models import Preferences
User = get_user_model()
admin.site.register(Preferences)
@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):

View File

@@ -15,7 +15,7 @@ urlpatterns = [
# User management
path("users/", include("benchcoach.users.urls", namespace="users")),
path("accounts/", include("allauth.urls")),
# Your stuff: custom urls includes go here
path("ts/", include("teamsnap.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

16
teamsnap/forms.py Normal file
View File

@@ -0,0 +1,16 @@
from django import forms
from django.forms import ModelForm
from .models import Preferences
class PreferencesForm(ModelForm):
class Meta:
model = Preferences
fields = ["user", "managed_team_id"]
widgets = {
"user": forms.HiddenInput(),
"managed_team_id": forms.Select(
choices=(), attrs={"class": "form-control"}
),
}

View File

@@ -0,0 +1,25 @@
# Generated by Django 3.2.13 on 2022-06-02 13:20
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Preferences',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('managed_team_id', models.IntegerField()),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@@ -1 +1,9 @@
# Create your models here.
from django.db import models
from benchcoach.users.models import User
class Preferences(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
managed_team_id = models.IntegerField()

View File

@@ -8,7 +8,6 @@ class TeamsnapAccount(ProviderAccount):
class TeamsnapProvider(OAuth2Provider):
id = "teamsnap"
name = "TeamSnap"
account_class = TeamsnapAccount

View File

@@ -0,0 +1,7 @@
{% extends "base.html" %}
{% block content %}
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
{% endblock content %}

View File

@@ -0,0 +1,48 @@
{% extends "base.html" %}{% load static %}
{% block title %} {{ title }}{% endblock %}
{% block content %}
{% load tz %}
<div class="">
<h3 class="mb-2">
Schedule
</h3>
<div class="card">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-striped table-sm">
{# <thead>#}
{# </thead>#}
<tbody>
{% for event in events %}
<tr class="small">
<th>
{{ event.data.formatted_title }}
</th>
<td class="">
{{ event.data.start_date|localtime|date:"D"}}
</td>
<td>
{{ event.data.start_date|localtime|date:"M j"}}
</td>
<td class="d-none d-md-table-cell">
{{ event.data.start_date|localtime|date:"Y"}}
</td>
<td>
{{ event.data.start_date|localtime|date:"g:i A"}}
</td>
<td class="small">
{{ event.data.location_name }}
</td>
<td>
<a class="btn btn-outline-secondary btn-sm" href=""><i class="bi bi-three-dots"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@@ -1,5 +1,9 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from django.urls import path
from .provider import TeamsnapProvider
from .views import PreferencesFormView
urlpatterns = default_urlpatterns(TeamsnapProvider)
urlpatterns += [path("preferences", PreferencesFormView.as_view(), name="preferences")]

View File

@@ -4,7 +4,10 @@ from allauth.socialaccount.providers.oauth2.views import (
OAuth2CallbackView,
OAuth2LoginView,
)
from django.views.generic.edit import FormView
from .forms import PreferencesForm
from .models import Preferences
from .provider import TeamsnapProvider
@@ -44,3 +47,59 @@ class TeamsnapAdapter(OAuth2Adapter):
oauth2_login = OAuth2LoginView.adapter_view(TeamsnapAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(TeamsnapAdapter)
class PreferencesFormView(FormView):
template_name = "preferences.html"
form_class = PreferencesForm
success_url = "/"
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
if form.data["user"] == str(self.request.user.id):
form.save()
return super().form_valid(form)
def get_initial(self):
"""
Returns the initial data to use for forms on this view.
"""
initial = super().get_initial()
initial["user"] = self.request.user
# initial['managed_team_id']
return initial
def get_form(self):
"""
Returns the initial data to use for forms on this view.
"""
import pyteamsnap
ts_account = self.request.user.socialaccount_set.first()
ts_token = ts_account.socialtoken_set.first()
# ts_token =
ts = pyteamsnap.TeamSnap(token=ts_token)
me = pyteamsnap.api.Me(ts)
teams = [
(id, pyteamsnap.api.Team.get(ts, id=id))
for id in me.data["managed_team_ids"]
]
try:
contact = Preferences.objects.get(user=self.request.user)
form = PreferencesForm(instance=contact, **self.get_form_kwargs())
except Preferences.DoesNotExist:
form = super().get_form(self.form_class)
choices = [
(id, f"{team.data['name']} ({team.data['season_name']})")
for id, team in teams
]
form.fields["managed_team_id"].widget.choices = choices
return form