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 %}