Private
Public Access
1
0
Files
rowsandall/rowers/plannedsessions.py
2018-02-06 20:14:46 +01:00

126 lines
2.6 KiB
Python

# Python
from django.utils import timezone
from datetime import datetime
from datetime import timedelta
from datetime import date
import time
from django.db import IntegrityError
import uuid
from django.conf import settings
from utils import myqueue
import django_rq
queue = django_rq.get_queue('default')
queuelow = django_rq.get_queue('low')
queuehigh = django_rq.get_queue('low')
from rowers.models import (
Rower, Workout,
GeoCourse, TrainingMicroCycle,TrainingMesoCycle,TrainingMacroCycle,
TrainingPlan,PlannedSession,
)
import metrics
import numpy as np
import dataprep
# Low Level functions - to be called by higher level methods
# dummies for now
def add_workouts_plannedsession(ws,ps):
for w in ws:
w.plannedsession = ps
w.save()
return 1
def remove_workout_plannedsession(w,ps):
if w.plannedsession == ps:
w.plannedsession = None
w.save()
return 1
return 0
def clone_planned_session(ps):
ps.save()
ps.pk = None # creates new instance
ps.save()
def timefield_to_seconds_duration(t):
duration = t.hour*3600.
duration += t.minute * 60.
duration += t.second
duration += t.microsecond/1.e6
return duration
def is_session_complete(ps):
ws = Workout.objects.filter(plannedsession=ps)
score = 0
for w in ws:
if ps.sessionmode == 'distance':
score += w.distance
elif ps.sessionmode == 'time':
durationseconds = timefield_to_seconds_duration(w.duration)
score += durationseconds
elif ps.sessionmode == 'TRIMP':
trimp = dataprep.workout_trimp(w)
score += trimp
elif ps.sessionmode == 'rScore':
rscore = dataprep.workout_rscore(w)
score += rscore
ratio = score/float(ps.value)
if ratio>0.8 and ratio<1.2:
return True
return False
def rank_results(ps):
return 1
def add_team_session(t,ps):
ps.team.add(t)
ps.save()
return 1
def add_rower_session(r,ps):
ps.rower.add(r)
ps.save()
return 1
def remove_team_session(t,ps):
ps.team.remove(t)
return 1
def remove_rower_session(r,ps):
ps.rower.remove(r)
return 1
def get_sessions(r,startdate=date.today(),
enddate=date.today()+timezone.timedelta(+1000)):
sps = PlannedSession.objects.filter(
rower__in=[r],
startdate__gte=startdate,
enddate__lte=enddate,
).order_by("startdate","enddate")
return sps
def update_plannedsession(ps,cd):
for attr, value in cd.items():
setattr(ps, attr, value)
ps.save()
return 1,'Planned Session Updated'