Private
Public Access
1
0

Complete and tested email processing

Processed successfully individual files, zip files, unrecognized files,
zips containing unrecognized files
This commit is contained in:
Sander Roosendaal
2017-10-22 16:04:03 +02:00
parent f36911d73b
commit 973bbb9f0b
4 changed files with 78 additions and 51 deletions

View File

@@ -27,11 +27,11 @@ queuehigh = django_rq.get_queue('default')
# Sends a confirmation with a link to the workout # Sends a confirmation with a link to the workout
def send_confirm(u, name, link, options): def send_confirm(user, name, link, options):
""" Send confirmation email to user when email has been processed """ """ Send confirmation email to user when email has been processed """
fullemail = u.email fullemail = user.email
subject = 'Workout added: ' + name subject = 'Workout added: ' + name
message = 'Dear ' + u.first_name + ',\n\n' message = 'Dear ' + user.first_name + ',\n\n'
message += "Your workout has been added to Rowsandall.com.\n" message += "Your workout has been added to Rowsandall.com.\n"
message += "Link to workout: " + link + "\n\n" message += "Link to workout: " + link + "\n\n"
message += "Best Regards, the Rowsandall Team" message += "Best Regards, the Rowsandall Team"
@@ -53,68 +53,73 @@ def send_confirm(u, name, link, options):
def rdata(file, rower=rrower()): def rdata(file, rower=rrower()):
""" Reads rowingdata data or returns 0 on Error """ """ Reads rowingdata data or returns 0 on Error """
try: try:
res = rrdata(file, rower=rower) result = rrdata(file, rower=rower)
except IOError: except IOError:
try: try:
res = rrdata(file + '.gz', rower=rower) result = rrdata(file + '.gz', rower=rower)
except IOError: except IOError:
res = 0 result = 0
return res return result
def make_new_workout_from_email(rr, f2, name, cntr=0): def make_new_workout_from_email(rower, datafile, name, cntr=0):
""" This one is used in processemail """ """ This one is used in processemail """
workouttype = 'rower' workouttype = 'rower'
try: try:
f2 = f2.name datafilename = datafile.name
fileformat = get_file_type('media/' + f2) fileformat = get_file_type('media/' + datafilename)
except IOError: except IOError:
f2 = f2.name + '.gz' datafilename = datafile.name + '.gz'
fileformat = get_file_type('media/' + f2) fileformat = get_file_type('media/' + datafilename)
except AttributeError: except AttributeError:
fileformat = get_file_type('media/' + f2) datafilename = datafile
fileformat = get_file_type('media/' + datafile)
if len(fileformat) == 3 and fileformat[0] == 'zip': if len(fileformat) == 3 and fileformat[0] == 'zip':
f_to_be_deleted = f2 with zipfile.ZipFile('media/' + datafilename) as zip_file:
with zipfile.ZipFile('media/' + f2) as z: datafilename = zip_file.extract(
f2 = z.extract(z.namelist()[0], path='media/')[6:] zip_file.namelist()[0],
path='media/')[6:]
fileformat = fileformat[2] fileformat = fileformat[2]
if fileformat == 'unknown': if fileformat == 'unknown':
fcopy = "copy_of_"+f2 # extension = datafilename[-4:].lower()
with open(f2, 'r') as f_in, open(fcopy, 'w') as f_out: # fcopy = "media/"+datafilename[:-4]+"_copy"+extension
shutil.copyfileobj(f_in,f_out) # with open('media/'+datafilename, 'r') as f_in, open(fcopy, 'w') as f_out:
# shutil.copyfileobj(f_in,f_out)
fcopy = "media/"+datafilename
if settings.DEBUG: if settings.DEBUG:
res = handle_sendemail_unrecognized.delay(fcopy, res = handle_sendemail_unrecognized.delay(fcopy,
"roosendaalsander@gmail.com") rower.user.email)
else: else:
res = queuehigh.enqueue(handle_sendemail_unrecognized, res = queuehigh.enqueue(handle_sendemail_unrecognized,
fcopy, "roosendaalsander@gmail.com") fcopy,
rower.user.email)
return 0 return 0
summary = '' summary = ''
# handle non-Painsled # handle non-Painsled
if fileformat != 'csv': if fileformat != 'csv':
f3, summary, oarlength, inboard = dataprep.handle_nonpainsled( filename_mediadir, summary, oarlength, inboard = dataprep.handle_nonpainsled(
'media/' + f2, fileformat, summary) 'media/' + datafilename, fileformat, summary)
else: else:
f3 = 'media/' + f2 filename_mediadir = 'media/' + datafilename
inboard = 0.88 inboard = 0.88
oarlength = 2.89 oarlength = 2.89
row = rdata(f3) row = rdata(filename_mediadir)
if row == 0: if row == 0:
return 0 return 0
# change filename # change filename
if f2[:5] != 'media': if datafilename[:5] != 'media':
timestr = time.strftime("%Y%m%d-%H%M%S") timestr = time.strftime("%Y%m%d-%H%M%S")
f2 = 'media/' + timestr + str(cntr) + 'o.csv' datafilename = 'media/' + timestr + str(cntr) + 'o.csv'
try: try:
avglat = row.df[' latitude'].mean() avglat = row.df[' latitude'].mean()
@@ -124,19 +129,21 @@ def make_new_workout_from_email(rr, f2, name, cntr=0):
except KeyError: except KeyError:
pass pass
row.write_csv(f2, gzip=True) row.write_csv(datafilename, gzip=True)
dosummary = (fileformat != 'fit') dosummary = (fileformat != 'fit')
if name == '': if name == '':
name = 'imported through email' name = 'imported through email'
id, message = dataprep.save_workout_database(f2, rr, id, message = dataprep.save_workout_database(
workouttype=workouttype, datafilename, rower,
dosummary=dosummary, workouttype=workouttype,
inboard=inboard, dosummary=dosummary,
oarlength=oarlength, inboard=inboard,
title=name, oarlength=oarlength,
workoutsource=fileformat, title=name,
notes='imported through email') workoutsource=fileformat,
notes='imported through email'
)
return id return id

