From 27b26922ba9a67586e044ba05027fb61e3dbe376 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Thu, 24 Jan 2019 18:26:43 +0100 Subject: [PATCH] race submission code in place, failing, littered with debug statements --- rowers/management/commands/processemail.py | 17 ++- rowers/plannedsessions.py | 130 ++++++++++++++++++++- rowers/tests/statements.py | 16 +++ rowers/tests/test_emails.py | 13 ++- rowers/tests/testdata/testdata.csv.gz | Bin 11426 -> 11426 bytes rowers/tests/testdata/testdata.tcx | 2 +- rowers/uploads.py | 35 +++++- 7 files changed, 207 insertions(+), 6 deletions(-) diff --git a/rowers/management/commands/processemail.py b/rowers/management/commands/processemail.py index 89e6a494..8988d1cc 100644 --- a/rowers/management/commands/processemail.py +++ b/rowers/management/commands/processemail.py @@ -23,7 +23,8 @@ from rowers.mailprocessing import make_new_workout_from_email, send_confirm import rowers.polarstuff as polarstuff import rowers.c2stuff as c2stuff import rowers.stravastuff as stravastuff -from rowers.models import User +from rowers.models import User,VirtualRace,Workout +from rowers.plannedsessions import email_submit_race workoutmailbox = Mailbox.objects.get(name='workouts') failedmailbox = Mailbox.objects.get(name='Failed') @@ -82,6 +83,20 @@ def processattachment(rower, fileobj, title, uploadoptions,testing=False): make_new_workout_from_email(therower, filename, title,testing=testing) ] + if testing and workoutid[0]: + w = Workout.objects.get(id = workoutid[0]) + w.startdatetime = timezone.now() + w.date = timezone.now().date() + w.save() + + if 'raceid' in uploadoptions and workoutid[0]: + try: + race = VirtualRace.objects.get(id=uploadoptions['raceid']) + if race.manager == rower.user: + result = email_submit_race(therower,race,workoutid[0]) + except VirtualRace.DoesNotExist: + pass + if testing: print 'Workout id = {workoutid}'.format(workoutid=workoutid) diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index bd7f1958..853ccccb 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -36,6 +36,7 @@ import courses import iso8601 from iso8601 import ParseError from rowers.tasks import handle_check_race_course +from rowers.utils import totaltime_sec_to_string def get_indoorraces(workout): races1 = VirtualRace.objects.filter( @@ -777,7 +778,6 @@ def race_can_submit(r,race): else: return False - print 'pop' return False def race_can_resubmit(r,race): @@ -877,6 +877,134 @@ def race_can_withdraw(r,race): return True +def email_submit_race(r,race,workoutid): + try: + w = Workout.objects.get(id=workoutid) + except Workout.DoesNotExist: + return 0 + + if race.sessionmode == 'time': + wduration = timefield_to_seconds_duration(w.duration) + delta = wduration - (60.*race.sessionvalue) + + if delta > -2 and delta < 2: + w.duration = totaltime_sec_to_string(60.*race.sessionvalue) + w.save() + + elif race.sessionmode == 'distance': + delta = w.distance - race.sessionvalue + + if delta > -5 and delta < 5: + w.distance = race.sessionvalue + w.save() + + + if race_can_register(r,race): + teamname = '' + weightcategory = w.weightcategory + sex = r.sex + if sex == 'not specified': + sex = 'male' + + if not r.birthdate: + return 0 + + age = calculate_age(r.birthdate) + + adaptiveclass = r.adaptiveclass + boatclass = w.workouttype + + record = IndoorVirtualRaceResult( + userid = r.id, + teamname=teamname, + race=race, + username = u'{f} {l}'.format( + f = r.user.first_name, + l = r.user.last_name + ), + weightcategory=weightcategory, + adaptiveclass=adaptiveclass, + duration=dt.time(0,0), + boatclass=boatclass, + coursecompleted=False, + sex=sex, + age=age + ) + + record.save() + + result = add_rower_race(r,race) + + otherrecords = IndoorVirtualRaceResult.objects.filter( + race = race).exclude(userid = r.id) + + for otherrecord in otherrecords: + otheruser = Rower.objects.get(id=otherrecord.userid) + othername = otheruser.user.first_name+' '+otheruser.user.last_name + registeredname = r.user.first_name+' '+r.user.last_name + if otherrecord.emailnotifications: + job = myqueue( + queue, + handle_sendemail_raceregistration, + otheruser.user.email, othername, + registeredname, + race.name, + race.id + ) + + + if race_can_submit(r,race): + records = IndoorVirtualRaceResult.objects.filter( + userid = r.id, + race=race + ) + + if not records: + print 'jet' + return 0 + + record = records[0] + + workouts = Workout.objects.filter(id=w.id) + print workouts + print record.id + print 'aap' + + result,comments,errors,jobid = add_workout_indoorrace( + workouts,race,r,recordid=record.id + ) + + print result,'noot' + + if result: + otherrecords = resultobj.objects.filter( + race = race).exclude(userid = r.id) + + for otherrecord in otherrecords: + otheruser = Rower.objects.get(id=otherrecord.userid) + othername = otheruser.user.first_name+' '+otheruser.user.last_name + registeredname = r.user.first_name+' '+r.user.last_name + if otherrecord.emailnotifications: + job = myqueue( + queue, + handle_sendemail_racesubmission, + otheruser.user.email, othername, + registeredname, + race.name, + race.id + ) + + return 1 + else: + for error in errors: + print error + print 'fail' + else: + + return 0 + + + def race_can_register(r,race): if race.sessiontype == 'race': recordobj = VirtualRaceResult diff --git a/rowers/tests/statements.py b/rowers/tests/statements.py index 2b01521c..14e80178 100644 --- a/rowers/tests/statements.py +++ b/rowers/tests/statements.py @@ -127,6 +127,22 @@ class UserFactory(factory.DjangoModelFactory): first_name = faker.name().split(' ')[0] last_name = faker.name().split(' ')[0] + +class RaceFactory(factory.DjangoModelFactory): + class Meta: + model = VirtualRace + + name = factory.LazyAttribute(lambda _: faker.word()) + registration_closure = timezone.now()+datetime.timedelta(days=1) + evaluation_closure = timezone.now()+datetime.timedelta(days=2) + startdate = timezone.now().date() + start_time = datetime.time() + enddate = (timezone.now()+datetime.timedelta(days=1)).date() + end_time = datetime.time() + preferreddate = timezone.now().date() + sessiontype = 'indoorrace' + sessionvalue = 1 + sessionmode = 'time' class WorkoutFactory(factory.DjangoModelFactory): class Meta: diff --git a/rowers/tests/test_emails.py b/rowers/tests/test_emails.py index 6970ad56..bf3e986c 100644 --- a/rowers/tests/test_emails.py +++ b/rowers/tests/test_emails.py @@ -86,6 +86,7 @@ class EmailTests(TestCase): workoutsbox.save() failbox = Mailbox.objects.create(name='Failed') failbox.save() + for filename in os.listdir(u'rowers/tests/testdata/emails'): m = Message(mailbox=workoutsbox, @@ -152,7 +153,8 @@ class EmailAdminUpload(TestCase): 'sander@ds.ds', 'koeinsloot') r = Rower.objects.create(user=u,gdproptin=True, - gdproptindate=timezone.now() + gdproptindate=timezone.now(), + birthdate = faker.profile()['birthdate'] ) self.theadmin = UserFactory(is_staff=True) @@ -162,6 +164,8 @@ class EmailAdminUpload(TestCase): gdproptindate=timezone.now(), rowerplan='coach') + self.race = RaceFactory(manager = self.theadmin) + nu = datetime.datetime.now() workoutsbox = Mailbox.objects.create(name='workouts') workoutsbox.save() @@ -210,4 +214,11 @@ race 1 w = ws[0] self.assertEqual(w.user.user.username,u'john') + results = IndoorVirtualRaceResult.objects.filter( + race=self.race) + + self.assertEqual(len(results),1) + result = results[0] + + self.assertTrue(result.coursecompleted) diff --git a/rowers/tests/testdata/testdata.csv.gz b/rowers/tests/testdata/testdata.csv.gz index 594e21a9c8c274737a218cee59201c83f65a8510..3138a5249bbb4830e3ad68684e1a70a05706078e 100644 GIT binary patch delta 15 WcmZ1!xhRrNzMF$%)#Ht9b94YMNd?0I delta 15 WcmZ1!xhRrNzMF$1^8QA)IXVC=PX%rO diff --git a/rowers/tests/testdata/testdata.tcx b/rowers/tests/testdata/testdata.tcx index a8b8d2b8..a9968393 100644 --- a/rowers/tests/testdata/testdata.tcx +++ b/rowers/tests/testdata/testdata.tcx @@ -2502,7 +2502,7 @@ - <Element 'Notes' at 0x14a96cf8> + <Element 'Notes' at 0x13de37f0> diff --git a/rowers/uploads.py b/rowers/uploads.py index 942c7430..9094d200 100644 --- a/rowers/uploads.py +++ b/rowers/uploads.py @@ -90,7 +90,18 @@ def matchuser(line): return words[1] return None - + +def matchrace(line): + testert = '^(race)' + tester = re.compile(testert) + if tester.match(line.lower()): + words = line.split() + try: + return int(words[1]) + except: + return None + + return None def matchsync(line): results = [] @@ -194,6 +205,14 @@ def getuseroptions_body2(uploadoptions,body): return uploadoptions +def getraceoptions_body2(uploadoptions,body): + for line in body.splitlines(): + raceid = matchrace(line) + if raceid: + uploadoptions['raceid'] = raceid + + return uploadoptions + def getsyncoptions_body2(uploadoptions,body): result = [] for line in body.splitlines(): @@ -284,6 +303,15 @@ def getuser(uploadoptions,value,key): return uploadoptions +def getrace(uploadoptions,value,key): + try: + raceid = int(value) + uploadoptions['raceid'] = raceid + except: + pass + + return uploadoptions + def getsource(uploadoptions,value,key): workoutsource = 'unknown' for type,verb in workoutsources: @@ -330,7 +358,9 @@ def upload_options(body): if 'source' in lowkey: uploadoptions = getsource(uploadoptions,value,'workoutsource') if 'username' in lowkey: - uploadoptions = getuser(uploadoptions,value,'workoutuser') + uploadoptions = getuser(uploadoptions,value,'username') + if 'raceid' in lowkey: + uploadoptions = getraceid(uploadoptions,value,'raceid') except AttributeError: #pass raise yaml.YAMLError @@ -343,6 +373,7 @@ def upload_options(body): uploadoptions = getstravaid(uploadoptions,body) uploadoptions = getworkoutsources(uploadoptions,body) uploadoptions = getuseroptions_body2(uploadoptions,body) + uploadoptions = getraceoptions_body2(uploadoptions,body) except IOError: pm = exc.problem_mark strpm = str(pm)