add teamsnap preferences page.
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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
16
teamsnap/forms.py
Normal 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"}
|
||||
),
|
||||
}
|
||||
25
teamsnap/migrations/0001_initial.py
Normal file
25
teamsnap/migrations/0001_initial.py
Normal 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)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -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()
|
||||
|
||||
@@ -8,7 +8,6 @@ class TeamsnapAccount(ProviderAccount):
|
||||
|
||||
|
||||
class TeamsnapProvider(OAuth2Provider):
|
||||
|
||||
id = "teamsnap"
|
||||
name = "TeamSnap"
|
||||
account_class = TeamsnapAccount
|
||||
|
||||
7
teamsnap/templates/preferences.html
Normal file
7
teamsnap/templates/preferences.html
Normal 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 %}
|
||||
48
teamsnap/templates/schedule.html
Normal file
48
teamsnap/templates/schedule.html
Normal 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 %}
|
||||
@@ -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")]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user