Private
Public Access
1
0

cleaned up processemail

This commit is contained in:
Sander Roosendaal
2017-10-22 11:49:47 +02:00
parent e541289066
commit a866b253a1
2 changed files with 202 additions and 205 deletions

View File

@@ -30,6 +30,8 @@ queuelow = django_rq.get_queue('low')
queuehigh = django_rq.get_queue('default')
# Sends a confirmation with a link to the workout
def send_confirm(u, name, link, options):
fullemail = u.email
subject = 'Workout added: ' + name
@@ -41,7 +43,6 @@ def send_confirm(u,name,link,options):
if options:
message += "\n\n" + str(options)
email = EmailMessage(subject, message,
'Rowsandall <info@rowsandall.com>',
[fullemail])
@@ -51,6 +52,8 @@ def send_confirm(u,name,link,options):
return 1
# Reads a "rowingdata" object, plus some error protections
def rdata(file, rower=rrower()):
try:
res = rrdata(file, rower=rower)
@@ -63,6 +66,8 @@ def rdata(file,rower=rrower()):
return res
# Some error protection around process attachments
def safeprocessattachments():
try:
return processattachments()
@@ -72,6 +77,8 @@ def safeprocessattachments():
# This is duplicated in management/commands/processemail
# Need to double check the code there, update here, and only
# use the code here.
def processattachments():
# in res, we store the ids of the new workouts
res = []
@@ -91,7 +98,8 @@ def processattachments():
try:
wid = [make_new_workout_from_email(rr, a.document, name)]
res += wid
link = 'https://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit'
link = 'https://rowsandall.com/rowers/workout/' + \
str(wid[0]) + '/edit'
if wid != 1:
dd = send_confirm(u, name, link)
except:
@@ -101,7 +109,6 @@ def processattachments():
except Rower.DoesNotExist:
pass
# remove attachment
if donotdelete == 0:
a.delete()
@@ -119,6 +126,8 @@ def processattachments():
return res
# As above, but with some print commands for debugging purposes
def processattachments_debug():
res = []
attachments = MessageAttachment.objects.all()
@@ -143,12 +152,11 @@ def processattachments_debug():
print name
wid = [make_new_workout_from_email(rr, a.document, name)]
res += wid
link = 'https://rowsandall.com/rowers/workout/'+str(wid[0])+'/edit'
link = 'https://rowsandall.com/rowers/workout/' + \
str(wid[0]) + '/edit'
if wid != 1:
dd = send_confirm(u, name, link)
# remove attachment
if donotdelete == 0:
a.delete()
@@ -167,6 +175,8 @@ def processattachments_debug():
# Process the attachment file, create new workout
# The code here is duplication of the code in views.py (workout_upload_view)
# Need to move the code to a subroutine used both in views.py and here
def make_new_workout_from_email(rr, f2, name, cntr=0):
workouttype = 'rower'
@@ -199,15 +209,13 @@ def make_new_workout_from_email(rr,f2,name,cntr=0):
summary = ''
# handle non-Painsled
if fileformat != 'csv':
f3,summary,oarlength,inboard = dataprep.handle_nonpainsled('media/'+f2,fileformat,summary)
f3, summary, oarlength, inboard = dataprep.handle_nonpainsled(
'media/' + f2, fileformat, summary)
else:
f3 = 'media/' + f2
inboard = 0.88
oarlength = 2.89
# make workout and put in database
# r = rrower(hrmax=rr.max,hrut2=rr.ut2,
# hrut1=rr.ut1,hrat=rr.at,
@@ -216,7 +224,6 @@ def make_new_workout_from_email(rr,f2,name,cntr=0):
if row == 0:
return 0
# change filename
if f2[:5] != 'media':
timestr = time.strftime("%Y%m%d-%H%M%S")
@@ -245,9 +252,4 @@ def make_new_workout_from_email(rr,f2,name,cntr=0):
workoutsource=fileformat,
notes='imported through email')
return id

View File

@@ -1,39 +1,35 @@
#!/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()):
""" Read rowing data file and return 0 if file doesn't exist"""
try:
res = rrdata(file, rower=rower)
except IOError:
@@ -42,8 +38,8 @@ def rdata(file,rower=rrower()):
return res
class Command(BaseCommand):
"""Run the Email processing command """
def handle(self, *args, **options):
res = []
attachments = MessageAttachment.objects.all()
@@ -72,7 +68,8 @@ class Command(BaseCommand):
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
@@ -110,7 +107,8 @@ class Command(BaseCommand):
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
@@ -146,17 +144,14 @@ class Command(BaseCommand):
# remove attachment
# if donotdelete == 0:
if m.attachments.exists()==False:
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:
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'))