diff --git a/rowers/models.py b/rowers/models.py index 2ce60e37..8db34f4e 100644 --- a/rowers/models.py +++ b/rowers/models.py @@ -508,7 +508,7 @@ class TeamRequest(models.Model): from rowers.utils import ( workflowleftpanel,workflowmiddlepanel, defaultleft,defaultmiddle,landingpages, - steps_read_fit, steps_write_fit + steps_read_fit, steps_write_fit, ps_dict_order ) from rowers.utils import geo_distance @@ -2418,11 +2418,27 @@ class PlannedSession(models.Model): filename = 'session'+encoder.encode_hex(self.pk)+'.fit' else: file = tempfile.NamedTemporaryFile(mode='w+b',suffix='.fit',dir=settings.MEDIA_ROOT) - filename = file.name + filename = file + steps = self.steps - steps['filename'] = filename + + steps['filename'] = settings.MEDIA_ROOT+'/'+filename fitfile = steps_write_fit(steps) - self.fitfile.name = fitfile + + filelist = fitfile.split('/media/') + self.fitfile.name = filelist[-1] + + # calculate approximate distance + if self.steps: + sdict, totalmeters, totalseconds = ps_dict_order(self.steps) + self.approximate_distance = int(totalmeters) + self.approximate_duration = int(totalseconds/60.) + self.criterium = 'none' + if self.sessionmode == 'time': + self.sessionvalue = self.approximate_duration + elif self.sessionmode == 'distance': + self.sessionvalue = self.approximate_distance + super(PlannedSession,self).save(*args, **kwargs) diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index 5fba2fb7..16790573 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -15,7 +15,7 @@ import uuid from django.conf import settings import pytz from dateutil import parser -from rowers.utils import myqueue,calculate_age,totaltime_sec_to_string +from rowers.utils import myqueue,calculate_age,totaltime_sec_to_string,ps_dict_order from rowers.rows import handle_uploaded_file import collections import re @@ -92,238 +92,17 @@ from rowers.tasks import ( ) from rowers.utils import totaltime_sec_to_string -def step_to_string(step): - type = 'Step' - repeatID = -1 - - nr = 0 - name = '' - intensity = '' - duration = '' - unit = '' - target = '' - repeat = '' - - durationtype = step['durationType'] - if step['durationValue'] == 0: - return '',type, -1, -1 - - if durationtype == 'Time': - unit = 'min' - value = step['durationValue'] - dd = timedelta(seconds=value/1000.) - duration = '{v}'.format(v=str(dd)) - elif durationtype == 'Distance': - unit = 'm' - value = step['durationValue']/100. - duration = int(value) - elif durationtype == 'HrLessThan': - value = step['durationValue'] - if value <= 100: - duration = 'until heart rate lower than {v}% of max'.format(v=value) - else: - duration = 'until heart rate lower than {v}'.format(v=value-100) - elif durationtype == 'HrGreaterThan': - value = step['durationValue'] - if value <= 100: - duration = 'until heart rate greater than {v}% of max'.format(v=value) - else: - duration = 'until heart rate greater than {v}'.format(v=value-100) - elif durationtype == 'PowerLessThan': - value = step['durationValue'] - targetvalue = step['targetvalue'] - if value <= 1000: - duration = 'Repeat until Power is less than {targetvalue} % of FTP'.format( - targetvalue=targetvalue - ) - else: - duration = 'Repeat until Power is less than {targetvalue} Watt'.format( - targetvalue=targetvalie - ) - elif durationtype == 'PowerGreaterThan': - value = step['durationValue'] - targetvalue = step['targetvalue'] - if value <= 1000: - duration = 'Repeat until Power is greater than {targetvalue} % of FTP'.format( - targetvalue=targetvalue - ) - else: - duration = 'Repeat until Power is greater than {targetvalue} Watt'.format( - targetvalue=targetvalie - ) - elif durationtype == 'RepeatUntilStepsCmplt': - type = 'RepeatStep' - ntimes = str(step['targetValue'])+' times:' - repeatID = step['durationValue'] - duration =ntimes - elif durationtype == 'RepeatUntilHrGreaterThan': - type = 'RepeatStep' - targetvalue = step['targetValue'] - if targetvalue <= 100: - duration = 'Repeat until Heart Rate is greater than {targetvalue} % of max'.format( - targetvalue=targetvalue - ) - else: - duration = 'Repeat until Heart Rate is greater than {targetvalue}:'.format( - targetvalue=targetvalue-100. - ) - repeatID = step['durationValue'] - elif durationtype == 'RepeatUntilHrLessThan': - type = 'RepeatStep' - targetvalue = step['targetValue'] - if targetvalue <= 100: - duration = 'Repeat until Heart Rate is less than {targetvalue} % of max'.format( - targetvalue=targetvalue - ) - else: - duration = 'Repeat until Heart Rate is less than {targetvalue}:'.format( - targetvalue=targetvalue-100. - ) - repeatID = step['durationValue'] - - - # - - try: - targettype = step['targetType'] - except KeyError: - targettype = None - - if targettype == 'HeartRate': - value = step['targetValue'] - valuelow = step['targetValueLow'] - valuehigh = step['targetValueHigh'] - - if value < 10 and value>0: - target = 'Target: Heart Rate in zone {v}'.format(v=value) - else: - if valuelow < 100: - target = 'Target: Heart Rate between {l} and {h} % of max'.format( - l = valuelow, - h = valuehigh - ) - else: - target = 'Target: Heart Rate between {l} and {h}'.format( - l = valuelow-100, - h = valuehigh+100 - ) - elif targettype == 'Power': - value = step['targetValue'] - valuelow = step['targetValueLow'] - valuehigh = step['targetValueHigh'] - - if value < 10 and value>0: - target = 'Target: Power in zone {v}'.format(v=value) - else: - if valuelow < 1000: - target = 'Target: Power between {l} and {h} % of FTP'.format( - l = valuelow, - h = valuehigh - ) - else: - target = 'Target: Power between {l} and {h} W'.format( - l = valuelow-1000, - h = valuehigh-1000 - ) - elif targettype == 'Speed': - value = step['targetValue'] - valuelow = step['targetValueLow'] - valuehigh = step['targetValueHigh'] - - if value != 0: - v = value/1000. - pace = 500./v - target = 'Target: Speed at {v} m/s'.format(v=value/1000.) - elif valuelow != 0 and valuehigh != 0: - v = valuelow/1000. - pace = 500./v - pacestringlow = to_pace(pace) - - v = valuehigh/1000. - pace = 500./v - pacestringhigh = to_pace(pace) - - target = 'Target: Speed between {l:1.2f} and {h:1.2f} m/s ({ph} to {pl} per 500m)'.format( - l = valuelow/1000., - h = valuehigh/1000., - pl = pacestringlow, - ph = pacestringhigh, - ) - elif targettype == 'Cadence': - value = step['targetValue'] - valuelow = step['targetValueLow'] - valuehigh = step['targetValueHigh'] - - if value != 0: - target = 'Target: Cadence at {v} SPM'.format(v=value) - elif valuelow != 0 and valuehigh != 0: - target = 'Target: Cadence between {l} and {h} SPM'.format( - l = valuelow/1000., - h = valuehigh/1000., - ) - - - nr = step['stepId'] - - name = step['wkt_step_name'] - - intensity = step['intensity'] - - - s = '{name} {intensity} {duration} {unit} {target} {repeat}'.format( - nr = nr, - name = name, - unit=unit, - intensity = intensity, - duration = duration, - target=target, - repeat = repeat, - ) - - if type == 'RepeatStep': - s = 'Repeat {duration}'.format(duration=duration) - - return s,type, nr, repeatID - -def ps_dict_order(d): - sdict = collections.OrderedDict({}) - steps = d['steps'] - - for step in steps: - sstring, type, stepID, repeatID = step_to_string(step) - - sdict[stepID] = { - 'string':sstring, - 'type':type, - 'stepID': stepID, - 'repeatID': repeatID, - } - - sdict2 = collections.OrderedDict(reversed(list(sdict.items()))) - - sdict3 = [] - hold = [] - holduntil = [] - spaces = '' - for key, item in sdict2.items(): - if item['type'] == 'RepeatStep': - hold.append(item) - holduntil.append(item['repeatID']) - spaces += ' ' - if item['type'] == 'Step': - item['string'] = spaces+item['string'] - sdict3.append(item) - if len(holduntil)>0 and item['stepID'] == holduntil[-1]: - sdict3.append(hold.pop()) - spaces = spaces[:-18] - holduntil.pop() - - sdict = list(reversed(sdict3)) - +# nog niet af +def ps_dict_get_durationdistance(d): + sdict = ps_dict_order(d) return sdict + + + + def ps_dict_get_description(d): - sdict = ps_dict_order(d) + sdict,totalmeters,totalseconds = ps_dict_order(d) s = '' for item in sdict: s += item['string']+'\n' @@ -331,7 +110,7 @@ def ps_dict_get_description(d): return s def ps_dict_get_description_html(d): - sdict = ps_dict_order(d) + sdict,totalmeters,totalseconds = ps_dict_order(d) s = '