diff --git a/rowers/polarstuff.py b/rowers/polarstuff.py index 4c718145..6d5d804a 100644 --- a/rowers/polarstuff.py +++ b/rowers/polarstuff.py @@ -34,9 +34,13 @@ from rowers.models import checkworkoutuser import dataprep from dataprep import columndict +from io import StringIO + import stravalib from stravalib.exc import ActivityUploadFailed,TimeoutExceeded +from django_mailbox.models import Message,Mailbox,MessageAttachment + from rowsandall_app.settings import ( POLAR_CLIENT_ID, POLAR_REDIRECT_URI, POLAR_CLIENT_SECRET, ) @@ -148,6 +152,9 @@ def get_polar_workouts(user): if response.status_code == 201: + + workoutsbox = Mailbox.objects.filter(name='workouts')[0] + transactionid = response.json()['transaction-id'] url = baseurl+'/users/{userid}/exercise-transactions/{transactionid}'.format( transactionid = transactionid, @@ -157,26 +164,29 @@ def get_polar_workouts(user): response = requests.get(url, headers=headers) if response.status_code == 200: exerciseurls = response.json()['exercises'] - print 'Found {n} workouts'.format( - n=len(exerciseurls) - ) for exerciseurl in exerciseurls: response = requests.get(exerciseurl,headers=headers) if response.status_code == 200: exercise_dict = response.json() - print response.json() tcxuri = exerciseurl+'/tcx' response = requests.get(tcxuri,headers=headers2) if response.status_code == 200: - filename = 'media/polarimport{id}.tcx'.format( - id = exercise_dict['id'] - ) - print 'downloading workout {id} to {filename}'.format( - filename=filename, + filename = 'media/mailbox_attachments/polarimport{id}.tcx'.format( id = exercise_dict['id'] ) + with open(filename,'wb') as fop: - fop.write(response.text) + fop.write(response.content) + + msg = Message(mailbox=workoutsbox, + from_header=user.email, + subject = 'Import from Polar Flow') + + msg.save() + + a = MessageAttachment(message=msg,document=filename) + a.save() + exercise_dict['filename'] = filename else: exercise_dict['filename'] = '' diff --git a/rowers/templates/polar_list_import.html b/rowers/templates/polar_list_import.html index 0792fd58..3e61acf2 100644 --- a/rowers/templates/polar_list_import.html +++ b/rowers/templates/polar_list_import.html @@ -5,16 +5,18 @@ {% block title %}Workouts{% endblock %} {% block content %} -
Due to a limitation in Polar Flow's API, we can only access new workouts.
+Due to a limitation in Polar Flow's API, we can only access new workouts. We + have imported the following workouts and are now processing them. You will + receive email when the workouts are ready.
{% if workouts %}| Import | +Imported | Date | Duration | Distance | @@ -25,7 +27,12 @@ {% for workout in workouts %}
|---|---|---|---|---|
| - Import | + {% if workout|lookup:'filename' == '' %} + NO + {% else %} + YES + {% endif %} +{{ workout|lookup:'starttime' }} | {{ workout|lookup:'duration' }} | {{ workout|lookup:'distance' }} m | diff --git a/rowers/urls.py b/rowers/urls.py index 2550da97..be8f6ea8 100644 --- a/rowers/urls.py +++ b/rowers/urls.py @@ -329,7 +329,6 @@ urlpatterns = [ url(r'^workout/sporttracksimport/(?P