Private
Public Access
1
0

some changes

This commit is contained in:
Sander Roosendaal
2021-12-16 16:13:58 +01:00
parent bcfeb28854
commit bdb0e55ca4
13 changed files with 31 additions and 846 deletions

View File

@@ -18,7 +18,7 @@ import json
import io
from django.core.management.base import BaseCommand
from django_mailbox.models import Message, MessageAttachment,Mailbox
#from django_mailbox.models import Message, MessageAttachment,Mailbox
from django.urls import reverse
from django.conf import settings
@@ -182,16 +182,6 @@ class Command(BaseCommand):
else: # pragma: no cover
testing = False
if 'mailbox' in options:
workoutmailbox = Mailbox.objects.get(name=options['mailbox'])
else: # pragma: no cover
workoutmailbox = Mailbox.objects.get(name='workouts')
if 'failedmailbox' in options: # pragma: no cover
failedmailbox = Mailbox.objects.get(name=options['failedmailbox'])
else: # pragma: no cover
failedmailbox = Mailbox.objects.get(name='Failed')
# Polar
try:
polar_available = polarstuff.get_polar_notifications()
@@ -235,100 +225,6 @@ class Command(BaseCommand):
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
dologging('processemail.log',''.join('!! ' + line for line in lines))
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
)
cntr = 0
for attachment in attachments:
filename, extension = os.path.splitext(attachment.document.name)
extension = extension.lower()
# extension = attachment.document.name[-3:].lower()
try:
message = Message.objects.get(id=attachment.message_id)
if message.encoded: # pragma: no cover
# if message.text:
body = "\n".join(message.text.splitlines())
else:
body = message.get_body()
uploadoptions = uploads.upload_options(body)
from_address = get_from_address(message)
name = message.subject
# get a list of users
# theusers = User.objects.filter(email=from_address)
rowers = [
r for r in Rower.objects.all() if r.user.email.lower() == from_address
]
try: # pragma: no cover
rowers2 = [
r for r in Rower.objects.all() if from_address in r.emailalternatives
]
rowers = rowers+rowers2
except TypeError:
pass
except IOError: # pragma: no cover
rowers = []
except Message.DoesNotExist: # pragma: no cover
try:
attachment.delete()
except:
pass
for rower in rowers:
if 'zip' in extension:
try:
zip_file = zipfile.ZipFile(attachment.document)
for id,filename in enumerate(zip_file.namelist()):
datafile = zip_file.extract(
filename, path='media/')
if id>0:
title = name+' ('+str(id+1)+')'
else:
title = name
workoutid = processattachment(
rower, datafile, title, uploadoptions,
testing=testing
)
except BadZipFile: # pragma: no cover
pass
else:
# move attachment and make workout
workoutid = processattachment(
rower, attachment.document, name, uploadoptions,
testing=testing
)
# We're done with the attachment. It can be deleted
try:
attachment.delete()
except IOError: # pragma: no cover
pass
except WindowsError: # pragma: no cover
if not testing:
time.sleep(2)
try:
attachment.delete()
except WindowsError:
pass
except: # pragma: no cover
message.mailbox = failedmailbox
message.save()
if message.attachments.exists() is False:
# no attachments, so can be deleted
message.delete()
messages = Message.objects.all()
for message in messages:
if message.attachments.exists() is False:
message.delete()
# Strava
#rowers = Rower.objects.filter(strava_auto_import=True)
#for r in rowers: