Private
Public Access
1
0
Files
rowsandall/rowers/tasks.py
sanderroosendaal 3eed3cc3e7 Uploading files
2016-10-30 17:39:32 +01:00

194 lines
5.5 KiB
Python

from celery import Celery,app
import os
import time
import gc
import rowingdata
from rowingdata import main as rmain
from rowingdata import rowingdata as rdata
import rowingdata
#from rowers.models import Workout
from matplotlib.backends.backend_agg import FigureCanvas
#from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas
import matplotlib.pyplot as plt
from matplotlib import figure
import stravalib
from django.core.mail import send_mail, BadHeaderError,EmailMessage
@app.task
def add(x, y):
return x + y
@app.task
def handle_sendemail_unrecognized(unrecognizedfile,useremail):
# send email with attachment
fullemail = 'roosendaalsander@gmail.com'
subject = "Unrecognized file from Rowsandall.com"
message = "Dear Sander,\n\n"
message += "Please find attached a file that someone tried to upload to rowsandall.com. The file was not recognized as a valid file type.\n\n"
message += "User Email "+useremail+"\n\n"
message += "Best Regards, the Rowsandall Team"
email = EmailMessage(subject, message,
'Rowsandall <info@rowsandall.com>',
[fullemail])
email.attach_file(unrecognizedfile)
res = email.send()
# remove tcx file
os.remove(unrecognizedfile)
return 1
@app.task
def handle_sendemailtcx(first_name,last_name,email,tcxfile):
# send email with attachment
fullemail = first_name + " " + last_name + " " + "<" + email + ">"
subject = "File from Rowsandall.com"
message = "Dear "+first_name+",\n\n"
message += "Please find attached the requested file for your workout.\n\n"
message += "Best Regards, the Rowsandall Team"
email = EmailMessage(subject, message,
'Rowsandall <info@rowsandall.com>',
[fullemail])
email.attach_file(tcxfile)
res = email.send()
# remove tcx file
os.remove(tcxfile)
return 1
@app.task
def handle_sendemailcsv(first_name,last_name,email,csvfile):
# send email with attachment
fullemail = first_name + " " + last_name + " " + "<" + email + ">"
subject = "File from Rowsandall.com"
message = "Dear "+first_name+",\n\n"
message += "Please find attached the requested file for your workout.\n\n"
message += "Best Regards, the Rowsandall Team"
email = EmailMessage(subject, message,
'Rowsandall <info@rowsandall.com>',
[fullemail])
email.attach_file(csvfile)
res = email.send()
return 1
@app.task
def handle_otwsetpower(f1,boattype,weightvalue,first_name,last_name,email,workoutid):
rowdata = rdata(f1)
weightvalue = float(weightvalue)
# do something with boat type
boatfile = {
'1x':'static/rigging/1x.txt',
'2x':'static/rigging/2x.txt',
'2-':'static/rigging/2-.txt',
'4x':'static/rigging/4x.txt',
'4-':'static/rigging/4-.txt',
'8+':'static/rigging/8+.txt',
}
try:
rg = rowingdata.getrigging(boatfile[boattype])
except KeyError:
rg = rowingdata.getrigging('static/rigging/1x.txt')
# do calculation
rowdata.otw_setpower_silent(skiprows=5,mc=weightvalue,rg=rg)
# save data
rowdata.write_csv(f1)
# send email
fullemail = first_name + " " + last_name + " " + "<" + email + ">"
subject = "Your Rowsandall OTW calculations are ready"
message = "Dear "+first_name+",\n\n"
message += "Your Rowsandall OTW calculations are ready.\n"
# message += "You can now create OTW plots with power information and wind corrections.\n\n"
message += "Thank you for using rowsandall.com.\n\n"
message += "Rowsandall OTW calculations have not been fully implemented yet.\n"
message += "We are now running an experimental version for debugging purposes. \n"
message += "Your wind/stream corrected plot is available here: http://rowsandall.com/rowers/workout/"
message += workoutid
message +="/interactiveotwplot\n\n"
# message += "This functionality will be available soon, though.\n\n"
message += "Please report any bugs/inconsistencies/unexpected results at rowsandall.slack.com or by reply to this email.\n\n"
message += "Best Regards, The Rowsandall Physics Department."
send_mail(subject, message,
'Rowsandall Physics Department <info@rowsandall.com>',
[fullemail])
return 1
@app.task
def handle_makeplot(f1,f2,t,hrdata,plotnr,imagename):
hrmax = hrdata['hrmax']
hrut2 = hrdata['hrut2']
hrut1 = hrdata['hrut1']
hrat = hrdata['hrat']
hrtr = hrdata['hrtr']
hran = hrdata['hran']
rr = rowingdata.rower(hrmax=hrmax,hrut2=hrut2,
hrut1=hrut1,hrat=hrat,
hrtr=hrtr,hran=hran)
row = rdata(f2,rower=rr)
nr_rows = len(row.df)
if (plotnr in [1,2,4,5,8,11,9,12]) and (nr_rows > 1200):
bin = int(nr_rows/1200.)
df = row.df.groupby(lambda x:x/bin).mean()
row.df = df
nr_rows = len(row.df)
if (plotnr==1):
fig1 = row.get_timeplot_erg(t)
elif (plotnr==2):
fig1 = row.get_metersplot_erg(t)
elif (plotnr==3):
fig1 = row.get_piechart(t)
elif (plotnr==4):
fig1 = row.get_timeplot_otw(t)
elif (plotnr==5):
fig1 = row.get_metersplot_otw(t)
elif (plotnr==6):
fig1 = row.get_piechart(t)
elif (plotnr==7) or (plotnr==10):
fig1 = row.get_metersplot_erg2(t)
elif (plotnr==8) or (plotnr==11):
fig1 = row.get_timeplot_erg2(t)
elif (plotnr==9) or (plotnr==12):
fig1 = row.get_time_otwpower(t)
canvas = FigureCanvas(fig1)
# plt.savefig('static/plots/'+imagename,format='png')
canvas.print_figure('static/plots/'+imagename)
# plt.imsave(fname='static/plots/'+imagename)
plt.close(fig1)
fig1.clf()
gc.collect()
return imagename
def add2(x,y):
return x+y