View File

@@ -11,6 +11,7 @@ from time import strftime
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django_mailbox.models import Message, MessageAttachment from django_mailbox.models import Message, MessageAttachment
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.conf import settings
from rowers.models import Workout, Rower from rowers.models import Workout, Rower
@@ -26,6 +27,9 @@ sys.path.append('$path_to_root_of_project$/$project_name$')
os.environ['DJANGO_SETTINGS_MODULE'] = '$project_name$.settings' os.environ['DJANGO_SETTINGS_MODULE'] = '$project_name$.settings'
if not getattr(__builtins__, "WindowsError", None):
class WindowsError(OSError): pass
def rdata(file_obj, rower=rrower()): def rdata(file_obj, rower=rrower()):
""" Read rowing data file and return 0 if file doesn't exist""" """ Read rowing data file and return 0 if file doesn't exist"""
try: try:
@@ -35,15 +39,20 @@ def rdata(file_obj, rower=rrower()):
return result return result
def processattachment(rower, filename, title, uploadoptions): def processattachment(rower, fileobj, title, uploadoptions):
try:
filename = fileobj.name
except AttributeError:
filename = fileobj[6:]
workoutid = [ workoutid = [
make_new_workout_from_email(rower, filename[6:], title) make_new_workout_from_email(rower, filename, title)
] ]
if workoutid: if workoutid[0]:
link = 'https://rowsandall.com'+reverse( link = settings.SITE_URL+reverse(
rower.defaultlandingpage, rower.defaultlandingpage,
kwargs = { kwargs = {
'id':workoutid, 'id':workoutid[0],
} }
) )
@@ -62,23 +71,23 @@ def processattachment(rower, filename, title, uploadoptions):
plottype, title, plottype, title,
imagename=imagename imagename=imagename
) )
try:
if workoutid:
email_sent = send_confirm(
rower.user, title, link,
uploadoptions
)
time.sleep(10)
except:
try: try:
time.sleep(10)
if workoutid: if workoutid:
email_sent = send_confirm( email_sent = send_confirm(
rower.user, title, link, rower.user, title, link,
uploadoptions uploadoptions
) )
time.sleep(10)
except: except:
try: pass
time.sleep(10)
if workoutid:
email_sent = send_confirm(
rower.user, title, link,
uploadoptions
)
except:
pass
return workoutid return workoutid
@@ -119,6 +128,12 @@ class Command(BaseCommand):
attachment.delete() attachment.delete()
except IOError: except IOError:
pass pass
except WindowsError:
time.sleep(2)
try:
attachment.delete()
except WindowsError:
pass
if message.attachments.exists() is False: if message.attachments.exists() is False:
# no attachments, so can be deleted # no attachments, so can be deleted

View File

@@ -261,6 +261,9 @@ TP_CLIENT_SECRET = CFG["tp_client_secret"]
TP_REDIRECT_URI = CFG["tp_redirect_uri"] TP_REDIRECT_URI = CFG["tp_redirect_uri"]
TP_CLIENT_KEY = TP_CLIENT_ID TP_CLIENT_KEY = TP_CLIENT_ID
# Full Site URL
SITE_URL = "https://rowsandall.com"
# RQ stuff # RQ stuff
RQ_QUEUES = { RQ_QUEUES = {

View File

@@ -76,6 +76,8 @@ LOGIN_REDIRECT_URL = '/rowers/list-workouts/'
SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies" SESSION_ENGINE = "django.contrib.sessions.backends.signed_cookies"
SITE_URL = "http://localhost:8000"
#EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' #EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
#EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend' #EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'