From 485edb67ef4fb940ab060a17022b63e5304bf6b4 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 19 Dec 2016 16:46:29 +0100 Subject: [PATCH 1/5] Some error catching --- rowers/views.py | 57 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/rowers/views.py b/rowers/views.py index 5f1f1cc7..25b704f1 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -830,26 +830,42 @@ def list_c2_upload_view(request,id=0): @login_required() def workout_tcxemail_view(request,id=0): message = "" + successmessage = "" r = Rower.objects.get(user=request.user) w = Workout.objects.get(id=id) if (checkworkoutuser(request.user,w)): - tcxfile = stravastuff.createstravaworkoutdata(w) - if settings.DEBUG: - res = handle_sendemailtcx.delay(r.user.first_name, - r.user.last_name, - r.user.email,tcxfile) + try: + tcxfile = stravastuff.createstravaworkoutdata(w) + if settings.DEBUG: + res = handle_sendemailtcx.delay(r.user.first_name, + r.user.last_name, + r.user.email,tcxfile) - else: - res = queuehigh.enqueue(handle_sendemailtcx,r.user.first_name, - r.user.last_name, - r.user.email,tcxfile) + else: + res = queuehigh.enqueue(handle_sendemailtcx,r.user.first_name, + r.user.last_name, + r.user.email,tcxfile) - successmessage = "The TCX file was sent to you per email" - url = reverse(workout_export_view, - kwargs = { - 'id':str(w.id), - 'successmessage':successmessage, + successmessage = "The TCX file was sent to you per email" + url = reverse(workout_export_view, + kwargs = { + 'id':str(w.id), + 'successmessage':successmessage, }) + except: + successmessage = "" + message = "Something went wrong (strava export) "+str(sys.exc_info()[0]) + with open("media/c2errors.log","a") as errorlog: + errorstring = str(sys.exc_info()[0]) + timestr = strftime("%Y%m%d-%H%M%S") + errorlog.write(timestr+errorstring+"\r\n") + + url = reverse(workout_export_view, + kwargs = { + 'id':str(w.id), + 'message':message, + }) + response = HttpResponseRedirect(url) else: @@ -978,7 +994,9 @@ def workout_c2_upload_view(request,id=0): except: message = "Unexpected Error: "+str(sys.exc_info()[0]) with open("media/c2errors.log","a") as errorlog: - errorlog.write("Unexpected Error: "+str(sys.exc_info()[0])) + errorstring = str(sys.exc_info()[0]) + timestr = time.strftime("%Y%m%d-%H%M%S") + errorlog.write(timestr+errorstring+"\n") # check for duplicate error first if (response.status_code == 409 ): @@ -996,13 +1014,18 @@ def workout_c2_upload_view(request,id=0): except: message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text with open("media/c2errors.log","a") as errorlog: - errorlog.write("Unexpected Error: "+str(sys.exc_info()[0])) + errorstring = str(sys.exc_info()[0]) + timestr = time.strftime("%Y%m%d-%H%M%S") + errorlog.write(timestr+errorstring+"\n") + else: s = response message = "Something went wrong in workout_c2_upload_view. C2 sync failed." with open("media/c2errors.log","a") as errorlog: - errorlog.write("Unexpected Error: "+response.text()) + errorstring = str(sys.exc_info()[0]) + timestr = time.strftime("%Y%m%d-%H%M%S") + errorlog.write(timestr+errorstring+"\n") else: message = "You are not authorized to upload this workout" From 583784a90ca789ad9366ae5a3dee957b935c19c2 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 19 Dec 2016 18:09:33 +0100 Subject: [PATCH 2/5] add zipfile import to mailprocessing --- rowers/mailprocessing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rowers/mailprocessing.py b/rowers/mailprocessing.py index 3c4fb3d3..563b734d 100644 --- a/rowers/mailprocessing.py +++ b/rowers/mailprocessing.py @@ -19,6 +19,8 @@ from rowingdata import summarydata,get_file_type from scipy.signal import savgol_filter +import zipfile + def send_confirm(u,name,link): fullemail = u.email subject = 'Workout added: '+name From ad642418e2f7661ed31a29f1a2522653134c264c Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 19 Dec 2016 19:14:51 +0100 Subject: [PATCH 3/5] zipfix2 --- rowers/mailprocessing.py | 53 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/rowers/mailprocessing.py b/rowers/mailprocessing.py index 563b734d..d2d6fc93 100644 --- a/rowers/mailprocessing.py +++ b/rowers/mailprocessing.py @@ -20,6 +20,7 @@ from rowingdata import summarydata,get_file_type from scipy.signal import savgol_filter import zipfile +import os def send_confirm(u,name,link): fullemail = u.email @@ -97,7 +98,53 @@ def processattachments(): m.delete() return res + +def processattachments_debug(): + res = [] + attachments = MessageAttachment.objects.all() + for a in attachments: + donotdelete = 1 + m = Message.objects.get(id=a.message_id) + from_address = m.from_address[0] + name = m.subject + # get a list of users + theusers = User.objects.filter(email=from_address) + print theusers + for u in theusers: + try: + rr = Rower.objects.get(user=u.id) + doorgaan = 1 + except: + doorgaan = 0 + if doorgaan: + # move attachment and make workout + print a.document + print name + wid = [make_new_workout_from_email(rr,a.document,name)] + res += wid + print wid + link = 'http://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit' + dd = send_confirm(u,name,link) + + + + # remove attachment + if donotdelete == 0: + a.delete() + + if m.attachments.exists()==False: + # no attachments, so can be deleted + m.delete() + + mm = Message.objects.all() + for m in mm: + if m.attachments.exists()==False: + m.delete() + + return res + + def make_new_workout_from_email(rr,f2,name,cntr=0): workouttype = 'rower' f2 = f2.name @@ -105,10 +152,10 @@ def make_new_workout_from_email(rr,f2,name,cntr=0): if len(fileformat)==3 and fileformat[0]=='zip': f_to_be_deleted = f2 - with zipfile.ZipFile(f2) as z: - f2 = z.extract(z.namelist()[0],path='media/') + with zipfile.ZipFile('media/'+f2) as z: + f2 = z.extract(z.namelist()[0],path='media/')[6:] fileformat = fileformat[2] - os.remove(f_to_be_deleted) + print f2 if fileformat == 'unknown': if settings.DEBUG: From 64356bec26e65086a1609f7f12fceb5a4c8a6bfc Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 19 Dec 2016 19:33:13 +0100 Subject: [PATCH 4/5] zipfix3 --- rowers/mailprocessing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rowers/mailprocessing.py b/rowers/mailprocessing.py index d2d6fc93..26bd0498 100644 --- a/rowers/mailprocessing.py +++ b/rowers/mailprocessing.py @@ -74,7 +74,7 @@ def processattachments(): wid = [make_new_workout_from_email(rr,a.document,name)] res += wid print wid - link = 'http://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit' + link = 'https://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit' dd = send_confirm(u,name,link) except: # replace with code to process error From 886fdc5881101f0e182bb1162630f062d059fa94 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 19 Dec 2016 19:45:10 +0100 Subject: [PATCH 5/5] updated link --- rowers/mailprocessing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rowers/mailprocessing.py b/rowers/mailprocessing.py index 26bd0498..d53c1eee 100644 --- a/rowers/mailprocessing.py +++ b/rowers/mailprocessing.py @@ -124,7 +124,7 @@ def processattachments_debug(): wid = [make_new_workout_from_email(rr,a.document,name)] res += wid print wid - link = 'http://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit' + link = 'https://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit' dd = send_confirm(u,name,link)