separated out teamsnap update buttons
This commit is contained in:
@@ -18,6 +18,9 @@ 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
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
|
||||
|
||||
def queryset_from_ids(Model, id_list):
|
||||
#https://stackoverflow.com/questions/4916851/django-get-a-queryset-from-array-of-ids-in-specific-order
|
||||
@@ -33,10 +36,16 @@ def home(request):
|
||||
current_benchcoach_user = request.user
|
||||
current_teamsnap_user = request.user.profile.teamsnap_user
|
||||
current_teamsnap_team = request.user.profile.teamsnapsettings.managed_team
|
||||
teamsnap_objects = {}
|
||||
for obj in [Availability, Event, LineupEntry, Location, Member, Opponent, Team, User]:
|
||||
teamsnap_objects[obj.__name__.lower()] = {}
|
||||
teamsnap_objects[obj.__name__.lower()]['object_count']=obj.objects.count()
|
||||
|
||||
context= {
|
||||
'benchcoach_user': current_benchcoach_user,
|
||||
'teamsnap_user': current_teamsnap_user,
|
||||
'teamsnap_team':current_teamsnap_team
|
||||
'teamsnap_team':current_teamsnap_team,
|
||||
'teamsnap_objects': teamsnap_objects
|
||||
}
|
||||
return render(request, 'teamsnap/home.html', context)
|
||||
|
||||
@@ -78,6 +87,85 @@ class TeamsnapObjTableView(View):
|
||||
formset = self.Formset(queryset=qs)
|
||||
return render(request, self.template, context={'formset': formset})
|
||||
|
||||
@login_required()
|
||||
def update_teamsnapdb_from_teamsnapapi(request, object_name, object_id=None):
|
||||
TOKEN = request.user.profile.teamsnap_access_token
|
||||
USER_ID = request.user.profile.teamsnap_user.id
|
||||
TEAM_ID = request.user.profile.teamsnapsettings.managed_team.id
|
||||
CLIENT = TeamSnap(token=TOKEN)
|
||||
|
||||
Object = {
|
||||
obj.__name__.lower():obj
|
||||
for obj in
|
||||
[Availability, Event, LineupEntry, Location, Member, Opponent, Team, User]
|
||||
}.get(object_name)
|
||||
|
||||
r = {}
|
||||
|
||||
for Obj in [Object]:
|
||||
r[Obj.__name__.lower()] = []
|
||||
a = Obj.ApiObject.search(CLIENT, team_id=TEAM_ID)
|
||||
for _a in a:
|
||||
obj, created = Obj.update_or_create_from_teamsnap_api(_a.data)
|
||||
r[Obj.__name__.lower()].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")
|
||||
else:
|
||||
result = [created for obj, created in results]
|
||||
messages.success(request,
|
||||
f"Success! {sum(result)} {object_name} objects created, {len(result) - sum(result)} {object_name} objects updated.")
|
||||
|
||||
return redirect('teamsnap home')
|
||||
|
||||
@login_required()
|
||||
def send_to_benchcoach(request, object_name):
|
||||
Object = {
|
||||
obj.__name__.lower(): obj
|
||||
for obj in
|
||||
[Availability, Event, LineupEntry, Location, Member, Opponent, Team, User]
|
||||
}.get(object_name)
|
||||
|
||||
TEAM_ID = request.user.profile.teamsnapsettings.managed_team.id
|
||||
r = {}
|
||||
|
||||
r[object_name]=[]
|
||||
|
||||
if object_name == 'team':
|
||||
for team in Object.objects.filter(id=TEAM_ID):
|
||||
r[object_name] += update_opponent(team, create_benchcoach_object=True, create_related=True)
|
||||
|
||||
if object_name == 'opponent':
|
||||
for team in Object.objects.filter(team_id=TEAM_ID):
|
||||
r[object_name] += update_team(team, create_benchcoach_object=True, create_related=True)
|
||||
|
||||
if object_name == 'location':
|
||||
for location in Location.objects.filter(team_id=TEAM_ID):
|
||||
r[object_name] += update_location(location, create_benchcoach_object=True, create_related=True)
|
||||
|
||||
if object_name == 'member':
|
||||
for member in Member.objects.filter(team_id=TEAM_ID, is_non_player=False):
|
||||
r[object_name] += update_member(member, create_benchcoach_object=True, create_related=True)
|
||||
|
||||
if object_name == 'event':
|
||||
for event in Event.objects.filter(team_id=TEAM_ID):
|
||||
r[object_name] += update_event(event, create_benchcoach_object=True, create_related=True)
|
||||
|
||||
if object_name == 'availability':
|
||||
for availability in Availability.objects.filter(team_id=TEAM_ID, member__is_non_player=False):
|
||||
r[object_name] += update_availability(availability, create_benchcoach_object=True, create_related=True)
|
||||
|
||||
for object_name, results in r.items():
|
||||
if len(r) == 0:
|
||||
messages.error(request, f"Error! No {object_name} objects created or updated")
|
||||
else:
|
||||
result = [created for obj, created in results]
|
||||
messages.success(request,
|
||||
f"Success! {sum(result)} {object_name} objects created, {len(result) - sum(result)} {object_name} objects updated.")
|
||||
|
||||
return redirect('teamsnap home')
|
||||
|
||||
def sync_teamsnapdb_with_teamsnapapi(request):
|
||||
'''
|
||||
This sync the internal TeamSnap Database with the TeamSnap API
|
||||
@@ -96,10 +184,11 @@ def sync_teamsnapdb_with_teamsnapapi(request):
|
||||
obj, created = Obj.update_or_create_from_teamsnap_api(_a.data)
|
||||
r[Obj.__name__].append((obj, created))
|
||||
|
||||
for Obj in [Event, Availability, Location, Member, Opponent, Team]:
|
||||
for Obj in [Event, Availability]:
|
||||
r[Obj.__name__] = []
|
||||
a = Obj.ApiObject.search(CLIENT, team_id=TEAM_ID)
|
||||
for _a in a:
|
||||
print(f"importing {_a}")
|
||||
obj, created = Obj.update_or_create_from_teamsnap_api(_a.data)
|
||||
r[Obj.__name__].append((obj, created))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user