Private
Public Access
1
0

copy/pasts plan to calendar

This commit is contained in:
Sander Roosendaal
2021-03-11 19:42:20 +01:00
parent 09558e848b
commit 99b1514d13
3 changed files with 111 additions and 5 deletions

View File

@@ -1052,6 +1052,27 @@ def get_workouts_session(r,ps):
return ws
def create_sessions_from_json(plansteps,athletes,startdate,manager):
trainingdays = plansteps['plan']['trainingDays']
for day in trainingdays:
for workout in day['workouts']:
ps = PlannedSession(
startdate = startdate+timedelta(days=day['order']),
enddate = startdate,
preferreddate = startdate,
sessionsport = 'water', # change this
name = workout['workoutName'],
steps = workout,
manager = manager,
sessionmode = 'time',
comment = workout['description']
)
ps.save()
for athlete in athletes:
add_rower_session(athlete,ps)
def update_plannedsession(ps,cd):
for attr, value in cd.items():
if attr == 'comment':

View File

@@ -10,6 +10,14 @@
<ul class="main-content">
<li class="grid_4">
<p>Created by: {{ plan.owner.first_name }} {{ plan.owner.last_name }}</p>
<p>Plan length: {{ plan.duration }} days</p>
<p>{{ plan.description }}</p>
<p>{{ plan.target }}</p>
<p>Goal: {{ plan.goal }}</p>
<p>{{ plan.hoursperweek }} hours per week</p>
</li>
<li class="grid_2">
<table width="100%" class="listtable">
<thead>
<tr>
@@ -34,6 +42,23 @@
</tbody>
</table>
</li>
<li class="grid_2">
<p>
When you submit this form, a training plan will be created based on {{ plan.name }}, ending at your target date,
and the sessions will be copied to your session calendar.
</p>
<p>
You can select the end date manually or use the training target (if you have any), and the plan will start at
the date it needs to complete in time.
</p>
<form enctype="multipart/form-data" action="" method="post">
<table>
{{ form.as_table }}
</table>
{% csrf_token %}
<p><input class="button" type="submit" value="Create Plan and Add Sessions"></p>
</form>
</li>
</ul>
{% endblock %}

View File

@@ -2433,6 +2433,9 @@ def rower_view_instantplan(request,id='',userid=0):
if not id:
raise Http404("Plan does not exist")
plan = InstantPlan.objects.get(uuid=id)
authorizationstring = 'Bearer '+settings.WORKOUTS_FIT_TOKEN
url = settings.WORKOUTS_FIT_URL+"/trainingplan/"+id
headers = {'Authorization':authorizationstring}
@@ -2442,13 +2445,13 @@ def rower_view_instantplan(request,id='',userid=0):
messages.error(request,"Could not connect to the training plan server")
return HttpResponseRedirect(reverse('rower_select_instantplan'))
plan = response.json()
trainingdays = plan['plan']['trainingDays']
plansteps = response.json()
trainingdays = plansteps['plan']['trainingDays']
trainingdays2 = []
nextday = trainingdays.pop(0)
for i in range(plan['plan']['duration']):
for i in range(plansteps['plan']['duration']):
if nextday['order'] == i+1:
nextday['week'] = (divmod(i,7)[0])+1
trainingdays2.append(nextday)
@@ -2465,6 +2468,61 @@ def rower_view_instantplan(request,id='',userid=0):
}
)
targets = TrainingTarget.objects.filter(
rowers=r,
date__gte=datetime.date.today(),
).order_by("-date")
if request.method == 'POST':
form = TrainingPlanForm(request.POST,user=request.user)
if form.is_valid():
plansteps = response.json()
name = form.cleaned_data['name']
try:
target = form.cleaned_data['target']
except KeyError:
try:
targetid = request.POST['target']
if targetid != '':
target = TrainingTarget.ojects.get(id=int(targetid))
else:
target = None
except KeyError:
target = None
enddate = form.cleaned_data['enddate']
notes = form.cleaned_data['notes']
status = form.cleaned_data['status']
if target:
enddate = target.date
startdate = enddate-datetime.timedelta(days=plan.duration+1)
try:
athletes = form.cleaned_data['rowers']
except KeyError:
athletes = [r]
p = TrainingPlan(
name=name,
target=target,
manager=r,
startdate=startdate,
enddate=enddate,status=status,
notes=notes,
)
p.save()
for athlete in athletes:
if can_plan_user(request.user,athlete):
p.rowers.add(athlete)
create_sessions_from_json(plansteps,athletes,startdate,r.user)
else:
form = TrainingPlanForm(targets=targets,initial={'status':True,'rowers':[r]},
user=request.user)
breadcrumbs = [
{
'url':reverse('plannedsessions_view'),
@@ -2484,7 +2542,7 @@ def rower_view_instantplan(request,id='',userid=0):
'id':id,
# 'userid':userid,
}),
'name':plan['name']
'name':plan.name
}
]
@@ -2493,8 +2551,10 @@ def rower_view_instantplan(request,id='',userid=0):
{
'rower':r,
'active':'nav-plan',
'plan':plan,
'plan': plan,
'trainingdays':trainingdays2,
'breadcrumbs':breadcrumbs,
'form':form,
})
@login_required()