diff --git a/rowers/forms.py b/rowers/forms.py index d27030bd..9666b376 100644 --- a/rowers/forms.py +++ b/rowers/forms.py @@ -42,6 +42,7 @@ class DocumentsForm(forms.Form): ('ergdata' , 'ErgData CSV'), ('ergstick' , 'ErgStick CSV'), ('painsleddesktop' , 'Painsled Desktop CSV'), + ('zip','Zipped file'), ) title = forms.CharField(required=False) file = forms.FileField(required=True, diff --git a/rowers/mailprocessing.py b/rowers/mailprocessing.py index 424241d3..3c4fb3d3 100644 --- a/rowers/mailprocessing.py +++ b/rowers/mailprocessing.py @@ -100,6 +100,14 @@ def make_new_workout_from_email(rr,f2,name,cntr=0): workouttype = 'rower' f2 = f2.name fileformat = get_file_type('media/'+f2) + + 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/') + fileformat = fileformat[2] + os.remove(f_to_be_deleted) + if fileformat == 'unknown': if settings.DEBUG: res = handle_sendemail_unrecognized.delay(f2, diff --git a/rowers/views.py b/rowers/views.py index 87d2991c..25b704f1 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -1,4 +1,5 @@ import time +import zipfile import operator from django.views.generic.base import TemplateView from django.db.models import Q @@ -829,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: @@ -977,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 ): @@ -995,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" @@ -3694,6 +3718,12 @@ def workout_upload_view(request,message=""): # new fileformat = get_file_type(f2) + 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/') + fileformat = fileformat[2] + os.remove(f_to_be_deleted) if fileformat == 'unknown': message = "We couldn't recognize the file type" diff --git a/rowsandall_app/rows.py b/rowsandall_app/rows.py index 1d9848c9..c93e7a69 100644 --- a/rowsandall_app/rows.py +++ b/rowsandall_app/rows.py @@ -48,7 +48,7 @@ def format_time(x,pos=None): def validate_file_extension(value): import os ext = os.path.splitext(value.name)[1] - valid_extensions = ['.tcx','.csv','.TCX','.CSV','.fit','.FIT'] + valid_extensions = ['.tcx','.csv','.TCX','.CSV','.fit','.FIT','.zip','.ZIP'] if not ext in valid_extensions: raise ValidationError(u'File not supported!')