From 34499ae287bce895459c43154d33589ad464afda Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 20 Nov 2023 19:06:11 +0100 Subject: [PATCH] fix errors tcx --- rowers/emails.py | 2 + rowers/tests/testdata/testdata.tcx.gz | Bin 4000 -> 4000 bytes rowers/views/apiviews.py | 90 ++++++++++++++------------ 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/rowers/emails.py b/rowers/emails.py index 577baf01..d209ee04 100644 --- a/rowers/emails.py +++ b/rowers/emails.py @@ -128,6 +128,8 @@ def send_template_email(from_email, to_email, subject, umsg.save() except User.DoesNotExist: pass + except Exception as e: + pass if not emailbounced: res = msg.send() diff --git a/rowers/tests/testdata/testdata.tcx.gz b/rowers/tests/testdata/testdata.tcx.gz index 3dcc1ddfb68207b48edf8b145094b979b25cfde6..a2d383697b269337dbd135bbd92626f0d58f689a 100644 GIT binary patch delta 258 zcmV+d0sa1Du$`N`ZufR2DxZ!!xc|C;b#`&( zHMxI&ezH31`t;K;UyRFhe|56--OsOACoeC5?(gY+Oov{1I3Kn=>Hd>0nVki3S3iHf z+iIOQd-=5AcEjbP{o?!w&(lpG{cAI75kUxERbXV@{;BmJ;e)RD0m#3M{_Rrn+ zU(;Ls^!4eBWx6wf9{_g$A1tnXv3v5{$8>mSi+Z(A&*4Erxx9Fop2mxG>BBq!1H!nf I4#a=~0NUA+aR2}S delta 258 zcmV+d0sa1Fv2Vd%Nk@FT0bI?bAvhSL@@KUEihicDuJLQTcS-!Ts0$tFwzM zugU%M^OMz4*QcL;`C?q2`>T_s?|y#0I(d2dbAM0oV>wi_-V?HA`ic%E+h@VDj3vfC^Zhy3Zoqq}ln2amh;@uP=_zdX%swtw!n z|C-+7r>{?6EYqC<`~a}~|6pXh-0CWnQ(EtDd diff --git a/rowers/views/apiviews.py b/rowers/views/apiviews.py index 360f0f0e..cb5bf487 100644 --- a/rowers/views/apiviews.py +++ b/rowers/views/apiviews.py @@ -15,7 +15,7 @@ from datetime import datetime as dt import rowingdata.tcxtools as tcxtools from rowingdata import TCXParser, rowingdata - +import arrow class XMLParser(BaseParser): media_type = "application/xml" @@ -239,12 +239,14 @@ def strokedata_tcx(request): Upload a TCX file through API """ if request.method != 'POST': + dologging('apilog.log','GET request to TCX endpoint') return HttpResponseNotAllowed("Method not supported") # pragma: no cover if request.content_type.lower() != 'application/xml': + dologging('apilog.log','POST data not application/xml, request to TCX endpoint') return HttpResponseNotAllowed("Need application/xml") - dologging('apilog.log', request.user.username+" (strokedatajson_TCX POST)") + dologging('apilog.log', request.user.username+" (strokedata_tcx POST)") try: tcxdata = request.data @@ -267,56 +269,64 @@ def strokedata_tcx(request): total_duration += lap_duration_seconds except Exception as e: + dologgin('apilog.log','TCX') dologging('apilog.log',e) return HttpResponseNotAllowed("Could not parse TCX data") - tcxfilename = 'media/{code}.tcx'.format(code=uuid4().hex[:16]) - xml_string = ET.tostring(tcxdata, encoding='utf-8', method='xml').decode('utf-8') - - with open(tcxfilename, 'w', encoding='utf-8') as xml_file: - xml_file.write(xml_string) + try: + tcxfilename = 'media/{code}.tcx'.format(code=uuid4().hex[:16]) + xml_string = ET.tostring(tcxdata, encoding='utf-8', method='xml').decode('utf-8') + + with open(tcxfilename, 'w', encoding='utf-8') as xml_file: + xml_file.write(xml_string) - duration = totaltime_sec_to_string(total_duration) - startdatetime = dt.strptime(start_time_str, "%Y-%m-%dT%H:%M:%S%z") - startdate = startdatetime.date() - partofday = part_of_day(startdatetime.hour) - title = '{partofday} water'.format(partofday=partofday) + duration = totaltime_sec_to_string(total_duration) + startdatetime = arrow.get(start_time_str) + #startdatetime = dt.strptime(start_time_str, "%Y-%m-%dT%H:%M:%S%z") + startdate = startdatetime.date() + partofday = part_of_day(startdatetime.hour) + title = '{partofday} water'.format(partofday=partofday) + + w = Workout(user=request.user.rower, + date=startdate, + name=title, + duration=duration) + w.save() - w = Workout(user=request.user.rower, - date=startdate, - name=title, - duration=duration) - w.save() + # need workouttype, duration - # need workouttype, duration - - uploadoptions = { - 'secret': UPLOAD_SERVICE_SECRET, - 'user': request.user.id, - 'file': tcxfilename, - 'id': w.id, - 'title': title, - 'rpe': 0, - 'workouttype': 'water', - 'boattype': '1x', - 'notes': '', - 'offline': False, - } + uploadoptions = { + 'secret': UPLOAD_SERVICE_SECRET, + 'user': request.user.id, + 'file': tcxfilename, + 'id': w.id, + 'title': title, + 'rpe': 0, + 'workouttype': 'water', + 'boattype': '1x', + 'notes': '', + 'offline': False, + } - _ = myqueue(queuehigh, - handle_post_workout_api, - uploadoptions) + _ = myqueue(queuehigh, + handle_post_workout_api, + uploadoptions) - workoutid = w.id + workoutid = w.id + + return JsonResponse( + {"workout public id": encoder.encode_hex(workoutid), + "workout id": workoutid, + "status": "success", + }) + except Exception as e: + dologging('apilog.log','TCX API endpoint') + dologging('apilog.log',e) + return HttpResponse(status=500) - return JsonResponse( - {"workout public id": encoder.encode_hex(workoutid), - "workout id": workoutid, - "status": "success", - })