Private
Public Access
1
0

removing obsolete rowingdata(csvfile)

This commit is contained in:
Sander Roosendaal
2019-11-11 17:31:56 +01:00
parent 23909feabf
commit 2294603129
6 changed files with 152 additions and 156 deletions

View File

@@ -86,7 +86,7 @@ def rower_strava_token_refresh(user):
r.save()
return r.stravatoken
# Make authorization URL including random string
def make_authorization_url(request):
return imports_make_authorization_url(oauth_data)
@@ -130,7 +130,7 @@ def get_strava_workouts(rower):
thetoken = strava_open(rower.user)
except NoTokenError:
return 0
res = get_strava_workout_list(rower.user,limit_n=10)
if (res.status_code != 200):
@@ -142,11 +142,11 @@ def get_strava_workouts(rower):
'elapsed_time':item['elapsed_time'],
'start_date':item['start_date'],
} for item in res.json()]
alldata = {}
for item in res.json():
alldata[item['id']] = item
wfailed = Workout.objects.filter(user=rower,uploadedtostrava=-1)
@@ -159,12 +159,12 @@ def get_strava_workouts(rower):
dd = datetime.min + timedelta(
seconds=int(elapsed_time)
)
if datetime.time(dd) == w.duration:
w.uploadedtostrava = int(stravaid)
w.save()
knownstravaids = [
w.uploadedtostrava for w in Workout.objects.filter(user=rower)
]
@@ -174,7 +174,7 @@ def get_strava_workouts(rower):
]
knownstravaids = uniqify(knownstravaids+tombstones)
newids = [stravaid for stravaid in stravaids if not stravaid in knownstravaids]
for stravaid in newids:
@@ -220,10 +220,10 @@ def create_async_workout(alldata,user,stravaid,debug=False):
try:
c2intervaltype = data['workout_type']
except KeyError:
c2intervaltype = ''
try:
title = data['name']
except KeyError:
@@ -237,22 +237,22 @@ def create_async_workout(alldata,user,stravaid,debug=False):
workoutdate = rowdatetime.astimezone(
pytz.timezone(thetimezone)
).strftime('%Y-%m-%d')
starttime = rowdatetime.astimezone(
pytz.timezone(thetimezone)
).strftime('%H:%m:%S')
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/mailbox_attachments/{code}_{importid}.csv'.format(
importid=stravaid,
code = uuid4().hex[:16]
)
# Check if workout has stroke data, and get the stroke data
@@ -268,7 +268,7 @@ def create_async_workout(alldata,user,stravaid,debug=False):
csvfilename,
workouttype = workouttype,
)
return 1
from rowers.utils import get_strava_stream
@@ -282,11 +282,11 @@ def get_workout(user,stravaid):
except NoTokenError:
s = "Token error"
return custom_exception_handler(401,s)
r = Rower.objects.get(user=user)
if (r.stravatoken == '') or (r.stravatoken is None):
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
return custom_exception_handler(401,s)
elif (r.stravatokenexpirydate is not None and timezone.now()>r.stravatokenexpirydate):
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
@@ -321,9 +321,9 @@ def get_workout(user,stravaid):
else:
duration = int(workoutsummary['elapsed_time'])
t = pd.Series(range(duration+1))
nr_rows = len(t)
if nr_rows == 0:
return (0,"Error: Time data had zero length")
@@ -336,7 +336,7 @@ def get_workout(user,stravaid):
if power is None:
power = np.zeros(nr_rows)
if hr is None:
hr = np.zeros(nr_rows)
@@ -365,7 +365,7 @@ def get_workout(user,stravaid):
strokelength = velo*60./(spm)
strokelength[np.isinf(strokelength)] = 0.0
pace = 500./(1.0*velo2)
pace[np.isinf(pace)] = 0.0
@@ -379,16 +379,16 @@ def get_workout(user,stravaid):
'power':power,
'strokelength':strokelength,
})
# startdatetime = datetime.datetime.strptime(startdatetime,"%Y-%m-%d-%H:%M:%S")
return [workoutsummary,df]
# Generate Workout data for Strava (a TCX file)
def createstravaworkoutdata(w,dozip=True):
filename = w.csvfilename
try:
row = rowingdata(filename)
row = rowingdata(csvfile=filename)
except IOError:
data = dataprep.read_df_sql(w.id)
try:
@@ -402,12 +402,12 @@ def createstravaworkoutdata(w,dozip=True):
index_label='index',
compression='gzip')
try:
row = rowingdata(filename)
row = rowingdata(csvfile=filename)
except IOError:
return '','Error - could not find rowing data'
else:
return '','Error - could not find rowing data'
return '','Error - could not find rowing data'
tcxfilename = filename[:-4]+'.tcx'
try:
newnotes = w.notes+'\n from '+w.workoutsource+' via rowsandall.com'
@@ -441,7 +441,7 @@ def handle_stravaexport(f2,workoutname,stravatoken,description='',
client = stravalib.Client(access_token=stravatoken)
act = client.upload_activity(f2,'tcx.gz',name=workoutname)
try:
res = act.wait(poll_interval=5.0,timeout=30)
message = 'Workout successfully synchronized to Strava'
@@ -474,10 +474,10 @@ def add_workout_from_data(user,importid,data,strokedata,
if workouttype.lower() == 'rowing':
workouttype = 'rower'
if 'summary_polyline' in data['map'] and workouttype=='rower':
workouttype = 'water'
if workouttype not in [x[0] for x in Workout.workouttypes]:
workouttype = 'other'
try:
@@ -497,14 +497,14 @@ def add_workout_from_data(user,importid,data,strokedata,
rowdatetime = iso8601.parse_date(data['start_date'])
except ParseError:
rowdatetime = iso8601.parse_date(data['date'])
try:
intervaltype = data['workout_type']
except KeyError:
intervaltype = ''
try:
title = data['name']
except KeyError:
@@ -550,7 +550,7 @@ def add_workout_from_data(user,importid,data,strokedata,
spm = strokedata.loc[:,'spm']
except KeyError:
spm = 0*dist2
try:
hr = strokedata.loc[:,'hr']
except KeyError:
@@ -568,7 +568,7 @@ def add_workout_from_data(user,importid,data,strokedata,
#if power.std() == 0 and power.mean() == 0:
# power = 2.8*velo**3
# save csv
# Create data frame with all necessary data to write to csv
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
@@ -590,10 +590,10 @@ def add_workout_from_data(user,importid,data,strokedata,
' ElapsedTime (sec)':seconds
})
df.sort_values(by='TimeStamp (sec)',ascending=True)
timestr = strftime("%Y%m%d-%H%M%S")
@@ -623,7 +623,7 @@ def workout_strava_upload(user,w):
thetoken = strava_open(user)
except NoTokenError:
return "Please connect to Strava first",0
message = "Uploading to Strava"
stravaid=-1
r = Rower.objects.get(user=user)
@@ -652,7 +652,7 @@ def workout_strava_upload(user,w):
except WindowsError:
pass
return message,stravaid
w.uploadedtostrava = res
w.save()
try:
@@ -704,7 +704,7 @@ def handle_strava_import_stroke_data(title,
startdatetime = workoutsummary['start_date']
r = type('Rower', (object,), {"stravatoken": stravatoken})
spm = get_strava_stream(r,'cadence',stravaid)
hr = get_strava_stream(r,'heartrate',stravaid)
t = get_strava_stream(r,'time',stravaid)
@@ -729,10 +729,10 @@ def handle_strava_import_stroke_data(title,
if power is None:
power = np.zeros(nr_rows)
if hr is None:
hr = np.zeros(nr_rows)
if velo is None:
velo = np.zeros(nr_rows)
@@ -747,7 +747,7 @@ def handle_strava_import_stroke_data(title,
velo2 = savgol_filter(velo,windowsize,3)
else:
velo2 = velo
if coords is not None:
try:
lat = coords[:,0]
@@ -774,7 +774,7 @@ def handle_strava_import_stroke_data(title,
unixtime = starttimeunix+t
strokedistance = 60.*velo2/spm
nr_strokes = len(t)
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
@@ -810,13 +810,13 @@ workouttype {workouttype}""".format(
stravaid=stravaid,
workouttype=workouttype
)
msg = Message(mailbox=workoutsbox,
from_header=useremail,
subject=title,
body=body)
msg.save()
a = MessageAttachment(message=msg,document=csvfilename[6:])
a.save()