diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index 243e79a7..997fa5d9 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -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': diff --git a/rowers/templates/instantplan.html b/rowers/templates/instantplan.html index 897d8174..572102b8 100644 --- a/rowers/templates/instantplan.html +++ b/rowers/templates/instantplan.html @@ -10,6 +10,14 @@
  • +

    Created by: {{ plan.owner.first_name }} {{ plan.owner.last_name }}

    +

    Plan length: {{ plan.duration }} days

    +

    {{ plan.description }}

    +

    {{ plan.target }}

    +

    Goal: {{ plan.goal }}

    +

    {{ plan.hoursperweek }} hours per week

    +
  • +
  • @@ -34,6 +42,23 @@
  • +
  • +

    + 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. +

    +

    + 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. +

    +
    + + {{ form.as_table }} +
    + {% csrf_token %} +

    +
    +
{% endblock %} diff --git a/rowers/views/planviews.py b/rowers/views/planviews.py index 37c25339..95d6e24c 100644 --- a/rowers/views/planviews.py +++ b/rowers/views/planviews.py @@ -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()