From d19ddec79e27e355982910cf920fa67550882950 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Fri, 10 Nov 2017 11:40:47 +0100 Subject: [PATCH] some fixes email processing --- rowers/mailprocessing.py | 9 ++++++-- rowers/management/commands/processemail.py | 25 ++++++++++++++++----- rowers/testdata/emails/noname | Bin 0 -> 2173 bytes rowers/tests.py | 13 +++++------ static/img/praha.jpeg | Bin 0 -> 142378 bytes 5 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 rowers/testdata/emails/noname create mode 100644 static/img/praha.jpeg diff --git a/rowers/mailprocessing.py b/rowers/mailprocessing.py index c25fb535..da8073fc 100644 --- a/rowers/mailprocessing.py +++ b/rowers/mailprocessing.py @@ -7,7 +7,12 @@ from rowers.tasks import ( handle_sendemail_unrecognized, handle_sendemail_unrecognizedowner ) -from django_mailbox.models import Message, MessageAttachment +from django_mailbox.models import Message, MessageAttachment,Mailbox + +workoutmailbox = Mailbox.objects.get(name='workouts') +failedmailbox = Mailbox.objects.get(name='Failed') + + from rowers.models import User, Rower, RowerForm from django.core.mail import EmailMessage @@ -148,7 +153,7 @@ def make_new_workout_from_email(rower, datafile, name, cntr=0,testing=False): dosummary = (fileformat != 'fit') if name == '': - name = 'imported through email' + name = 'Workout from Background Queue' id, message = dataprep.save_workout_database( datafilename, rower, diff --git a/rowers/management/commands/processemail.py b/rowers/management/commands/processemail.py index de9d1351..686d3ced 100644 --- a/rowers/management/commands/processemail.py +++ b/rowers/management/commands/processemail.py @@ -9,7 +9,7 @@ import time from time import strftime from django.core.management.base import BaseCommand -from django_mailbox.models import Message, MessageAttachment +from django_mailbox.models import Message, MessageAttachment,Mailbox from django.core.urlresolvers import reverse from django.conf import settings @@ -21,6 +21,9 @@ from rowingdata import rowingdata as rrdata import rowers.uploads as uploads from rowers.mailprocessing import make_new_workout_from_email, send_confirm +workoutmailbox = Mailbox.objects.get(name='workouts') +failedmailbox = Mailbox.objects.get(name='Failed') + # If you find a solution that does not need the two paths, please comment! sys.path.append('$path_to_root_of_project$') sys.path.append('$path_to_root_of_project$/$project_name$') @@ -104,7 +107,11 @@ class Command(BaseCommand): """Run the Email processing command """ def handle(self, *args, **options): - attachments = MessageAttachment.objects.all() + messages = Message.objects.filter(mailbox_id = workoutmailbox.id) + message_ids = [m.id for m in messages] + attachments = MessageAttachment.objects.filter( + message_id__in=message_ids + ) if 'testing' in options: testing = options['testing'] else: @@ -114,10 +121,13 @@ class Command(BaseCommand): extension = attachment.document.name[-3:].lower() try: message = Message.objects.get(id=attachment.message_id) - #if message.text: - # body = "\n".join(message.text.splitlines()) - #else: - body = message.get_body() + if message.text: + body = "\n".join(message.text.splitlines()) + else: + body = message.get_body() + + + uploadoptions = uploads.upload_options(body) from_address = message.from_address[0].lower() @@ -162,6 +172,9 @@ class Command(BaseCommand): attachment.delete() except WindowsError: pass + except: + message.mailbox = failedmailbox + message.save() if message.attachments.exists() is False: # no attachments, so can be deleted diff --git a/rowers/testdata/emails/noname b/rowers/testdata/emails/noname new file mode 100644 index 0000000000000000000000000000000000000000..4eb4a5b22069c92a0b0264cf4a0bee8afdd18fb5 GIT binary patch literal 2173 zcmX}ud6-pm9LMqBIrnqt&PaP1)nE!G^^At}6rs(K_Kk!mLrGMoCS-}`-gD1gre($w z$&w{olqEZ%ELpN-DP$>xLZMJpzUSTZxX*K5&*yx8zwWo##A1h-tPI78WI(_PL3ZoEtZALf5k@r(bw+dFOK1+qrVv=LcSa>*HLpOYBi- zk2P*rhx=i7#u=WB~i&uSa)z+Jc(V}qjALTqT(s+pNrv6)ke8LBO| z$4=PA%&420)rr`PrUD1xQ2J3ghGr~|!-+Trt8oU-#3W{LHrC^OTxf<`gp19z9wsj{ zGg@xuw8G44m6_LSnl-o<*O_T;z>Q|0&A8P}X*=%3-M9}mI9e^jM%dg8)XvPP19rx8 z?2bLLw;8Ge2jEZ~Wo9%6$C_D