cleaned up processemail
This commit is contained in:
@@ -1,139 +1,137 @@
|
||||
#!/srv/venv/bin/python
|
||||
""" Process emails """
|
||||
import sys
|
||||
import os
|
||||
|
||||
import zipfile
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
import time
|
||||
from time import strftime
|
||||
from django.conf import settings
|
||||
|
||||
from django_mailbox.models import Message, MessageAttachment
|
||||
from rowers.models import Workout, Rower
|
||||
|
||||
|
||||
from rowingdata import rower as rrower
|
||||
|
||||
from rowingdata import rowingdata as rrdata
|
||||
|
||||
from rowers.mailprocessing import make_new_workout_from_email, send_confirm
|
||||
import rowers.uploads as uploads
|
||||
|
||||
# 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$')
|
||||
|
||||
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
|
||||
import time
|
||||
from time import strftime
|
||||
from django.conf import settings
|
||||
from rowers.tasks import handle_sendemail_unrecognized
|
||||
from django_mailbox.models import Mailbox,Message,MessageAttachment
|
||||
from rowers.models import Workout, User, Rower, WorkoutForm,RowerForm,GraphImage,AdvancedWorkoutForm
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
from rowsandall_app.settings import BASE_DIR
|
||||
|
||||
from rowingdata import rower as rrower
|
||||
from rowingdata import main as rmain
|
||||
from rowingdata import rowingdata as rrdata
|
||||
|
||||
from rowingdata import make_cumvalues
|
||||
from rowingdata import summarydata,get_file_type
|
||||
|
||||
from scipy.signal import savgol_filter
|
||||
from rowers.mailprocessing import make_new_workout_from_email,send_confirm
|
||||
import rowers.uploads as uploads
|
||||
|
||||
def rdata(file,rower=rrower()):
|
||||
def rdata(file, rower=rrower()):
|
||||
""" Read rowing data file and return 0 if file doesn't exist"""
|
||||
try:
|
||||
res = rrdata(file,rower=rower)
|
||||
res = rrdata(file, rower=rower)
|
||||
except IOError:
|
||||
res = 0
|
||||
res = 0
|
||||
|
||||
return res
|
||||
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""Run the Email processing command """
|
||||
def handle(self, *args, **options):
|
||||
res = []
|
||||
attachments = MessageAttachment.objects.all()
|
||||
cntr = 0
|
||||
for a in attachments:
|
||||
res = []
|
||||
attachments = MessageAttachment.objects.all()
|
||||
cntr = 0
|
||||
for a in attachments:
|
||||
extension = a.document.name[-3:].lower()
|
||||
donotdelete = 0
|
||||
m = Message.objects.get(id=a.message_id)
|
||||
donotdelete = 0
|
||||
m = Message.objects.get(id=a.message_id)
|
||||
body = "\n".join(m.text.splitlines())
|
||||
uploadoptions = uploads.upload_options(body)
|
||||
from_address = m.from_address[0].lower()
|
||||
name = m.subject
|
||||
cntr += 1
|
||||
# get a list of users
|
||||
# theusers = User.objects.filter(email=from_address)
|
||||
from_address = m.from_address[0].lower()
|
||||
name = m.subject
|
||||
cntr += 1
|
||||
# get a list of users
|
||||
# theusers = User.objects.filter(email=from_address)
|
||||
ther = [
|
||||
r for r in Rower.objects.all() if r.user.email.lower() == from_address
|
||||
]
|
||||
for rr in ther:
|
||||
for rr in ther:
|
||||
if extension == 'zip':
|
||||
z = zipfile.ZipFile(a.document)
|
||||
for f in z.namelist():
|
||||
f2 = z.extract(f,path='media/')
|
||||
f2 = z.extract(f, path='media/')
|
||||
title = os.path.basename(f2)
|
||||
wid = [
|
||||
make_new_workout_from_email(rr,f2[6:],title)
|
||||
make_new_workout_from_email(rr, f2[6:], title)
|
||||
]
|
||||
res += wid
|
||||
link = 'http://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit'
|
||||
link = 'http://rowsandall.com/rowers/workout/' + \
|
||||
str(wid[0]) + '/edit'
|
||||
if uploadoptions and not 'error' in uploadoptions:
|
||||
w = Workout.objects.get(id=wid[0])
|
||||
r = w.user
|
||||
uploads.do_sync(w,uploadoptions)
|
||||
uploads.make_private(w,uploadoptions)
|
||||
uploads.do_sync(w, uploadoptions)
|
||||
uploads.make_private(w, uploadoptions)
|
||||
if 'make_plot' in uploadoptions:
|
||||
plottype = uploadoptions['plottype']
|
||||
f1 = w.csvfilename[6:-4]
|
||||
f1 = w.csvfilename[6:-4]
|
||||
timestr = strftime("%Y%m%d-%H%M%S")
|
||||
imagename = f1+timestr+'.png'
|
||||
resu = uploads.make_plot(r,w,f1,
|
||||
imagename = f1 + timestr + '.png'
|
||||
resu = uploads.make_plot(r, w, f1,
|
||||
w.csvfilename,
|
||||
plottype,name,
|
||||
plottype, name,
|
||||
imagename=imagename)
|
||||
try:
|
||||
if wid != 1:
|
||||
dd = send_confirm(rr.user,title,link,
|
||||
dd = send_confirm(rr.user, title, link,
|
||||
uploadoptions)
|
||||
time.sleep(10)
|
||||
except:
|
||||
try:
|
||||
time.sleep(10)
|
||||
if wid != 1:
|
||||
dd = send_confirm(rr.user,title,link,
|
||||
dd = send_confirm(rr.user, title, link,
|
||||
uploadoptions)
|
||||
except:
|
||||
pass
|
||||
|
||||
else:
|
||||
# move attachment and make workout
|
||||
try:
|
||||
wid = [
|
||||
# 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'
|
||||
link = 'http://rowsandall.com/rowers/workout/' + \
|
||||
str(wid[0]) + '/edit'
|
||||
if uploadoptions:
|
||||
w = Workout.objects.get(id=wid[0])
|
||||
r = w.user
|
||||
uploads.do_sync(w,uploadoptions)
|
||||
uploads.make_private(w,uploadoptions)
|
||||
uploads.do_sync(w, uploadoptions)
|
||||
uploads.make_private(w, uploadoptions)
|
||||
if 'make_plot' in uploadoptions:
|
||||
plottype = uploadoptions['plottype']
|
||||
f1 = w.csvfilename[6:-4]
|
||||
f1 = w.csvfilename[6:-4]
|
||||
timestr = strftime("%Y%m%d-%H%M%S")
|
||||
imagename = f1+timestr+'.png'
|
||||
resu = uploads.make_plot(r,w,f1,
|
||||
imagename = f1 + timestr + '.png'
|
||||
resu = uploads.make_plot(r, w, f1,
|
||||
w.csvfilename,
|
||||
plottype,name,
|
||||
plottype, name,
|
||||
imagename=imagename)
|
||||
|
||||
except:
|
||||
# replace with code to process error
|
||||
res += ['fail: '+name]
|
||||
donotdelete = 1
|
||||
except:
|
||||
# replace with code to process error
|
||||
res += ['fail: ' + name]
|
||||
donotdelete = 1
|
||||
wid = 1
|
||||
try:
|
||||
if wid != 1:
|
||||
dd = send_confirm(rr.user,name,link,
|
||||
dd = send_confirm(rr.user, name, link,
|
||||
uploadoptions)
|
||||
time.sleep(10)
|
||||
except:
|
||||
@@ -144,19 +142,16 @@ class Command(BaseCommand):
|
||||
except IOError:
|
||||
pass
|
||||
# remove attachment
|
||||
#if donotdelete == 0:
|
||||
# if donotdelete == 0:
|
||||
|
||||
if m.attachments.exists()==False:
|
||||
# no attachments, so can be deleted
|
||||
m.delete()
|
||||
if m.attachments.exists() is False:
|
||||
# no attachments, so can be deleted
|
||||
m.delete()
|
||||
|
||||
mm = Message.objects.all()
|
||||
for m in mm:
|
||||
if m.attachments.exists()==False:
|
||||
m.delete()
|
||||
mm = Message.objects.all()
|
||||
for m in mm:
|
||||
if m.attachments.exists() is False:
|
||||
m.delete()
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Successfully processed email attachments'))
|
||||
|
||||
|
||||
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
'Successfully processed email attachments'))
|
||||
|
||||
Reference in New Issue
Block a user