Private
Public Access
1
0

moved workout creation to dataprep for workout_upload

This commit is contained in:
Sander Roosendaal
2017-02-11 15:03:28 +01:00
parent 97b6212161
commit f287f9af21
4 changed files with 168 additions and 296 deletions

View File

@@ -15,11 +15,23 @@ from pytz import timezone as tz,utc
from django.utils.timezone import get_current_timezone
thetimezone = get_current_timezone()
from rowingdata import (
TCXParser,RowProParser,ErgDataParser,TCXParserNoHR,
BoatCoachParser,RowPerfectParser,BoatCoachAdvancedParser,
MysteryParser,
painsledDesktopParser,speedcoachParser,ErgStickParser,
SpeedCoach2Parser,FITParser,fitsummarydata,
make_cumvalues,
summarydata,get_file_type,
)
import os
import pandas as pd
import numpy as np
import itertools
from tasks import handle_sendemail_unrecognized
from django.conf import settings
from sqlalchemy import create_engine
import sqlalchemy as sa
@@ -212,10 +224,46 @@ def new_workout_from_file(r,f2,
workouttype='rower',
title='Workout',
notes=''):
message = None
fileformat = get_file_type(f2)
summary = ''
# handle non-Painsled
if len(fileformat)==3 and fileformat[0]=='zip':
f_to_be_deleted = f2
with zipfile.ZipFile(f2) as z:
# for now, we're getting only the first file
# from the NK zip file (issue #69 on bitbucket)
f2 = z.extract(z.namelist()[0],path='media/')
fileformat = fileformat[2]
os.remove(f_to_be_deleted)
# Some people try to upload Concept2 logbook summaries
if fileformat == 'c2log':
os.remove(f2)
message = "This C2 logbook summary does not contain stroke data. Please download the Export Stroke Data file from the workout details on the C2 logbook."
return (0,message)
# Some people try to upload RowPro summary logs
if fileformat == 'rowprolog':
os.remove(f2)
message = "This RowPro logbook summary does not contain stroke data. Please use the Stroke Data CSV file for the individual workout in your log."
return (0,message)
# Sometimes people try an unsupported file type.
# Send an email to info@rowsandall.com with the file attached
# for me to check if it is a bug, or a new file type
# worth supporting
if fileformat == 'unknown':
message = "We couldn't recognize the file type"
if settings.DEBUG:
res = handle_sendemail_unrecognized.delay(f2,
request.user.email)
else:
res = queuehigh.enqueue(handle_sendemail_unrecognized,
f2,request.user.email)
return (0,'message')
# handle non-Painsled by converting it to painsled compatible CSV
if (fileformat != 'csv'):
# handle RowPro:
if (fileformat == 'rp'):
@@ -232,10 +280,18 @@ def new_workout_from_file(r,f2,
if (fileformat == 'tcxnohr'):
row = TCXParserNoHR(f2)
# handle RowPerfect
if (fileformat == 'rowperfect3'):
row = RowPerfectParser(f2)
# handle ErgData
if (fileformat == 'ergdata'):
row = ErgDataParser(f2)
# handle Mike
if (fileformat == 'bcmike'):
row = BoatCoachAdvancedParser(f2)
# handle BoatCoach
if (fileformat == 'boatcoach'):
row = BoatCoachParser(f2)
@@ -251,7 +307,10 @@ def new_workout_from_file(r,f2,
# handle speed coach GPS 2
if (fileformat == 'speedcoach2'):
row = SpeedCoach2Parser(f2)
summary = row.allstats()
try:
summary = row.allstats()
except:
pass
# handle ErgStick
@@ -266,24 +325,30 @@ def new_workout_from_file(r,f2,
summary = s.summarytext
f_to_be_deleted = f2
# should delete file
f2 = f2[:-4]+'o.csv'
row.write_csv(f2,gzip=True)
f_to_be_deleted = f2
# should delete file
f2 = f2[:-4]+'o.csv'
row.write_csv(f2,gzip=True)
#os.remove(f2)
try:
os.remove(f_to_be_deleted)
except:
os.remove(f_to_be_deleted+'.gz')
#os.remove(f2)
try:
os.remove(f_to_be_deleted)
except:
os.remove(f_to_be_deleted+'.gz')
powerperc = 100*np.array([r.pw_ut2,
r.pw_ut1,
r.pw_at,
r.pw_tr,r.pw_an])/r.ftp
# make workout and put in database
rr = rrower(hrmax=r.max,hrut2=r.ut2,
hrut1=r.ut1,hrat=r.at,
hrtr=r.tr,hran=r.an,ftp=r.ftp)
hrtr=r.tr,hran=r.an,ftp=r.ftp,
powerperc=powerperc,powerzones=r.powerzones)
row = rdata(f2,rower=rr)
if row == 0:
return HttpResponse("Error: CSV Data File Not Found")
return (0,'Error: CSV data file not found')
# auto smoothing
pace = row.df[' Stroke500mPace (sec/500m)'].values
@@ -336,6 +401,10 @@ def new_workout_from_file(r,f2,
totaltime = totaltime+row.df.ix[0,' ElapsedTime (sec)']
hours = int(totaltime/3600.)
if hours>23:
message = 'Warning: The workout duration was longer than 23 hours'
hours = 23
minutes = int((totaltime - 3600.*hours)/60.)
seconds = int(totaltime - 3600.*hours - 60.*minutes)
tenths = int(10*(totaltime - 3600.*hours - 60.*minutes - seconds))
@@ -362,9 +431,10 @@ def new_workout_from_file(r,f2,
w.save()
# put stroke data in database
res = dataprep(row.df,id=w.id,bands=True,barchart=True,otwpower=True,empower=True)
res = dataprep(row.df,id=w.id,bands=True,
barchart=True,otwpower=True,empower=True)
return True
return (w.id,message)
# Compare the data from the CSV file and the database
# Currently only calculates number of strokes. To be expanded with