Private
Public Access
1
0
Files
rowsandall/rowers/uploads.py
Sander Roosendaal 8af7ac8af4 commit
2022-02-15 08:05:12 +01:00

266 lines
7.7 KiB
Python

from rowers.mytypes import workouttypes, boattypes, otwtypes, workoutsources, workouttypes_ordered
import rowers.c2stuff as c2stuff
from rowers.rower_rules import is_promember
import rowers.tpstuff as tpstuff
import rowers.sporttracksstuff as sporttracksstuff
import rowers.stravastuff as stravastuff
from rowers.utils import (
geo_distance, serialize_list, deserialize_list, uniqify,
str2bool, range_to_color_hex, absolute, myqueue, NoTokenError
)
# 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
from rowers.rower_rules import ispromember
from rowers.utils import dologging
from PIL import Image
import numpy as np
import yaml
import argparse
import yamllint
from subprocess import call
import re
import sys
import time
from verbalexpressions import VerEx
import django_rq
queue = django_rq.get_queue('default')
queuelow = django_rq.get_queue('low')
queuehigh = django_rq.get_queue('high')
try:
from cStringIO import StringIO
except:
from io import StringIO
sources = [s for s, name in workoutsources]
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 otwtypes:
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),
'hrzones': serialize_list(r.hrzones),
}
# make plot - asynchronous task
plotnrs = {
'timeplot': 1,
'distanceplot': 2,
'pieplot': 3,
'None': 0,
}
axis = r.staticgrids
if axis == None: # pragma: no cover
gridtrue = False
axis = 'both'
else:
gridtrue = True
if plotnr == 0:
plotnr = plotnrs[plottype]
if plotnr == 0:
return 0, 0
if w.workouttype in otwtypes:
plotnr = plotnr+3
otwrange = [r.fastpaceotw.total_seconds(), r.slowpaceotw.total_seconds()]
oterange = [r.fastpaceerg.total_seconds(), r.slowpaceerg.total_seconds()]
job = myqueue(queuehigh, handle_makeplot, f1, f2,
title, hrpwrdata,
plotnr, imagename, gridtrue=gridtrue, axis=axis,
otwrange=otwrange, oterange=oterange)
try:
width, height = Image.open(fullpathimagename).size
except:
width = 1200
height = 600
imgs = GraphImage.objects.filter(workout=w)
if len(imgs) < 7:
i = GraphImage(workout=w,
creationdatetime=timezone.now(),
filename=fullpathimagename,
width=width, height=height)
i.save()
else: # pragma: no cover
return 0, 'You have reached the maximum number of static images for this workout. Delete an image first'
return i.id, job.id
def do_sync(w, options, quick=False):
do_strava_export = w.user.strava_auto_export
try:
upload_to_strava = options['upload_to_Strava'] or do_strava_export
except KeyError:
upload_to_strava = False
try:
if options['stravaid'] != 0 and options['stravaid'] != '': # pragma: no cover
w.uploadedtostrava = options['stravaid']
upload_to_strava = False
do_strava_export = False
w.save()
except KeyError:
pass
try:
if options['nkid'] != 0 and options['nkid'] != '': # pragma: no cover
w.uploadedtonk = options['nkid']
w.save()
except KeyError:
pass
try:
if options['inboard'] != 0 and options['inboard'] != '': # pragma: no cover
w.inboard = options['inboard']
except KeyError:
pass
try:
if options['oarlength'] != 0 and options['oarlength'] != '': # pragma: no cover
w.oarlength = options['oarlength']
except KeyError:
pass
try:
if options['garminid'] != 0 and options['garminid'] != '': # pragma: no cover
w.uploadedtogarmin = options['garminid']
w.save()
except KeyError:
pass
do_c2_export = w.user.c2_auto_export
try:
upload_to_c2 = options['upload_to_C2'] or do_c2_export
except KeyError:
upload_to_c2 = False
try:
if options['c2id'] != 0 and options['c2id'] != '': # pragma: no cover
w.uploadedtoc2 = options['c2id']
upload_to_c2 = False
do_c2_export = False
w.save()
except KeyError:
pass
try:
if options['rp3id'] != 0 and options['rp3id'] != '': # pragma: no cover
w.uploadedtorp3 = options['rp3id']
w.save()
except KeyError:
pass
if w.duplicate:
return 0
if do_c2_export: # pragma: no cover
try:
message, id = c2stuff.workout_c2_upload(
w.user.user, w, asynchron=True)
except NoTokenError:
id = 0
message = "Something went wrong with the Concept2 sync"
except: # pragma: no cover
pass
if do_strava_export: # pragma: no cover
try:
message, id = stravastuff.workout_strava_upload(
w.user.user, w, quick=quick, asynchron=True,
)
dologging(
'strava_export_log.log',
'exporting workout {id} as {type}'.format(
id=w.id,
type=w.workouttype,
)
)
except NoTokenError: # pragma: no cover
id = 0
message = "Please connect to Strava first"
except:
e = sys.exc_info()[0]
t = time.localtime()
timestamp = time.strftime('%b-%d-%Y_%H%M', t)
with open('stravalog.log', 'a') as f:
f.write('\n')
f.write(timestamp)
f.write(str(e))
do_tp_export = w.user.trainingpeaks_auto_export
try: # pragma: no cover
upload_to_tp = options['upload_to_SportTracks'] or do_tp_export
do_tp_export = upload_to_tp
except KeyError:
upload_to_tp = False
if do_tp_export: # pragma: no cover
try:
message, id = sporttracksstuff.workout_sporttracks_upload(
w.user.user, w, asynchron=True,
)
with open('st_export.log', 'a') as logfile: # pragma: no cover
logfile.write(str(timezone.now())+': ')
logfile.write('Workout uploaded '+str(w.id)+'\n')
except NoTokenError:
with open('st_export.log', 'a') as logfile:
logfile.write(str(timezone.now())+': ')
logfile.write(str(w.user)+' NoTokenError\n')
message = "Please connect to SportTracks first"
id = 0
if ('upload_to_TrainingPeaks' in options and options['upload_to_TrainingPeaks']) or (w.user.trainingpeaks_auto_export): # pragma: no cover
try:
message, id = tpstuff.workout_tp_upload(
w.user.user, w
)
except NoTokenError:
message = "Please connect to TrainingPeaks first"
id = 0
return 1