253 lines
6.6 KiB
Python
253 lines
6.6 KiB
Python
import time
|
|
from django.conf import settings
|
|
from rowers.tasks import handle_sendemail_unrecognized
|
|
from django_mailbox.models import Mailbox,Message,MessageAttachment
|
|
from rowers.models import Workout, User, Rower, WorkoutForm,RowerForm,GraphImage,AdvancedWorkoutForm
|
|
from django.core.files.base import ContentFile
|
|
from django.core.mail import send_mail, BadHeaderError,EmailMessage
|
|
from rowsandall_app.settings import BASE_DIR
|
|
|
|
from rowingdata import rower as rrower
|
|
from rowingdata import main as rmain
|
|
from rowingdata import rowingdata as rrdata
|
|
from rowingdata import TCXParser,RowProParser,ErgDataParser,TCXParserNoHR
|
|
from rowingdata import MysteryParser,BoatCoachParser
|
|
from rowingdata import painsledDesktopParser,speedcoachParser,ErgStickParser
|
|
from rowingdata import SpeedCoach2Parser,FITParser,fitsummarydata
|
|
from rowingdata import make_cumvalues
|
|
from rowingdata import summarydata,get_file_type
|
|
|
|
from scipy.signal import savgol_filter
|
|
|
|
def send_confirm(u,name,link):
|
|
fullemail = u.email
|
|
subject = 'Workout added: '+name
|
|
message = 'Dear '+u.first_name+',\n\n'
|
|
message += "Your workout has been added to Rowsandall.com.\n"
|
|
message += "Link to workout: "+link+"\n\n"
|
|
message += "Best Regards, the Rowsandall Team"
|
|
|
|
email = EmailMessage(subject,message,
|
|
'Rowsandall <info@rowsandall.com>',
|
|
[fullemail])
|
|
|
|
res = email.send()
|
|
|
|
return 1
|
|
|
|
def rdata(file,rower=rrower()):
|
|
try:
|
|
res = rrdata(file,rower=rower)
|
|
except IOError:
|
|
res = 0
|
|
|
|
return res
|
|
|
|
def safeprocessattachments():
|
|
try:
|
|
return processattachments()
|
|
except:
|
|
return [0]
|
|
|
|
def processattachments():
|
|
res = []
|
|
attachments = MessageAttachment.objects.all()
|
|
for a in attachments:
|
|
donotdelete = 0
|
|
m = Message.objects.get(id=a.message_id)
|
|
from_address = m.from_address[0]
|
|
name = m.subject
|
|
|
|
# get a list of users
|
|
theusers = User.objects.filter(email=from_address)
|
|
for u in theusers:
|
|
try:
|
|
rr = Rower.objects.get(user=u.id)
|
|
# move attachment and make workout
|
|
try:
|
|
wid = [make_new_workout_from_email(rr,a.document,name)]
|
|
res += wid
|
|
print wid
|
|
link = 'http://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit'
|
|
dd = send_confirm(u,name,link)
|
|
except:
|
|
# replace with code to process error
|
|
res += ['fail: '+name]
|
|
donotdelete = 1
|
|
except Rower.DoesNotExist:
|
|
pass
|
|
|
|
|
|
# remove attachment
|
|
if donotdelete == 0:
|
|
a.delete()
|
|
|
|
if m.attachments.exists()==False:
|
|
# no attachments, so can be deleted
|
|
m.delete()
|
|
|
|
mm = Message.objects.all()
|
|
for m in mm:
|
|
if m.attachments.exists()==False:
|
|
m.delete()
|
|
|
|
return res
|
|
|
|
def make_new_workout_from_email(rr,f2,name,cntr=0):
|
|
workouttype = 'rower'
|
|
f2 = f2.name
|
|
fileformat = get_file_type('media/'+f2)
|
|
if fileformat == 'unknown':
|
|
if settings.DEBUG:
|
|
res = handle_sendemail_unrecognized.delay(f2,
|
|
"roosendaalsander@gmail.com")
|
|
|
|
else:
|
|
res = queuehigh.enqueue(handle_sendemail_unrecognized,
|
|
f2,"roosendaalsander@gmail.com")
|
|
|
|
return 0
|
|
|
|
summary = ''
|
|
# handle non-Painsled
|
|
if (fileformat != 'csv'):
|
|
# handle RowPro:
|
|
if (fileformat == 'rp'):
|
|
row = RowProParser('media/'+f2)
|
|
|
|
# handle TCX
|
|
if (fileformat == 'tcx'):
|
|
row = TCXParser('media/'+f2)
|
|
|
|
# handle Mystery
|
|
if (fileformat == 'mystery'):
|
|
row = MysteryParser('media/'+f2)
|
|
|
|
# handle TCX no HR
|
|
if (fileformat == 'tcxnohr'):
|
|
row = TCXParserNoHR('media/'+f2)
|
|
|
|
# handle ErgData
|
|
if (fileformat == 'ergdata'):
|
|
row = ErgDataParser('media/'+f2)
|
|
|
|
# handle BoatCoach
|
|
if (fileformat == 'boatcoach'):
|
|
row = BoatCoachParser('media/'+f2)
|
|
|
|
# handle painsled desktop
|
|
if (fileformat == 'painsleddesktop'):
|
|
row = painsledDesktopParser('media/'+f2)
|
|
|
|
# handle speed coach GPS
|
|
if (fileformat == 'speedcoach'):
|
|
row = speedcoachParser('media/'+f2)
|
|
|
|
# handle speed coach GPS 2
|
|
if (fileformat == 'speedcoach2'):
|
|
row = SpeedCoach2Parser('media/'+f2)
|
|
|
|
# handle ErgStick
|
|
if (fileformat == 'ergstick'):
|
|
row = ErgStickParser('media/'+f2)
|
|
|
|
# handle FIT
|
|
if (fileformat == 'fit'):
|
|
row = FITParser('media/'+f2)
|
|
s = fitsummarydata('media/'+f2)
|
|
s.setsummary()
|
|
summary = s.summarytext
|
|
|
|
timestr = time.strftime("%Y%m%d-%H%M%S")
|
|
filename = timestr+str(cntr)+'o.csv'
|
|
row.write_csv('media/'+filename)
|
|
f2 = filename
|
|
|
|
# make workout and put in database
|
|
#r = rrower(hrmax=rr.max,hrut2=rr.ut2,
|
|
# hrut1=rr.ut1,hrat=rr.at,
|
|
# hrtr=rr.tr,hran=rr.an,ftp=r.ftp)
|
|
row = rdata('media/'+f2) #,rower=r)
|
|
if row == 0:
|
|
return 0
|
|
|
|
# change filename
|
|
if f2[:5] != 'media':
|
|
timestr = time.strftime("%Y%m%d-%H%M%S")
|
|
f2 = 'media/'+timestr+str(cntr)+'o.csv'
|
|
|
|
# auto smoothing
|
|
pace = row.df[' Stroke500mPace (sec/500m)'].values
|
|
velo = 500./pace
|
|
|
|
f = row.df['TimeStamp (sec)'].diff().mean()
|
|
windowsize = 2*(int(10./(f)))+1
|
|
|
|
if not 'originalvelo' in row.df:
|
|
row.df['originalvelo'] = velo
|
|
|
|
if windowsize > 3:
|
|
velo2 = savgol_filter(velo,windowsize,3)
|
|
else:
|
|
velo2 = velo
|
|
|
|
pace2 = 500./abs(velo2)
|
|
row.df[' Stroke500mPace (sec/500m)'] = pace2
|
|
|
|
row.df = row.df.fillna(0)
|
|
|
|
row.write_csv(f2)
|
|
|
|
# recalculate power data
|
|
if workouttype == 'rower' or workouttype == 'dynamic' or workouttype == 'slides':
|
|
try:
|
|
row.erg_recalculatepower()
|
|
# row.spm_fromtimestamps()
|
|
row.write_csv(f2)
|
|
except:
|
|
pass
|
|
|
|
if fileformat != 'fit':
|
|
summary = row.summary()
|
|
summary += '\n'
|
|
summary += row.intervalstats_painsled()
|
|
|
|
averagehr = row.df[' HRCur (bpm)'].mean()
|
|
maxhr = row.df[' HRCur (bpm)'].max()
|
|
|
|
totaldist = row.df['cum_dist'].max()
|
|
totaltime = row.df['TimeStamp (sec)'].max()-row.df['TimeStamp (sec)'].min()
|
|
totaltime = totaltime+row.df.ix[0,' ElapsedTime (sec)']
|
|
|
|
|
|
hours = int(totaltime/3600.)
|
|
minutes = int((totaltime - 3600.*hours)/60.)
|
|
seconds = int(totaltime - 3600.*hours - 60.*minutes)
|
|
tenths = int(10*(totaltime - 3600.*hours - 60.*minutes - seconds))
|
|
|
|
|
|
duration = "%s:%s:%s.%s" % (hours,minutes,seconds,tenths)
|
|
|
|
workoutdate = row.rowdatetime.strftime('%Y-%m-%d')
|
|
workoutstarttime = row.rowdatetime.strftime('%H:%M:%S')
|
|
|
|
notes = 'imported through email'
|
|
if name == '':
|
|
name = 'imported through email'
|
|
|
|
w = Workout(user=rr,name=name,date=workoutdate,
|
|
workouttype=workouttype,
|
|
duration=duration,distance=totaldist,
|
|
weightcategory=rr.weightcategory,
|
|
starttime=workoutstarttime,
|
|
csvfilename=f2,notes=notes,summary=summary,
|
|
maxhr=maxhr,averagehr=averagehr,
|
|
startdatetime=row.rowdatetime)
|
|
|
|
w.save()
|
|
return w.id
|
|
|
|
|
|
|
|
|