sync improvements

allow blank for teamsnap fields
This commit is contained in:
2021-12-22 15:21:36 -06:00
parent e0b9ed6d87
commit 3f39a7aa68
13 changed files with 189 additions and 590 deletions

View File

@@ -4,20 +4,20 @@ from .teamsnap.api import Event as TsApiEvent
from .teamsnap.api import TeamSnap
from .models import User, Member, Team, Event, Location, LineupEntry, Opponent, Availability
from lib.views import BenchcoachListView
from .forms import LineupEntryForm, LineupEntryFormSet, EventForm, EventFormSet
from .forms import EventForm, MemberForm, OpponentForm, TeamForm, LocationForm
from django.urls import reverse
from django.db.models import Case, When
from django.views import View
from django.http import HttpResponse
import benchcoachproject.models
import benchcoach.models
import teamsnap.teamsnap.api
import json
from django.http import JsonResponse
from .utils.teamsnap_object_utils import update_users, update_teams, update_events, update_members, update_locations, update_availabilities
from django.contrib import messages
from django.template.loader import render_to_string
from .utils.import_teamsnap import update_team, update_event, update_member, update_location, update_opponent, update_availability, update_teamsnap_object
from django.forms import modelformset_factory
import django.db.models
def queryset_from_ids(Model, id_list):
#https://stackoverflow.com/questions/4916851/django-get-a-queryset-from-array-of-ids-in-specific-order
@@ -40,61 +40,45 @@ def home(request):
}
return render(request, 'teamsnap/home.html', context)
class EventsTableView(View):
def get(self, request):
qs = Event.objects.all()
formset = EventFormSet(queryset=qs)
return render(request,'teamsnap/event-table.html', context={'formset':formset})
class TeamsnapObjTableView(View):
Model = None
Form = None
template = 'teamsnap/table.html'
options = {
'event': (Event, EventForm),
'location': (Location, LocationForm),
'member': (Member, MemberForm),
'opponent': (Opponent, OpponentForm),
'team': (Team, TeamForm)
}
class EventsListView(BenchcoachListView):
Model = Event
edit_url = 'teamsnap edit event'
list_url = 'teamsnap list events'
page_title = "TeamSnap Events"
title_strf = '{item.formatted_title}'
body_strf = "{item.start_date:%a, %b %-d, %-I:%M %p},\n{item.location.name}"
def post(self, request, object):
self.Model, self.Form = self.options[object]
self.Formset = modelformset_factory(
model=self.Model,
form=self.Form,
extra=0
)
formset = self.Formset(request.POST, request.FILES)
if formset.is_valid():
formset.save()
return HttpResponse(200)
else:
return HttpResponse(422)
pass
def get_context_data(self):
context = super().get_context_data()
for item in context['items']:
item['buttons'].append(
{
'label': 'Edit Lineup',
'href': reverse('teamsnap edit lineup', args=[item['id']])
}
)
return context
def get(self, request, object):
self.Model, self.Form = self.options[object]
self.Formset = modelformset_factory(
model=self.Model,
form=self.Form,
extra=0
)
qs = self.Model.objects.all()
formset = self.Formset(queryset=qs)
return render(request, self.template, context={'formset': formset})
class TeamListView(BenchcoachListView):
Model = Team
edit_url = 'teamsnap edit team'
list_url = 'teamsnap list teams'
page_title = "TeamSnap Teams"
class LocationListView(BenchcoachListView):
Model = Location
edit_url = 'teamsnap edit location'
list_url = 'teamsnap list locations'
page_title = "TeamSnap Locations"
def update_from_teamsnap_event(request):
TOKEN = benchcoachproject.models.User.objects.get(id=1).teamsnap_access_token
CLIENT = TeamSnap(token=TOKEN)
teamsnap_event_id=request.POST.get('teamsnap event')
benchcoach_event_id=request.POST.get('teamsnap event')
if teamsnap_event_id:
benchcoach_event = benchcoach.models.Event.objects.get(id=benchcoach_event_id)
teamsnap_object = benchcoach.models.Event.objects.get(id=teamsnap_event_id)
teamsnap_id = teamsnap_object.teamsnap_id
teamsnap_response = TsApiEvent.search(client=CLIENT, id=teamsnap_id)
if teamsnap_response[0]:
data = teamsnap_response[0].data
location = Location.objects.get(teamsnap_id=data['location_id'])
opponent = Team.objects.get(teamsnap_id=data['opponent_id'])
return HttpResponse(f'Success, {data}')
def sync_with_teamsnap_api(request):
def sync_teamsnapdb_with_teamsnapapi(request):
'''
This sync the internal TeamSnap Database with the TeamSnap API
'''
@@ -119,6 +103,7 @@ def sync_with_teamsnap_api(request):
obj, created = Obj.update_or_create_from_teamsnap_api(_a.data)
r[Obj.__name__].append((obj, created))
for object_name, results in r.items():
if len(r) == 0:
messages.error(request, f"Error! No {object_name} objects created or updated")
@@ -132,7 +117,7 @@ def sync_with_teamsnap_api(request):
return JsonResponse(data)
def sync_teamsnap_db(request):
def sync_teamsnapdb_to_benchcoachdb(request):
'''
This syncs the internal BenchCoach Database and the TeamSnap Database
'''
@@ -148,7 +133,7 @@ def sync_teamsnap_db(request):
r['location'] = []
for location in Location.objects.filter(team_id=TEAM_ID):
r['team/location'] += update_location(location, create_benchcoach_object=True, create_related=True)
r['location'] += update_location(location, create_benchcoach_object=True, create_related=True)
r['member'] = []
for member in Member.objects.filter(team_id=TEAM_ID, is_non_player=False):
@@ -159,8 +144,9 @@ def sync_teamsnap_db(request):
r['event'] += update_event(event, create_benchcoach_object=True, create_related=True)
r['availability'] = []
for availability in Availability.objects.filter(team_id=TEAM_ID):
for availability in Availability.objects.filter(team_id=TEAM_ID, member__is_non_player=False):
r['availability'] += update_availability(availability, create_benchcoach_object=True, create_related=True)
pass
for object_name, results in r.items():
if len(r) == 0: