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

@@ -104,7 +104,7 @@ def alert_get_stats(alert,nperiod=0):
if df.empty: if df.empty:
return { return {
'workouts':len(workouts), 'workouts':workouts.count(),
'startdate':startdate, 'startdate':startdate,
'enddate':enddate, 'enddate':enddate,
'nr_strokes':0, 'nr_strokes':0,
@@ -139,7 +139,7 @@ def alert_get_stats(alert,nperiod=0):
df.dropna(inplace=True,axis=0) df.dropna(inplace=True,axis=0)
else: else:
return { return {
'workouts':len(workouts), 'workouts':workouts.count(),
'startdate':startdate, 'startdate':startdate,
'enddate':enddate, 'enddate':enddate,
'nr_strokes':0, 'nr_strokes':0,
@@ -183,7 +183,7 @@ def alert_get_stats(alert,nperiod=0):
std = df[alert.measured.metric].std() std = df[alert.measured.metric].std()
return { return {
'workouts':len(workouts), 'workouts':workouts.count(),
'startdate':startdate, 'startdate':startdate,
'enddate':enddate, 'enddate':enddate,
'nr_strokes':nr_strokes, 'nr_strokes':nr_strokes,

View File

@@ -364,7 +364,7 @@ class Team(models.Model):
if manager.rower.rowerplan in ['plan','pro']: if manager.rower.rowerplan in ['plan','pro']:
otherteams = Team.objects.filter(manager=manager) otherteams = Team.objects.filter(manager=manager)
if len(otherteams) >= 1: if otherteams.count() >= 1:
raise ValidationError( raise ValidationError(
"Pro and Self-Coach users cannot have more than one team" "Pro and Self-Coach users cannot have more than one team"
) )
@@ -569,7 +569,7 @@ def course_length(course):
if not polygons: if not polygons:
return 0 return 0
for i in range(len(polygons)-1): for i in range(polygons.count()-1):
latitude1,longitude1 = polygon_coord_center(polygons[i]) latitude1,longitude1 = polygon_coord_center(polygons[i])
latitude2,longitude2 = polygon_coord_center(polygons[i+1]) latitude2,longitude2 = polygon_coord_center(polygons[i+1])
@@ -656,7 +656,7 @@ class CoachingGroup(models.Model):
def __len__(self): def __len__(self):
rs = Rower.objects.filter(coachinggroups__in=[self]) rs = Rower.objects.filter(coachinggroups__in=[self])
return len(rs) return rs.count()
# Extension of User with rowing specific data # Extension of User with rowing specific data
@python_2_unicode_compatible @python_2_unicode_compatible
@@ -3133,7 +3133,7 @@ def auto_delete_image_on_delete(sender,instance, **kwargs):
if instance.filename: if instance.filename:
if os.path.isfile(instance.filename): if os.path.isfile(instance.filename):
others = GraphImage.objects.filter(filename=instance.filename) others = GraphImage.objects.filter(filename=instance.filename)
if len(others) == 0: if others.count() == 0:
os.remove(instance.filename) os.remove(instance.filename)
else: else:
print("couldn't find the file "+instance.filename) print("couldn't find the file "+instance.filename)

View File

@@ -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') errors.append('For tests and training sessions, selected workouts must all be done on the same date')
return result,comments,errors 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') errors.append('For tests, you can only attach one workout')
return result,comments,errors return result,comments,errors
@@ -438,7 +438,7 @@ def get_session_metrics(ps):
ws = Workout.objects.filter(user=r,plannedsession=ps).order_by("date") ws = Workout.objects.filter(user=r,plannedsession=ps).order_by("date")
if len(ws) != 0: if ws.count() != 0:
for w in ws: for w in ws:
distancev += w.distance distancev += w.distance
durationv += timefield_to_seconds_duration(w.duration) durationv += timefield_to_seconds_duration(w.duration)
@@ -493,7 +493,7 @@ cratiocolors = {
def is_session_complete_ws(ws,ps): def is_session_complete_ws(ws,ps):
ws = ws.order_by("date") ws = ws.order_by("date")
if len(ws)==0: if ws.count()==0:
today = date.today() today = date.today()
if today > ps.enddate: if today > ps.enddate:
verdict = 'missed' verdict = 'missed'
@@ -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') errors.append('For tests and training sessions, selected workouts must all be done on the same date')
return result,comments,errors,0 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') errors.append('For tests, you can only attach one workout')
return result,comments,errors,0 return result,comments,errors,0
@@ -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') errors.append('For tests and training sessions, selected workouts must all be done on the same date')
return result,comments,errors,0 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') errors.append('For tests, you can only attach one workout')
return result,comments,errors,0 return result,comments,errors,0
@@ -1549,5 +1549,3 @@ def delete_race_result(workout,race):
for r in results: for r in results:
r.workoutid = None r.workoutid = None
r.save() r.save()

View File

@@ -297,7 +297,7 @@ def plannedsession_multiclone_view(
'enddate':enddate, 'enddate':enddate,
}) })
if len(Team.objects.filter(manager=request.user))>=1: if Team.objects.filter(manager=request.user).count()>=1:
teamform = RowerTeamForm(request.user) teamform = RowerTeamForm(request.user)
if teamid: if teamid:
teamform = RowerTeamForm(request.user,initial={'team':teamid}) teamform = RowerTeamForm(request.user,initial={'team':teamid})
@@ -587,7 +587,7 @@ def plannedsession_multicreate_view(request,
'enddate':enddate 'enddate':enddate
}) })
if len(Team.objects.filter(manager=request.user))>=1: if Team.objects.filter(manager=request.user).count()>=1:
teamform = RowerTeamForm(request.user) teamform = RowerTeamForm(request.user)
if teamid: if teamid:
teamform = RowerTeamForm(request.user,initial={'team':teamid}) teamform = RowerTeamForm(request.user,initial={'team':teamid})
@@ -623,7 +623,7 @@ def plannedsession_teamcreate_view(request,
teams = Team.objects.filter(manager=request.user) teams = Team.objects.filter(manager=request.user)
if len(teams)>0: if teams.count()>0:
teamchoices = [(team.id, team.name) for team in teams] teamchoices = [(team.id, team.name) for team in teams]
teaminitial = [str(teams[0].id)] teaminitial = [str(teams[0].id)]
else: else:
@@ -1320,7 +1320,7 @@ def plannedsessions_manage_view(request,userid=0,
else: else:
selectedworkouts = [] selectedworkouts = []
if len(selectedworkouts)==0: if selectedworkouts.count()==0:
for w in ws: for w in ws:
remove_workout_plannedsession(w,ps) remove_workout_plannedsession(w,ps)
@@ -1476,7 +1476,7 @@ def plannedsession_teamclone_view(request,id=0):
r = getrequestplanrower(request) r = getrequestplanrower(request)
teams = Team.objects.filter(manager=request.user) teams = Team.objects.filter(manager=request.user)
if len(teams)>0: if teams.count()>0:
teamchoices = [(team.id, team.name) for team in teams] teamchoices = [(team.id, team.name) for team in teams]
teaminitial = [str(teams[0].id)] teaminitial = [str(teams[0].id)]
else: else:
@@ -1596,7 +1596,7 @@ def plannedsession_edit_view(request,id=0,userid=0):
if ps.sessiontype in ['race','indoorrace']: if ps.sessiontype in ['race','indoorrace']:
raise PermissionDenied("You are not allowed to edit this planned session because it is a race") raise PermissionDenied("You are not allowed to edit this planned session because it is a race")
if ps.team.all() or len(ps.rower.all())>1: if ps.team.all() or ps.rower.all().count()>1:
url = reverse(plannedsession_teamedit_view, url = reverse(plannedsession_teamedit_view,
kwargs={ kwargs={
'sessionid':id, 'sessionid':id,
@@ -2941,6 +2941,3 @@ def planmacrocyclebymonth(request,id=0,userid=0):
'thismesoid':str(mesos[0].id)}) 'thismesoid':str(mesos[0].id)})
return HttpResponseRedirect(url) return HttpResponseRedirect(url)