From ab3a57b7dd8926b4733e562d8cb147ff353f70d9 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Fri, 12 Jun 2020 14:14:59 +0200 Subject: [PATCH] send email when course fails --- rowers/plannedsessions.py | 6 +- rowers/tasks.py | 75 ++++++++++++++++++++++- rowers/templates/trajectoryfailemail.html | 22 +++++++ rowers/views/planviews.py | 4 +- 4 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 rowers/templates/trajectoryfailemail.html diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index 02df37f3..d8936ca3 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -381,6 +381,7 @@ def add_workouts_plannedsession(ws,ps,r): record.save() job = myqueue(queue,handle_check_race_course,w.csvfilename, w.id,ps.course.id,record.id, + w.user.user.email,w.user.user.first_name, mode='coursetest') else: errors.append('Workout %i did not match session dates' % w.id) @@ -659,6 +660,7 @@ def is_session_complete_ws(ws,ps): record.save() job = myqueue(queue,handle_check_race_course,ws[0].csvfilename, ws[0].id,ps.course.id,record.id, + ws[0].user.user.email,ws[0].user.user.first_name, mode='coursetest') return (0,'not done',None) @@ -1575,7 +1577,9 @@ def add_workout_race(ws,race,r,splitsecond=0,recordid=0): comments.append('Workouts submitted to virtual events have to be public. We have changed the workout to a public workout.') job = myqueue(queue,handle_check_race_course,ws[0].csvfilename, - ws[0].id,race.course.id,record.id,splitsecond=splitsecond, + ws[0].id,race.course.id,record.id, + ws[0].user.user.email,ws[0].user.user.first_name, + splitsecond=splitsecond, referencespeed=record.referencespeed,coursedistance=race.course.distance ) diff --git a/rowers/tasks.py b/rowers/tasks.py index fcd7bc62..68a33e89 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -119,7 +119,11 @@ siteurl = SITE_URL # testing task from rowers.emails import send_template_email -from rowers.courseutils import coursetime_paths, coursetime_first, time_in_path +from rowers.courseutils import ( + coursetime_paths, coursetime_first, time_in_path, + InvalidTrajectoryError + ) + @app.task def add(x, y): @@ -342,7 +346,8 @@ def polygon_to_path(polygon,debug=True): @app.task(bind=True) def handle_check_race_course(self, f1,workoutid,courseid, - recordid,**kwargs): + recordid,useremail,userfirstname, + **kwargs): if 'debug' in kwargs: debug = kwargs['debug'] @@ -432,7 +437,15 @@ def handle_check_race_course(self, paths.append(path) # check how many times went through start polygon - entrytimes,entrydistances = time_in_path(rowdata,paths[0],maxmin='max',getall=True) + try: + entrytimes,entrydistances = time_in_path(rowdata,paths[0],maxmin='max',getall=True) + except InvalidTrajectoryError: + entrytimes = [] + entrydistances = [] + coursecompleted = False + coursemeters = 0 + coursetimeseconds = 0 + cseconds = [] cmeters = [] @@ -528,6 +541,39 @@ def handle_check_race_course(self, return 1 else: + query = 'UPDATE rowers_virtualraceresult SET coursecompleted = 0, duration = "{duration}", distance = {distance}, workoutid = {workoutid}, startsecond = {startsecond}, endsecond = {endsecond}, points={points} WHERE id={recordid}'.format( + recordid=recordid, + duration=totaltime_sec_to_string(0), + distance=0), + points=0, + workoutid=workoutid, + startsecond=startsecond, + endsecond=endsecond, + ) + + if mode == 'coursetest': + query = 'UPDATE rowers_coursetestresult SET coursecompleted = 0, duration = "{duration}", distance = {distance}, workoutid = {workoutid}, startsecond = {startsecond}, endsecond = {endsecond}, points={points} WHERE id={recordid}'.format( + recordid=recordid, + duration=totaltime_sec_to_string(0), + distance=0, + points=0, + workoutid=workoutid, + startsecond=startsecond, + endsecond=endsecond, + ) + + + with engine.connect() as conn, conn.begin(): + result = conn.execute(query) + + conn.close() + engine.dispose() + + # send email + handle_send_email_coursefail( + useremail,userfirstname, + ) + return 2 return 0 @@ -1118,6 +1164,29 @@ def handle_sendemail_raceregistration( return 1 +def handle_sendemail_coursefail( + useremail, username, **kwargs): + + if 'debug' in kwargs: + debug = kwargs['debug'] + else: + debug = True + + subject = "The validation of your course has failed" + + from_email = 'Rowsandall ' + + d = { + 'username':username, + } + + res = send_template_email(from_email,[useremail], + subject, + 'trajectoryfailemail.html', + d,**kwargs) + + return 1 + @app.task def handle_sendemail_optout( useremail, username, registeredname, racename, raceid, **kwargs): diff --git a/rowers/templates/trajectoryfailemail.html b/rowers/templates/trajectoryfailemail.html new file mode 100644 index 00000000..3022682b --- /dev/null +++ b/rowers/templates/trajectoryfailemail.html @@ -0,0 +1,22 @@ +{% extends "emailbase.html" %} +{% block body %} +

Dear {{ username }},

+ +

+ Unfortunately, the course you took did not go through all gates for + the virtual challenge. + You can check your course versus the gates by clicking on Details + in your challenge result. + Of course, you can always submit a new row! +

+ +

+ If you feel that your course should have been valid, or in case + you have questions, please + contact me by reply to this email. +

+ +

+ Best Regards, the Rowsandall Team +

+{% endblock %} diff --git a/rowers/views/planviews.py b/rowers/views/planviews.py index f596d83a..f30b51b9 100644 --- a/rowers/views/planviews.py +++ b/rowers/views/planviews.py @@ -1956,7 +1956,9 @@ def plannedsession_view(request,id=0,userid=0): record.save() job = myqueue(queue,handle_check_race_course, w.csvfilename,w.id,ps.course.id, - record.id,mode='coursetest') + record.id, + w.user.user.email,w.user.user.first_name, + mode='coursetest') intsecs = 0 microsecs = 0