Private
Public Access
1
0
Files
rowsandall/rowers/uploads.py
2017-09-25 16:24:57 +02:00

116 lines
2.8 KiB
Python

# for actions related to uploads
from django.conf import settings
from django.utils import timezone,translation
from rowers.tasks import (
handle_sendemail_unrecognized,handle_sendemailnewcomment,
handle_sendemailnewresponse, handle_updatedps,
handle_makeplot,handle_otwsetpower,handle_sendemailtcx,
handle_sendemailcsv
)
from rowers.models import GraphImage
import numpy as np
import yaml
import argparse
import yamllint
from subprocess import call
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
from rowers.utils import (
geo_distance,serialize_list,deserialize_list,uniqify,
str2bool,range_to_color_hex,absolute
)
#Configuration for argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("path", help='File/Directory path to be examined', type=str)
args = (parser.parse_args())
#The main loop
#Verify file is in YAML, if so save as dict then end
#ignore = set([])
#with open(args.path, "r") as f:
# try:
# yml = (yaml.load(f))
# print yml
# #If not yaml run through yamllint
# except yaml.YAMLError as exc:
# call(["yamllint","-f","parsable",args.path])
def upload_options(body):
uploadoptions = {}
# ok, a temp solution - write to temp file
with open('temp.txt','w') as f:
f.write(body)
with open('temp.txt','r') as f:
try:
yml = (yaml.load(f))
print yml
except yaml.YAMLError as exc:
call(["yamllint","-f","parsable",'temp.txt'])
return uploadoptions
def make_plot(r,w,f1,f2,plottype,title,imagename='',plotnr=0):
if imagename == '':
imagename = f1[:-4]+'.png'
fullpathimagename = 'static/plots/'+imagename
powerperc = 100*np.array([r.pw_ut2,
r.pw_ut1,
r.pw_at,
r.pw_tr,r.pw_an])/r.ftp
ftp = float(r.ftp)
if w.workouttype in ('water','coastal'):
ftp = ftp*(100.-r.otwslack)/100.
hrpwrdata = {
'hrmax':r.max,
'hrut2':r.ut2,
'hrut1':r.ut1,
'hrat':r.at,
'hrtr':r.tr,
'hran':r.an,
'ftp':ftp,
'powerperc':serialize_list(powerperc),
'powerzones':serialize_list(r.powerzones),
}
# make plot - asynchronous task
plotnrs = {
'timeplot':1,
'distanceplot':2,
'pieplot':3,
}
if plotnr == 0:
plotnr = plotnrs[plottype]
if w.workouttype in ('water','coastal'):
plotnr = plotnr+3
if settings.DEBUG:
res = handle_makeplot.delay(f1,f2,title,
hrpwrdata,plotnr,
imagename)
else:
res = queue.enqueue(handle_makeplot,f1,f2,
title,hrpwrdata,
plotnr,imagename)
i = GraphImage(workout=w,
creationdatetime=timezone.now(),
filename=fullpathimagename)
i.save()
return i.id