using models in tasks.py
This commit is contained in:
@@ -784,7 +784,8 @@ def deletecpdata_sql(rower_id, table='cpdata'): # pragma: no cover
|
|||||||
with engine.connect() as conn, conn.begin():
|
with engine.connect() as conn, conn.begin():
|
||||||
try:
|
try:
|
||||||
_ = conn.execute(query)
|
_ = conn.execute(query)
|
||||||
except:
|
except Exception as e:
|
||||||
|
print(Exception, e)
|
||||||
print("Database locked")
|
print("Database locked")
|
||||||
conn.close()
|
conn.close()
|
||||||
engine.dispose()
|
engine.dispose()
|
||||||
@@ -1811,7 +1812,8 @@ def delete_agegroup_db(age, sex, weightcategory, debug=False):
|
|||||||
with engine.connect() as conn, conn.begin():
|
with engine.connect() as conn, conn.begin():
|
||||||
try:
|
try:
|
||||||
_ = conn.execute(query)
|
_ = conn.execute(query)
|
||||||
except: # pragma: no cover
|
except Exception as e: # pragma: no cover
|
||||||
|
print(Exception, e)
|
||||||
print("Database locked")
|
print("Database locked")
|
||||||
conn.close()
|
conn.close()
|
||||||
engine.dispose()
|
engine.dispose()
|
||||||
|
|||||||
@@ -555,6 +555,7 @@ def polygon_coord_center(polygon):
|
|||||||
|
|
||||||
def polygon_to_path(polygon):
|
def polygon_to_path(polygon):
|
||||||
points = GeoPoint.objects.filter(polygon=polygon).order_by("order_in_poly")
|
points = GeoPoint.objects.filter(polygon=polygon).order_by("order_in_poly")
|
||||||
|
|
||||||
s = []
|
s = []
|
||||||
for point in points:
|
for point in points:
|
||||||
s.append([point.latitude, point.longitude])
|
s.append([point.latitude, point.longitude])
|
||||||
|
|||||||
256
rowers/tasks.py
256
rowers/tasks.py
@@ -11,7 +11,10 @@ except KeyError:
|
|||||||
from django.core.wsgi import get_wsgi_application
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
application = get_wsgi_application()
|
application = get_wsgi_application()
|
||||||
from rowers.models import *
|
from rowers.models import (
|
||||||
|
Workout, GeoPolygon, GeoPoint, GeoCourse,
|
||||||
|
VirtualRaceResult, CourseTestResult, Rower
|
||||||
|
)
|
||||||
|
|
||||||
import math
|
import math
|
||||||
from rowers.courseutils import (
|
from rowers.courseutils import (
|
||||||
@@ -51,7 +54,6 @@ from rowingdata import make_cumvalues
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from rowingdata import rowingdata as rdata
|
from rowingdata import rowingdata as rdata
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from sqlalchemy import create_engine
|
|
||||||
|
|
||||||
from rowers.celery import app
|
from rowers.celery import app
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
@@ -296,7 +298,9 @@ def summaryfromsplitdata(splitdata, data, filename, sep='|', workouttype='rower'
|
|||||||
|
|
||||||
@app.task
|
@app.task
|
||||||
def handle_request_post(url, data, debug=False, **kwargs): # pragma: no cover
|
def handle_request_post(url, data, debug=False, **kwargs): # pragma: no cover
|
||||||
response = requests.post(url, data)
|
if 'localhost' in url:
|
||||||
|
url = 'http'+url[5:]
|
||||||
|
response = requests.post(url, data, verify=False)
|
||||||
dologging('upload_api.log', data)
|
dologging('upload_api.log', data)
|
||||||
dologging('upload_api.log', response.status_code)
|
dologging('upload_api.log', response.status_code)
|
||||||
return response.status_code
|
return response.status_code
|
||||||
@@ -530,30 +534,7 @@ def getagegrouprecord(age, sex='male', weightcategory='hwt',
|
|||||||
return power
|
return power
|
||||||
|
|
||||||
|
|
||||||
def polygon_to_path(polygon, debug=True):
|
from rowers.models import polygon_to_path
|
||||||
pid = polygon[0]
|
|
||||||
query = "SELECT id, latitude, longitude FROM rowers_geopoint WHERE polygon_id = {pid}"\
|
|
||||||
" ORDER BY order_in_poly ASC".format(
|
|
||||||
pid=pid)
|
|
||||||
if debug:
|
|
||||||
engine = create_engine(database_url_debug, echo=False)
|
|
||||||
else: # pragma: no cover
|
|
||||||
engine = create_engine(database_url, echo=False)
|
|
||||||
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
result = conn.execute(query)
|
|
||||||
points = result.fetchall()
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
engine.dispose()
|
|
||||||
s = []
|
|
||||||
|
|
||||||
for point in points:
|
|
||||||
s.append([point[1], point[2]])
|
|
||||||
|
|
||||||
p = path.Path(s[:-1])
|
|
||||||
|
|
||||||
return p
|
|
||||||
|
|
||||||
|
|
||||||
@app.task(bind=True)
|
@app.task(bind=True)
|
||||||
@@ -639,25 +620,9 @@ def handle_check_race_course(self,
|
|||||||
rowdata = rowdata.resample('100ms', on='dt').mean()
|
rowdata = rowdata.resample('100ms', on='dt').mean()
|
||||||
rowdata = rowdata.interpolate()
|
rowdata = rowdata.interpolate()
|
||||||
|
|
||||||
# initiate database engine
|
course = GeoCourse.objects.get(courseid)
|
||||||
|
polygons = course.polygons.all()
|
||||||
|
|
||||||
if debug: # pragma: no cover
|
|
||||||
engine = create_engine(database_url_debug, echo=False)
|
|
||||||
else:
|
|
||||||
engine = create_engine(database_url, echo=False)
|
|
||||||
|
|
||||||
# get polygons
|
|
||||||
query = "SELECT id,name FROM rowers_geopolygon WHERE course_id = {courseid} ORDER BY order_in_course ASC".format(
|
|
||||||
courseid=courseid
|
|
||||||
)
|
|
||||||
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
result = conn.execute(query)
|
|
||||||
polygons = result.fetchall()
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
engine.dispose()
|
|
||||||
|
|
||||||
paths = []
|
paths = []
|
||||||
for polygon in polygons:
|
for polygon in polygons:
|
||||||
@@ -759,33 +724,25 @@ def handle_check_race_course(self,
|
|||||||
coursedistance = coursemeters
|
coursedistance = coursemeters
|
||||||
velo = coursedistance/coursetimeseconds
|
velo = coursedistance/coursetimeseconds
|
||||||
points = 100*(2.-referencespeed/velo)
|
points = 100*(2.-referencespeed/velo)
|
||||||
query = 'UPDATE rowers_virtualraceresult SET coursecompleted = 1,'\
|
record = VirtualRaceResult.objects.get(id=recordid)
|
||||||
' duration = "{duration}", distance = {distance},'\
|
record.duration = totaltime_sec_to_string(coursetimeseconds)
|
||||||
' workoutid = {workoutid}, startsecond = {startsecond},'\
|
record.distance=int(coursemeters)
|
||||||
' endsecond = {endsecond}, points={points} WHERE id={recordid}'.format(
|
record.points = points
|
||||||
recordid=recordid,
|
record.startsecond = startsecond
|
||||||
duration=totaltime_sec_to_string(coursetimeseconds),
|
record.endsecond = endsecond
|
||||||
distance=int(coursemeters),
|
record.workoutid = workoutid
|
||||||
points=points,
|
record.coursecompleted = 1
|
||||||
workoutid=workoutid,
|
record.save()
|
||||||
startsecond=startsecond,
|
|
||||||
endsecond=endsecond,)
|
|
||||||
|
|
||||||
if mode == 'coursetest':
|
if mode == 'coursetest':
|
||||||
query = 'UPDATE rowers_coursetestresult SET coursecompleted = 1,'\
|
record = CourseTestResult.objects.get(id=recordid)
|
||||||
' duration = "{duration}", distance = {distance},'\
|
record.duration = totaltime_sec_to_string(coursetimeseconds)
|
||||||
' workoutid = {workoutid}, startsecond = {startsecond},'\
|
record.distance = int(coursemeters)
|
||||||
' endsecond = {endsecond}, points={points} WHERE id={recordid}'.format(
|
record.workoutid = workoutid
|
||||||
recordid=recordid,
|
record.startsecond = startsecond
|
||||||
duration=totaltime_sec_to_string(coursetimeseconds),
|
record.endsecond = endsecond
|
||||||
distance=int(coursemeters),
|
record.points = points
|
||||||
points=points,
|
record.save()
|
||||||
workoutid=workoutid,
|
|
||||||
startsecond=startsecond,
|
|
||||||
endsecond=endsecond,)
|
|
||||||
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
result = conn.execute(query)
|
|
||||||
|
|
||||||
if summary:
|
if summary:
|
||||||
try:
|
try:
|
||||||
@@ -807,15 +764,10 @@ def handle_check_race_course(self,
|
|||||||
|
|
||||||
summary = row.allstats()
|
summary = row.allstats()
|
||||||
row.write_csv(f1, gzip=True)
|
row.write_csv(f1, gzip=True)
|
||||||
|
workout = Workout.objects.get(id=workoutid)
|
||||||
|
workout.summary = summary
|
||||||
|
workout.save()
|
||||||
|
|
||||||
query = "UPDATE `rowers_workout` SET `summary` = '%s' WHERE `id` = %s" % (
|
|
||||||
summary, workoutid)
|
|
||||||
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
result = conn.execute(query)
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
engine.dispose()
|
|
||||||
|
|
||||||
if successemail:
|
if successemail:
|
||||||
handle_sendemail_coursesucceed(
|
handle_sendemail_coursesucceed(
|
||||||
@@ -827,36 +779,26 @@ def handle_check_race_course(self,
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
else: # pragma: no cover
|
else: # pragma: no cover
|
||||||
query = 'UPDATE rowers_virtualraceresult SET coursecompleted = 0,'\
|
record = VirtualRaceResult.object.get(id=recordid)
|
||||||
' duration = "{duration}", distance = {distance},'\
|
record.duration = totaltime_sec_to_string(0)
|
||||||
' workoutid = {workoutid}, startsecond = {startsecond},'\
|
record.distance = 0
|
||||||
' endsecond = {endsecond}, points={points} WHERE id={recordid}'.format(
|
record.workoutid = workoutid
|
||||||
recordid=recordid,
|
record.startsecond = startsecond
|
||||||
duration=totaltime_sec_to_string(0),
|
record.endsecond = endsecond
|
||||||
distance=0,
|
record.points = 0
|
||||||
points=0.0,
|
record.save()
|
||||||
workoutid=workoutid,
|
|
||||||
startsecond=startsecond,
|
|
||||||
endsecond=endsecond,)
|
|
||||||
|
|
||||||
if mode == 'coursetest':
|
if mode == 'coursetest':
|
||||||
query = 'UPDATE rowers_coursetestresult SET coursecompleted = 0,'\
|
record = CourseTestResult.objects.get(id=recordid)
|
||||||
' duration = "{duration}", distance = {distance}, workoutid = {workoutid}'\
|
record.duration = totaltime_sec_to_string(0)
|
||||||
', startsecond = {startsecond}, endsecond = {endsecond}'\
|
record.distance = 0
|
||||||
', points={points} WHERE id={recordid}'.format(
|
record.workoutid = workoutid
|
||||||
recordid=recordid,
|
record.startsecond = startsecond
|
||||||
duration=totaltime_sec_to_string(0),
|
record.endsecond = endsecond
|
||||||
distance=0,
|
record.points = 0
|
||||||
points=0,
|
record.save()
|
||||||
workoutid=workoutid,
|
|
||||||
startsecond=startsecond,
|
|
||||||
endsecond=endsecond,)
|
|
||||||
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
result = conn.execute(query)
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
engine.dispose()
|
|
||||||
|
|
||||||
# add times for all gates to log file
|
# add times for all gates to log file
|
||||||
with open(logfile, 'ab') as f:
|
with open(logfile, 'ab') as f:
|
||||||
@@ -1109,10 +1051,7 @@ def handle_calctrimp(id,
|
|||||||
hrmax,
|
hrmax,
|
||||||
hrmin,
|
hrmin,
|
||||||
debug=False, **kwargs):
|
debug=False, **kwargs):
|
||||||
if debug: # pragma: no cover
|
|
||||||
engine = create_engine(database_url_debug, echo=False)
|
|
||||||
else:
|
|
||||||
engine = create_engine(database_url, echo=False)
|
|
||||||
|
|
||||||
tss = 0
|
tss = 0
|
||||||
normp = 0
|
normp = 0
|
||||||
@@ -1201,21 +1140,14 @@ def handle_calctrimp(id,
|
|||||||
if hrtss > 1000: # pragma: no cover
|
if hrtss > 1000: # pragma: no cover
|
||||||
hrtss = 0
|
hrtss = 0
|
||||||
|
|
||||||
query = 'UPDATE rowers_workout SET rscore = {tss},'\
|
workout = Workout.objects.get(id=id)
|
||||||
' normp = {normp}, trimp={trimp}, hrtss={hrtss},'\
|
workout.tss = int(tss)
|
||||||
' normv={normv}, normw={normw} WHERE id={id}'.format(
|
workout.normp = int(normp)
|
||||||
tss=int(tss),
|
workout.trimp = int(trimp)
|
||||||
normp=int(normp),
|
workout.hrtss = int(hrtss)
|
||||||
trimp=int(trimp),
|
workout.normv = normv
|
||||||
hrtss=int(hrtss),
|
workout.normw = normw
|
||||||
normv=normv,
|
workout.save()
|
||||||
normw=normw,
|
|
||||||
id=id,)
|
|
||||||
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
_ = conn.execute(query)
|
|
||||||
conn.close()
|
|
||||||
engine.dispose()
|
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@@ -2857,23 +2789,13 @@ def handle_update_wps(rid, types, ids, mode, debug=False, **kwargs):
|
|||||||
except ValueError: # pragma: no cover
|
except ValueError: # pragma: no cover
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
rower = Rower.objects.get(id=rid)
|
||||||
if mode == 'water':
|
if mode == 'water':
|
||||||
query = "UPDATE `rowers_rower` SET `median_wps` = '%s' WHERE `id` = '%s'" % (
|
rower.median_wps = wps_median
|
||||||
wps_median, rid)
|
|
||||||
else:
|
else:
|
||||||
query = "UPDATE `rowers_rower` SET `median_wps_erg` = '%s' WHERE `id` = '%s'" % (
|
rower.median_wps_erg = wps_median
|
||||||
wps_median, rid)
|
|
||||||
|
|
||||||
if debug: # pragma: no cover
|
rower.save()
|
||||||
engine = create_engine(database_url_debug, echo=False)
|
|
||||||
else:
|
|
||||||
engine = create_engine(database_url, echo=False)
|
|
||||||
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
_ = conn.execute(query)
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
engine.dispose()
|
|
||||||
|
|
||||||
return wps_median
|
return wps_median
|
||||||
|
|
||||||
@@ -3022,23 +2944,8 @@ def handle_nk_async_workout(alldata, userid, nktoken, nkid, delaysec, defaulttim
|
|||||||
workoutid, error = add_workout_from_data(userid, nkid, data, df)
|
workoutid, error = add_workout_from_data(userid, nkid, data, df)
|
||||||
|
|
||||||
# dologging('nklog.log','NK Workout ID {id}'.format(id=workoutid))
|
# dologging('nklog.log','NK Workout ID {id}'.format(id=workoutid))
|
||||||
|
workout = Workout.objects.get(id=workoutid)
|
||||||
if debug: # pragma: no cover
|
newnkid = workout.uploadedtonk
|
||||||
engine = create_engine(database_url_debug, echo=False)
|
|
||||||
else:
|
|
||||||
engine = create_engine(database_url, echo=False)
|
|
||||||
|
|
||||||
query = 'SELECT uploadedtonk from rowers_workout WHERE id ={workoutid}'.format(
|
|
||||||
workoutid=workoutid)
|
|
||||||
|
|
||||||
newnkid = 0
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
result = conn.execute(query)
|
|
||||||
tdata = result.fetchall()
|
|
||||||
if tdata:
|
|
||||||
newnkid = tdata[0][0]
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
parkedids = []
|
parkedids = []
|
||||||
try:
|
try:
|
||||||
@@ -3326,22 +3233,8 @@ def handle_c2_async_workout(alldata, userid, c2token, c2id, delaysec, defaulttim
|
|||||||
|
|
||||||
workoutid = response.json()['id']
|
workoutid = response.json()['id']
|
||||||
|
|
||||||
if debug: # pragma: no cover
|
workout = Workout.objects.get(id=workoutid)
|
||||||
engine = create_engine(database_url_debug, echo=False)
|
newc2id = workout.uploadedtoc2
|
||||||
else:
|
|
||||||
engine = create_engine(database_url, echo=False)
|
|
||||||
|
|
||||||
query = 'SELECT uploadedtoc2 from rowers_workout WHERE id ={workoutid}'.format(
|
|
||||||
workoutid=workoutid)
|
|
||||||
|
|
||||||
newc2id = 0
|
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
result = conn.execute(query)
|
|
||||||
tdata = result.fetchall()
|
|
||||||
if tdata: # pragma: no cover
|
|
||||||
newc2id = tdata[0][0]
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
|
|
||||||
parkedids = []
|
parkedids = []
|
||||||
with open('c2blocked.json', 'r') as c2blocked:
|
with open('c2blocked.json', 'r') as c2blocked:
|
||||||
@@ -3358,14 +3251,10 @@ def handle_c2_async_workout(alldata, userid, c2token, c2id, delaysec, defaulttim
|
|||||||
json.dump(tdata, c2blocked)
|
json.dump(tdata, c2blocked)
|
||||||
|
|
||||||
# set distance, time
|
# set distance, time
|
||||||
query = "UPDATE `rowers_workout` SET `distance` = '%s', `duration` = '%s' WHERE `id` = '%s'" % (
|
workout = Workout.objects.get(id=workoutid)
|
||||||
distance, duration, workoutid)
|
workout.distance = distance
|
||||||
|
workout.duration = duration
|
||||||
with engine.connect() as conn, conn.begin():
|
workout.save()
|
||||||
result = conn.execute(query)
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
engine.dispose()
|
|
||||||
|
|
||||||
# summary
|
# summary
|
||||||
if 'workout' in data:
|
if 'workout' in data:
|
||||||
@@ -3382,14 +3271,9 @@ def handle_c2_async_workout(alldata, userid, c2token, c2id, delaysec, defaulttim
|
|||||||
summary, sa, results = summaryfromsplitdata(
|
summary, sa, results = summaryfromsplitdata(
|
||||||
splitdata, data, csvfilename, workouttype=workouttype)
|
splitdata, data, csvfilename, workouttype=workouttype)
|
||||||
|
|
||||||
query = "UPDATE `rowers_workout` SET `summary` = '%s' WHERE `id` = %s" % (
|
workout = Workout.objects.get(id=workoutid)
|
||||||
summary, workoutid)
|
workout.summary = summary
|
||||||
|
workout.save()
|
||||||
with engine.connect() as conn, conn.begin():
|
|
||||||
result = conn.execute(query)
|
|
||||||
|
|
||||||
conn.close()
|
|
||||||
engine.dispose()
|
|
||||||
|
|
||||||
from rowingdata.trainingparser import getlist
|
from rowingdata.trainingparser import getlist
|
||||||
if sa:
|
if sa:
|
||||||
|
|||||||
@@ -81,10 +81,13 @@ class AsyncTaskTests(TestCase):
|
|||||||
|
|
||||||
def test_polygons(self):
|
def test_polygons(self):
|
||||||
polygons = GeoPolygon.objects.all()
|
polygons = GeoPolygon.objects.all()
|
||||||
|
|
||||||
polygon = polygons[0]
|
polygon = polygons[0]
|
||||||
obj = (polygon.id,polygon.name)
|
|
||||||
path = tasks.polygon_to_path(obj)
|
#obj = (polygon.id,polygon.name)
|
||||||
self.assertEqual(len(path),4)
|
path = tasks.polygon_to_path(polygon)
|
||||||
|
|
||||||
|
self.assertEqual(len(path),6)
|
||||||
|
|
||||||
def test_summaryfromsplitdata(self):
|
def test_summaryfromsplitdata(self):
|
||||||
splitdata = [
|
splitdata = [
|
||||||
|
|||||||
Reference in New Issue
Block a user