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