From bac131917dcaa1332e14369fed362acdc4ad5ce0 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Mon, 13 Feb 2017 17:42:03 +0100 Subject: [PATCH] first attempt multi zip --- rowers/management/commands/processemail.py | 47 ++++++++++++++++------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/rowers/management/commands/processemail.py b/rowers/management/commands/processemail.py index 6aab0eab..a6ef9d8b 100644 --- a/rowers/management/commands/processemail.py +++ b/rowers/management/commands/processemail.py @@ -7,6 +7,8 @@ sys.path.append('$path_to_root_of_project$/$project_name$') os.environ['DJANGO_SETTINGS_MODULE'] = '$project_name$.settings' +import zipfile + from django.core.management.base import BaseCommand, CommandError from django.conf import settings #from rowers.mailprocessing import processattachments @@ -45,6 +47,7 @@ class Command(BaseCommand): attachments = MessageAttachment.objects.all() cntr = 0 for a in attachments: + extension = a.document[-3:].lower() donotdelete = 0 m = Message.objects.get(id=a.message_id) from_address = m.from_address[0] @@ -55,20 +58,40 @@ class Command(BaseCommand): for u in theusers: try: rr = Rower.objects.get(user=u.id) + if extension == 'zip': + z = zipfile.ZipFile(a.document) + for f in z.namelist(): + f2 = z.extract(f) + try: + wid = [ + make_new_workout_from_email(rr,f2,name) + ] + res += wid + link = 'http://rowsandall.com/rowers/workout/'+str(wid)+'/edit' + dd = send_confirm(u,name,link) + except: + res += ['fail: '+f] + donotdelete = 1 + os.remove(f2) + else: # move attachment and make workout - try: - wid = [make_new_workout_from_email(rr,a.document,name)] - res += wid - link = 'http://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit' + try: + wid = [ + make_new_workout_from_email(rr, + a.document, + name) + ] + res += wid + link = 'http://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit' - except: - # replace with code to process error - res += ['fail: '+name] - donotdelete = 1 - try: - dd = send_confirm(u,name,link) - except: - pass + except: + # replace with code to process error + res += ['fail: '+name] + donotdelete = 1 + try: + dd = send_confirm(u,name,link) + except: + pass except Rower.DoesNotExist: pass