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 3dcc1ddf..a2d38369 100644 Binary files a/rowers/tests/testdata/testdata.tcx.gz and b/rowers/tests/testdata/testdata.tcx.gz differ 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", - })