Private
Public Access
1
0

not really importing the steps correctly

This commit is contained in:
2024-12-15 17:04:17 +01:00
parent b34a415961
commit 8c5dd6bb91
3 changed files with 42 additions and 35 deletions

View File

@@ -322,30 +322,23 @@ class IntervalsIntegration(SyncIntegration):
} }
# first get the folders - we need the folder id for the next call # first get the folders - we need the folder id for the next call
url = self.oauth_data['base_url'] + 'athlete/0/folders' url = self.oauth_data['base_url'] + 'athlete/0/events?category=WORKOUT'
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if response.status_code != 200: if response.status_code != 200:
return [] return []
data = response.json() data = response.json()
# get all elements in the list where start_date_local is not None
folders = [x for x in data if x['start_date_local']]
for plan in folders:
plan_start_date = arrow.get(plan['start_date_local']).datetime
for session in plan["children"]:
session["date"] = (plan_start_date+timedelta(days=session["day"])).date()
return folders return data
def get_plannedsession(self, id, *args, **kwargs): def get_plannedsession(self, id, *args, **kwargs):
_ = self.open() _ = self.open()
r = self.rower r = self.rower
url = self.oauth_data['base_url'] + 'athlete/0/workouts/' + str(id) url = self.oauth_data['base_url'] + 'athlete/0/events/' + str(id)
headers = { headers = {
'Authorization': 'Bearer ' + r.intervals_token, 'Authorization': 'Bearer ' + r.intervals_token,
} }
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if response.status_code != 200: if response.status_code != 200:
@@ -354,6 +347,19 @@ class IntervalsIntegration(SyncIntegration):
data = response.json() data = response.json()
# get file from athlete/0/events/{id}/downloadfit
url = self.oauth_data['base_url'] + 'athlete/0/events/' + str(id) + '/downloadfit'
response = requests.get(url, headers=headers)
if response.status_code != 200:
dologging('intervals.icu.log', response.text)
return 0
filename = 'media/planned_' + str(id) + '.fit'
with open(filename, 'wb') as f:
f.write(response.content)
data['fitfile'] = filename
return data return data

View File

@@ -6,7 +6,7 @@
{% block main %} {% block main %}
<h1>Sessions on Intervals.icu</h1> <h1>Sessions on Intervals.icu</h1>
{% if folders %} {% if sessions %}
<ul class="main-content"> <ul class="main-content">
<li class="grid_4"> <li class="grid_4">
<form enctype="multipart/form-data" method="post"> <form enctype="multipart/form-data" method="post">
@@ -16,7 +16,6 @@
<thead> <thead>
<tr> <tr>
<th>Import</th> <th>Import</th>
<th>Plan</th>
<th>Date</th> <th>Date</th>
<th>Description</th> <th>Description</th>
<th>Type</th> <th>Type</th>
@@ -24,20 +23,17 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for folder in folders %} {% for session in sessions %}
{% for session in folder.children %}
<tr> <tr>
<td> <td>
<input type="checkbox" name="session" value="{{ session.id }}"> <input type="checkbox" name="session" value="{{ session.id }}">
</td> </td>
<td>{{ folder.name }}</td> <td>{{ session.start_date_local }}</td>
<td>{{ session.date }}</td>
<td>{{ session.description }}</td> <td>{{ session.description }}</td>
<td>{{ session.type }}</td> <td>{{ session.type }}</td>
<td>{{ session.icu_training_load }}</td> <td>{{ session.icu_training_load }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
{% endfor %}
</tbody> </tbody>
</table> </table>
</form> </form>

View File

@@ -699,28 +699,33 @@ def plannedsession_intervalsimport_view(request, message="", userid=0):
sessions_list = integration.get_plannedsessions_list() sessions_list = integration.get_plannedsessions_list()
if request.method == 'POST': # pragma: no cover if request.method == 'POST': # pragma: no cover
try: tdict = dict(request.POST.lists())
tdict = dict(request.POST.lists()) sessionids = [id for id in tdict['session']]
print(tdict) for sessionid in sessionids:
ids = tdict['session'] sessiondata = integration.get_plannedsession(sessionid)
sessionids = [int(id) for id in ids] if sessiondata:
for sessionid in sessionids: ps = PlannedSession(
try: name=sessiondata['name'],
_ = integration.get_plannedsession(sessionid) comment=sessiondata['description'],
except NoTokenError: startdate=arrow.get(sessiondata['start_date_local']).datetime,
pass enddate=arrow.get(sessiondata['end_date_local']).datetime,
messages.info( preferreddate=arrow.get(sessiondata['start_date_local']).datetime,
request, sessionsport=mytypes.intervalsmappinginv[sessiondata['type']],
'Your Intervals.icu planned sessions will be imported in the background.' sessiontype='session',
' It may take a few minutes before they appear.') intervals_icu_id=sessiondata['id'],
manager=request.user,
fitfile=sessiondata['fitfile'],
)
ps.save()
ps.rower.add(r)
ps.update_steps()
url = reverse('plannedsessions_view') url = reverse('plannedsessions_view')
return HttpResponseRedirect(url) return HttpResponseRedirect(url)
except KeyError:
pass
return render(request, 'intervals_list_import.html', return render(request, 'intervals_list_import.html',
{ {
'folders': sessions_list, 'sessions': sessions_list,
'rower': r, 'rower': r,
'active': 'nav-plans', 'active': 'nav-plans',
}) })