Private
Public Access
1
0

not working yet, need to complete in tasks.py

This commit is contained in:
Sander Roosendaal
2018-06-22 16:45:19 +02:00
parent 486f47bbb4
commit 80cbcb9512
4 changed files with 236 additions and 2 deletions

View File

@@ -16,6 +16,8 @@ from math import sin,cos,atan2,sqrt
import os,sys
import gzip
from pytz import timezone as tz,utc
# Django
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse,JsonResponse
@@ -32,7 +34,8 @@ from rowers.models import Rower,Workout
from rowers.models import checkworkoutuser
import dataprep
from dataprep import columndict
from utils import uniqify,isprorower
from uuid import uuid4
import stravalib
from stravalib.exc import ActivityUploadFailed,TimeoutExceeded
@@ -115,7 +118,7 @@ def get_token(code):
def make_authorization_url(request):
# Generate a random string for the state parameter
# Save it for use later to prevent xsrf attacks
from uuid import uuid4
state = str(uuid4())
params = {"client_id": STRAVA_CLIENT_ID,
@@ -144,6 +147,130 @@ def get_strava_workout_list(user):
return s
def add_stroke_data(user,stravaid,workoutid,startdatetime,csvfilename):
r = Rower.objects.get(user=user)
starttimeunix = arrow.get(startdatetime).timestamp
job = myqueue(queue,
handle_strava_import_stroke_data,
r.stravatoken,
stravid,
workoutid,
starttimeunix,
csvfilename)
# gets all new Strava workouts for a rower
def get_strava_workouts(rower):
if not isprorower(rower):
return 0
res = get_strava_workout_list(rower.user)
if (res.status_code != 200):
return 0
else:
stravaids = [int(item['id']) for item in res.json()]
alldata = {}
for item in res.json():
alldata[item['id']] = item
knownstravaids = uniqify([
w.uploadedtostrava for w in Workout.objects.filter(user=rower)
])
newids = [stravaid for stravaid in stravaids if not stravaid in knownstravaids]
print newids,'aap'
for stravaid in newids:
workoutid = create_async_workout(alldata,rower.user,stravaid)
return 1
def create_async_workout(alldata,user,stravaid):
data = alldata[stravid]
distance = data['distance']
stravaid = data['id']
try:
workouttype = data['type']
except:
workouttype = 'rower'
if workouttype not in [x[0] for x in Workout.workouttypes]:
workouttype = 'other'
try:
comments = data['comments']
except:
comments = ' '
try:
thetimezone = tz(data['timezone'])
except:
thetimezone = 'UTC'
try:
rowdatetime = iso8601.parse_date(data['date_utc'])
except KeyError:
rowdatetime = iso8601.parse_date(data['start_date'])
except ParseError:
rowdatetime = iso8601.parse_date(data['date'])
try:
c2intervaltype = data['workout_type']
except KeyError:
c2intervaltype = ''
try:
title = data['name']
except KeyError:
title = ""
try:
t = data['comments'].split('\n', 1)[0]
title += t[:20]
except:
title = 'Imported'
totaltime = data['elapsed_time']
duration = dataprep.totaltime_sec_to_string(totaltime)
weightcategory = 'hwt'
# Create CSV file name and save data to CSV file
csvfilename ='media/{code}_{importid}.csv'.format(
importid=stravaid,
code = uuid4().hex[:16]
)
w = Workout(
user=r,
workouttype = workouttype,
name = title,
date = workoutdate,
starttime = starttime,
startdatetime = rowdatetime,
timezone = thetimezone,
duration = duration,
distance=distance,
weightcategory = weightcategory,
uploadedtostrava = stravaid,
csvfilename = csvfilename,
notes = ''
)
w.save()
# Check if workout has stroke data, and get the stroke data
result = add_stroke_data(user,stravaid,w.id,rowdatetime,csvfilename)
return w.id
# Get a Strava workout summary data and stroke data by ID
def get_strava_workout(user,stravaid):
r = Rower.objects.get(user=user)