From 44df1a6d79df95bfacedf764c69337a77e9aa07c Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Wed, 8 Jan 2025 20:05:13 +0100 Subject: [PATCH] fixes icu --- rowers/integrations/intervals.py | 69 ++++++++++++++++++++------ rowers/plannedsessions.py | 3 +- rowers/templates/plannedsessions.html | 8 --- rowers/tests/testdata/testdata.tcx.gz | Bin 3989 -> 3989 bytes rowers/utils.py | 4 ++ rowers/views/otherviews.py | 2 + rowers/views/planviews.py | 20 ++++---- 7 files changed, 72 insertions(+), 34 deletions(-) diff --git a/rowers/integrations/intervals.py b/rowers/integrations/intervals.py index 908d0943..c91d9158 100644 --- a/rowers/integrations/intervals.py +++ b/rowers/integrations/intervals.py @@ -169,7 +169,10 @@ class IntervalsIntegration(SyncIntegration): def workout_export(self, workout, *args, **kwargs) -> str: - token = self.open() + try: + token = self.open() + except NoTokenError: + return 0 dologging('intervals.icu.log', "Exporting workout {id}".format(id=workout.id)) filename = self.createworkoutdata(workout) @@ -220,6 +223,11 @@ class IntervalsIntegration(SyncIntegration): return id def get_workout_list(self, *args, **kwargs) -> int: + try: + token = self.open() + except NoTokenError: + return [] + url = self.oauth_data['base_url'] + 'athlete/0/activities?' startdate = timezone.now() - timedelta(days=30) enddate = timezone.now() + timedelta(days=1) @@ -238,7 +246,7 @@ class IntervalsIntegration(SyncIntegration): url += 'oldest=' + startdate.strftime('%Y-%m-%d') + '&newest=' + enddate.strftime('%Y-%m-%d') headers = { 'accept': '*/*', - 'authorization': 'Bearer ' + self.open(), + 'authorization': 'Bearer ' + token } response = requests.get(url, headers=headers) @@ -278,7 +286,10 @@ class IntervalsIntegration(SyncIntegration): return workouts def update_workout(self, id, *args, **kwargs) -> int: - _ = self.open() + try: + _ = self.open() + except NoTokenError: + return 0 r = self.rower headers = { @@ -308,8 +319,10 @@ class IntervalsIntegration(SyncIntegration): pass try: w.rpe = data['icu_rpe'] + if w.rpe is None: + w.rpe = 0 except KeyError: - pass + w.rpe = 0 try: w.is_commute = data['commute'] if w.is_commute is None: @@ -387,7 +400,11 @@ class IntervalsIntegration(SyncIntegration): return 1 def get_workout(self, id, *args, **kwargs) -> int: - _ = self.open() + try: + _ = self.open() + except NoTokenError: + return 0 + r = self.rower do_async = kwargs.get('do_async', True) @@ -506,7 +523,7 @@ class IntervalsIntegration(SyncIntegration): return 1 - def pair_workout_and_session(w, id): + def pair_workout_and_session(self, w, id): pass @@ -541,7 +558,11 @@ class IntervalsIntegration(SyncIntegration): return super(IntervalsIntegration, self).token_refresh(*args, **kwargs) def get_plannedsessions_list(self, *args, **kwargs): - _ = self.open() + try: + _ = self.open() + except NoTokenError: + return [] + r = self.rower headers = { @@ -562,7 +583,11 @@ class IntervalsIntegration(SyncIntegration): return data def update_plannedsession(self, ps, data, *args, **kwargs): - _ = self.open() + try: + _ = self.open() + except NoTokenError: + return 0 + r = self.rower if data['category'] == 'WORKOUT': @@ -584,7 +609,11 @@ class IntervalsIntegration(SyncIntegration): return data def get_plannedsession(self, id, *args, **kwargs): - _ = self.open() + try: + _ = self.open() + except NoTokenError: + return 0 + r = self.rower url = self.oauth_data['base_url'] + 'athlete/0/events/' + str(id) @@ -618,7 +647,11 @@ class IntervalsIntegration(SyncIntegration): return data def plannedsession_create(self, ps, *args, **kwargs): - _ = self.open() + try: + _ = self.open() + except NoTokenError: + return 0 + r = self.rower headers = { @@ -626,6 +659,7 @@ class IntervalsIntegration(SyncIntegration): } stepstext = ps.steps_intervals() + print(stepstext) category = 'WORKOUT' startdate = ps.preferreddate.strftime('%Y-%m-%dT%H:%M:%S') @@ -671,7 +705,11 @@ class IntervalsIntegration(SyncIntegration): return id def plannedsession_delete(self, ps, *args, **kwargs): - _ = self.open() + try: + _ = self.open() + except NoTokenError: + return 0 + r = self.rower headers = { @@ -750,9 +788,12 @@ class IntervalsIntegration(SyncIntegration): ps.sessionvalue = timetarget ps.save() if data['category'].lower() == 'workout': - ps.fitfile = data['fitfile'] - ps.save() - ps.update_steps() + try: + ps.fitfile = data['fitfile'] + ps.save() + ps.update_steps() + except KeyError: + pass if data['category'].lower() == 'target': ps.sessiontype = 'cycletarget' ps.sessionvalue = int(data['time_target'])/60. diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index 067133f7..43fe0642 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -25,6 +25,7 @@ from rowingdata import rowingdata as rrdata import arrow import polars as pl import json +from rowers import integrations # Python from django.utils import timezone @@ -418,7 +419,7 @@ def add_workouts_plannedsession(ws, ps, r): comments.append('Attached workout %s to session' % encoder.encode_hex(w.id)) if ps.intervals_icu_id: - integration = integrations.IntervalsIntegration(w.user) + integration = integrations.IntervalsIntegration(w.user.user) integration.pair_workout_and_session(w, ps.intervals_icu_id) if ps.sessiontype == 'coursetest': # pragma: no cover record = CourseTestResult( diff --git a/rowers/templates/plannedsessions.html b/rowers/templates/plannedsessions.html index 83728c8b..7f74a3ce 100644 --- a/rowers/templates/plannedsessions.html +++ b/rowers/templates/plannedsessions.html @@ -137,17 +137,9 @@ {% else %} - {% if request.GET.startdate %} - - {% elif request.GET.when %} - - {% else %} {% endif %} - {% endif %} {% endif %} {{ ps.sessionvalue }} diff --git a/rowers/tests/testdata/testdata.tcx.gz b/rowers/tests/testdata/testdata.tcx.gz index 2744ab2816b3d9567f090496458823e12ae72d06..74057599315de98e92bb3703760c72aba6ef0318 100644 GIT binary patch delta 16 XcmbO#KUJPxzMF&N%bL24?0x(IEw=@o delta 16 XcmbO#KUJPxzMF%izrJ=OdmldlDd7bQ diff --git a/rowers/utils.py b/rowers/utils.py index ba032e86..c9cee710 100644 --- a/rowers/utils.py +++ b/rowers/utils.py @@ -737,6 +737,10 @@ def steps_write_fit(steps): url = settings.WORKOUTS_FIT_URL+"/tofit" headers = {'Authorization': authorizationstring} + # convert to json, value of keys called wkt_step_name to string + for step in steps['steps']: + step['wkt_step_name'] = str(step['wkt_step_name']) + response = requests.post(url=url, headers=headers, json=steps) if response.status_code != 200: # pragma: no cover diff --git a/rowers/views/otherviews.py b/rowers/views/otherviews.py index 3fef3dd5..0946a477 100644 --- a/rowers/views/otherviews.py +++ b/rowers/views/otherviews.py @@ -33,7 +33,9 @@ def filmdeaths_view(request): @login_required() def download_fit(request, filename=''): + print(filename) pss = PlannedSession.objects.filter(fitfile=filename) + print(pss, len(pss)) if len(pss) != 1: # pragma: no cover raise Http404("Could not find the required file") diff --git a/rowers/views/planviews.py b/rowers/views/planviews.py index 06b24ff0..33255a17 100644 --- a/rowers/views/planviews.py +++ b/rowers/views/planviews.py @@ -1841,6 +1841,8 @@ def plannedsession_clone_view(request, id=0, userid=0): if not ps.is_template: ps.name += ' (copy)' ps.is_template = False + ps.intervals_icu_id = None + ps.rojabo_id = 0 deltadays = ps.preferreddate-ps.startdate @@ -2057,11 +2059,6 @@ def plannedsession_tointervals_view(request, id=0): r = getrequestplanrower(request) - startdate, enddate = get_dates_timeperiod(request) - startdate = startdate.date() - enddate = enddate.date() - - ps = get_object_or_404(PlannedSession, pk=id) intervals = IntervalsIntegration(request.user) @@ -2077,13 +2074,14 @@ def plannedsession_tointervals_view(request, id=0): url = reverse(plannedsession_view, kwargs={'userid': r.user.id, 'id': ps.id, }) - startdatestring = startdate.strftime('%Y-%m-%d') - enddatestring = enddate.strftime('%Y-%m-%d') - url += '?when='+startdatestring+'/'+enddatestring + thenext = request.GET.get('next', url) - next = request.GET.get('next', url) + # add ?startdate=startdate&enddate=enddate to url, with startdate ps.startdate and enddate ps.enddate + startdate = ps.startdate.strftime('%Y-%m-%d') + enddate = ps.enddate.strftime('%Y-%m-%d') + url += '?startdate='+startdate+'&enddate='+enddate - return HttpResponseRedirect(next) + return HttpResponseRedirect(thenext) @permission_required('plannedsession.change_session', fn=get_session_by_pk, raise_exception=True) @@ -2114,7 +2112,7 @@ def plannedsession_togarmin_view(request, id=0): startdatestring = startdate.strftime('%Y-%m-%d') enddatestring = enddate.strftime('%Y-%m-%d') - url += '?when='+startdatestring+'/'+enddatestring + url += '?startdate='+startdatestring+'&enddate='+enddatestring next = request.GET.get('next', url)