Private
Public Access
1
0

replaced len with count() on a few queries

This commit is contained in:
Sander Roosendaal
2019-10-31 18:32:58 +01:00
parent 9d6eabca19
commit df80860ed1
4 changed files with 248 additions and 253 deletions

View File

@@ -62,7 +62,7 @@ def checkscores(r,macrocycles):
m.plantrimp = 0
m.actualtrimp = 0
mesocycles = TrainingMesoCycle.objects.filter(
plan=m,
type='userdefined').order_by("startdate")
@@ -76,19 +76,19 @@ def checkscores(r,macrocycles):
me.actualrscore = 0
me.plantrimp = 0
me.actualtrimp = 0
microcycles = TrainingMicroCycle.objects.filter(
plan=me,
type='userdefined').order_by("startdate")
for mm in microcycles:
sps = get_sessions(r,startdate=mm.startdate,enddate=mm.enddate)
# sps = PlannedSession.objects.filter(
# rower = r,
# startdate__lte=mm.enddate,
# enddate__gte=mm.startdate)
mm.plantime = 0
mm.actualtime = 0
@@ -139,11 +139,11 @@ def checkscores(r,macrocycles):
m.plantrimp += me.plantrimp
m.actualtrimp += me.actualtrimp
if m.type == 'userdefined':
m.save()
def get_execution_report(rower,startdate,enddate,plan=None):
if plan:
@@ -188,7 +188,7 @@ def get_execution_report(rower,startdate,enddate,plan=None):
startdates = []
planned = []
executed = []
for mm in micros:
plannedscore = 0
actualscore = 0
@@ -219,7 +219,7 @@ def get_execution_report(rower,startdate,enddate,plan=None):
plannedscore += 60.
elif ps.sessionmode == 'TRIMP':
plannedscore += ps.sessionvalue/2.
for w in ws:
if w.rscore != 0:
if ratio > 0:
@@ -234,19 +234,19 @@ def get_execution_report(rower,startdate,enddate,plan=None):
actualscore += w.hrtss
else:
plannedscore += 60
actualscore += 0
actualscore += 0
else:
minutes = w.duration.hour*60+w.duration.minute
if ratio > 0:
plannedscore += minutes/ratio
else:
plannedscore += 60
actualscore += minutes
actualscore = int(actualscore)
plannedscore = int(plannedscore)
startdates += [mm.startdate]
planned += [plannedscore]
executed += [actualscore]
@@ -272,14 +272,14 @@ def get_indoorraces(workout):
if workout.duration.second == 0 and workout.duration.microsecond == 0:
duration = 60*workout.duration.hour+workout.duration.minute
races2 = VirtualRace.objects.filter(
sessiontype='indoorrace',
startdate__lte=workout.date,
enddate__gte=workout.date,
sessionmode='time',
sessionvalue=duration)
races = races1 | races2
else:
races = races1
@@ -290,13 +290,13 @@ def get_indoorraces(workout):
userid=workout.user.id)
races = [r.race for r in registrations]
return races
def get_todays_micro(plan,thedate=date.today()):
thismicro = None
thismacro = TrainingMacroCycle.objects.filter(
plan=plan,
startdate__lte = thedate,
@@ -334,7 +334,7 @@ def add_workouts_plannedsession(ws,ps,r):
errors.append('For tests and training sessions, selected workouts must all be done on the same date')
return result,comments,errors
if len(ws)>1 and ps.sessiontype == 'test':
if ws.count()>1 and ps.sessiontype == 'test':
errors.append('For tests, you can only attach one workout')
return result,comments,errors
@@ -438,7 +438,7 @@ def get_session_metrics(ps):
ws = Workout.objects.filter(user=r,plannedsession=ps).order_by("date")
if len(ws) != 0:
if ws.count() != 0:
for w in ws:
distancev += w.distance
durationv += timefield_to_seconds_duration(w.duration)
@@ -493,7 +493,7 @@ cratiocolors = {
def is_session_complete_ws(ws,ps):
ws = ws.order_by("date")
if len(ws)==0:
if ws.count()==0:
today = date.today()
if today > ps.enddate:
verdict = 'missed'
@@ -519,7 +519,7 @@ def is_session_complete_ws(ws,ps):
'way over target': 1.5
}
if ps.criterium == 'none':
if ps.sessiontype == 'session':
cratiomin = 0.8
@@ -549,7 +549,7 @@ def is_session_complete_ws(ws,ps):
elif rscore == 0:
trimp,hrtss = dataprep.workout_trimp(w)
score += hrtss
if not completiondate and score>=cratiomin*value:
completiondate = w.date
@@ -646,7 +646,7 @@ def is_session_complete_ws(ws,ps):
mode='coursetest')
return (0,'not done',None)
else:
if not completiondate:
@@ -657,7 +657,7 @@ def is_session_complete_ws(ws,ps):
def is_session_complete(r,ps):
verdict = 'not done'
if r not in ps.rower.all():
return 0,'not assigned',None
@@ -686,7 +686,7 @@ def add_rower_session(r,ps):
elif ps.manager.rower == r and r.rowerplan != 'freecoach':
ps.rower.add(r)
ps.save()
return 0
def remove_team_session(t,ps):
@@ -722,9 +722,9 @@ def get_dates_timeperiod(request,startdatestring='',enddatestring='',
except ValueError:
startdate = parser.parse(startdatestring,fuzzy=True)
enddate = parser.parse(enddatestring, fuzzy=True)
return startdate,enddate
daterangetester = re.compile('^(\d+-\d+-\d+)\/(\d+-\d+-\d+)')
if timeperiod=='today':
@@ -770,7 +770,7 @@ def get_dates_timeperiod(request,startdatestring='',enddatestring='',
elif timeperiod=='lastyear':
today = date.today()
startdate = today-timezone.timedelta(days=365)
enddate = today+timezone.timedelta(days=1)
enddate = today+timezone.timedelta(days=1)
elif daterangetester.match(timeperiod):
tstartdatestring = daterangetester.match(timeperiod).group(1)
tenddatestring = daterangetester.match(timeperiod).group(2)
@@ -783,18 +783,18 @@ def get_dates_timeperiod(request,startdatestring='',enddatestring='',
startdate = startdate2
except ValueError:
startdate = date.today()
enddate = date.today()
enddate = date.today()
else:
startdate = date.today()
enddate = date.today()
if startdatestring != '':
try:
startdate = iso8601.parse_date(startdatestring)
except ParseError:
pass
if enddatestring != '':
try:
enddate = iso8601.parse_date(enddatestring)
@@ -834,7 +834,7 @@ def get_sessions(r,startdate=date.today(),
rower__in=[r],
startdate__lte=enddate,
enddate__gte=startdate,
is_template=False,
is_template=False,
).order_by("preferreddate","startdate","enddate").exclude(
sessiontype='race').exclude(sessiontype='indoorrace')
@@ -871,14 +871,14 @@ def update_indoorvirtualrace(ps,cd):
value.replace("\r\n", "&#10");
value.replace("\n", "&#10");
setattr(ps, attr, value)
timezone_str = cd['timezone']
# correct times
startdatetime = datetime.combine(cd['startdate'],cd['start_time'])
enddatetime = datetime.combine(cd['enddate'],cd['end_time'])
startdatetime = pytz.timezone(timezone_str).localize(
startdatetime
)
@@ -910,7 +910,7 @@ def update_indoorvirtualrace(ps,cd):
ps.registration_closure = registration_closure
ps.timezone = timezone_str
ps.save()
return 1,'Virtual Race Updated'
@@ -924,14 +924,14 @@ def update_virtualrace(ps,cd):
setattr(ps, attr, value)
# correct times
course = cd['course']
geocourse = GeoCourse.objects.get(id= course.id)
timezone_str = get_course_timezone(geocourse)
startdatetime = datetime.combine(cd['startdate'],cd['start_time'])
enddatetime = datetime.combine(cd['enddate'],cd['end_time'])
startdatetime = pytz.timezone(timezone_str).localize(
startdatetime
)
@@ -963,7 +963,7 @@ def update_virtualrace(ps,cd):
ps.registration_closure = registration_closure
ps.timezone = timezone_str
ps.save()
return 1,'Virtual Race Updated'
@@ -977,12 +977,12 @@ def race_rower_status(r,race):
resultobj = VirtualRaceResult
else:
resultobj = IndoorVirtualRaceResult
vs = resultobj.objects.filter(userid=r.id,race=race)
if vs:
has_registered = True
is_complete = vs[0].coursecompleted
return is_complete,has_registered
def race_can_edit(r,race):
@@ -999,7 +999,7 @@ def race_can_edit(r,race):
return True
else:
return False
return False
@@ -1058,7 +1058,7 @@ def race_can_adddiscipline(r,race):
if race.sessiontype != 'race':
return False
records = VirtualRaceResult.objects.filter(
userid=r.id,
race=race)
@@ -1066,7 +1066,7 @@ def race_can_adddiscipline(r,race):
if not records:
return False
start_time = race.start_time
start_date = race.startdate
startdatetime = datetime.combine(start_date,start_time)
@@ -1086,24 +1086,24 @@ def race_can_adddiscipline(r,race):
return False
return False
def race_can_withdraw(r,race):
if race.sessiontype == 'race':
recordobj = VirtualRaceResult
else:
recordobj = IndoorVirtualRaceResult
records = recordobj.objects.filter(
userid=r.id,
race=race
)
if not records:
return False
start_time = race.start_time
start_date = race.startdate
startdatetime = datetime.combine(start_date,start_time)
@@ -1114,7 +1114,7 @@ def race_can_withdraw(r,race):
registration_closure = race.registration_closure
if registration_closure is None or registration_closure == '':
registration_closure = startdatetime
if timezone.now() > registration_closure:
return False
elif timezone.now() > startdatetime:
@@ -1145,7 +1145,7 @@ def email_submit_race(r,race,workoutid):
w.distance = race.sessionvalue
w.save()
if race_can_register(r,race):
teamname = ''
weightcategory = w.weightcategory
@@ -1213,16 +1213,16 @@ def email_submit_race(r,race,workoutid):
record = records[0]
workouts = Workout.objects.filter(id=w.id)
result,comments,errors,jobid = add_workout_indoorrace(
workouts,race,r,recordid=record.id
)
if result:
otherrecords = IndoorVirtualRaceResult.objects.filter(
race = race)
for otherrecord in otherrecords:
otheruser = Rower.objects.get(id=otherrecord.userid)
othername = otheruser.user.first_name+' '+otheruser.user.last_name
@@ -1241,18 +1241,18 @@ def email_submit_race(r,race,workoutid):
else:
return 0
else:
return 0
return 0
def race_can_register(r,race):
if race.sessiontype == 'race':
recordobj = VirtualRaceResult
else:
recordobj = IndoorVirtualRaceResult
records = recordobj.objects.filter(
userid=r.id,
race=race)
@@ -1330,7 +1330,7 @@ def add_workout_indoorrace(ws,race,r,recordid=0):
errors.append('For tests and training sessions, selected workouts must all be done on the same date')
return result,comments,errors,0
if len(ws)>1 and race.sessiontype == 'test':
if ws.count()>1 and race.sessiontype == 'test':
errors.append('For tests, you can only attach one workout')
return result,comments,errors,0
@@ -1343,7 +1343,7 @@ def add_workout_indoorrace(ws,race,r,recordid=0):
errors.append('For tests, you can only attach one workout')
return result,comments,errors,0
username = r.user.first_name+' '+r.user.last_name
if r.birthdate:
@@ -1388,10 +1388,10 @@ def add_workout_indoorrace(ws,race,r,recordid=0):
if ws[0].workouttype != record.boatclass:
errors.append('Your workout boat class is different than on your race registration')
return 0,comments,errors,0
if ws[0].workouttype not in mytypes.otetypes:
errors.append('You must submit a indoor rowing workout')
return 0,comments, errors, 0
return 0,comments, errors, 0
if ws[0].weightcategory != record.weightcategory:
errors.append('Your workout weight category did not match the weight category you registered')
@@ -1416,7 +1416,7 @@ def add_workout_indoorrace(ws,race,r,recordid=0):
otherrecord.workoutid = None
otherrecord.coursecompleted = False
otherrecord.save()
record.coursecompleted = True
record.workoutid = ws[0].id
@@ -1424,15 +1424,15 @@ def add_workout_indoorrace(ws,race,r,recordid=0):
ws[0].privacy = 'visible'
ws[0].save()
comments.append('Workouts submitted to virtual events have to be public. We have changed the workout to a public workout.')
record.save()
add_workouts_plannedsession(ws,race,r)
return result,comments,errors,0
def add_workout_race(ws,race,r,splitsecond=0,recordid=0):
result = 0
comments = []
@@ -1458,7 +1458,7 @@ def add_workout_race(ws,race,r,splitsecond=0,recordid=0):
errors.append('For tests and training sessions, selected workouts must all be done on the same date')
return result,comments,errors,0
if len(ws)>1 and race.sessiontype == 'test':
if ws.count()>1 and race.sessiontype == 'test':
errors.append('For tests, you can only attach one workout')
return result,comments,errors,0
@@ -1471,7 +1471,7 @@ def add_workout_race(ws,race,r,splitsecond=0,recordid=0):
errors.append('For tests, you can only attach one workout')
return result,comments,errors,0
username = r.user.first_name+' '+r.user.last_name
if r.birthdate:
@@ -1498,11 +1498,11 @@ def add_workout_race(ws,race,r,splitsecond=0,recordid=0):
if ws[0].workouttype not in mytypes.otwtypes:
errors.append('You have to submit a rowing on water workout')
return 0,comments,errors,0
if ws[0].workouttype != record.boatclass:
errors.append('Your workout boat class is different than on your race registration')
return 0,comments,errors,0
if ws[0].boattype != record.boattype:
errors.append('Your workout boat type did not match the boat type you registered')
return 0,comments,errors,0
@@ -1530,7 +1530,7 @@ def add_workout_race(ws,race,r,splitsecond=0,recordid=0):
otherrecord.workoutid = None
otherrecord.coursecompleted = False
otherrecord.save()
if ws[0].privacy == 'private':
ws[0].privacy = 'visible'
ws[0].save()
@@ -1538,10 +1538,10 @@ def add_workout_race(ws,race,r,splitsecond=0,recordid=0):
job = myqueue(queue,handle_check_race_course,ws[0].csvfilename,
ws[0].id,race.course.id,record.id,splitsecond=splitsecond)
add_workouts_plannedsession(ws,race,r)
return result,comments,errors,job.id
def delete_race_result(workout,race):
@@ -1549,5 +1549,3 @@ def delete_race_result(workout,race):
for r in results:
r.workoutid = None
r.save()