diff --git a/rowers/mailprocessing.py b/rowers/mailprocessing.py index 3c4fb3d3..d53c1eee 100644 --- a/rowers/mailprocessing.py +++ b/rowers/mailprocessing.py @@ -19,6 +19,9 @@ 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 subject = 'Workout added: '+name @@ -71,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 @@ -95,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 = 'https://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 @@ -103,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: diff --git a/rowers/views.py b/rowers/views.py index 14d5f58e..2850f6c9 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -834,26 +834,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: @@ -982,7 +998,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 ): @@ -1000,13 +1018,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"