143 lines
4.2 KiB
Python
143 lines
4.2 KiB
Python
# Processes emails sent to workouts@rowsandall.com
|
|
""" Processes emails sent to workouts@rowsandall.com """
|
|
import shutil
|
|
import time
|
|
from django.conf import settings
|
|
from rowers.tasks import handle_sendemail_unrecognized
|
|
from django_mailbox.models import Message, MessageAttachment
|
|
from rowers.models import User, Rower, RowerForm
|
|
|
|
from django.core.mail import EmailMessage
|
|
|
|
from rowingdata import rower as rrower
|
|
|
|
from rowingdata import rowingdata as rrdata
|
|
|
|
from rowingdata import get_file_type
|
|
|
|
import zipfile
|
|
import os
|
|
import rowers.dataprep as dataprep
|
|
|
|
import django_rq
|
|
queue = django_rq.get_queue('default')
|
|
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):
|
|
""" Send confirmation email to user when email has been processed """
|
|
fullemail = u.email
|
|
subject = 'Workout added: ' + name
|
|
message = 'Dear ' + u.first_name + ',\n\n'
|
|
message += "Your workout has been added to Rowsandall.com.\n"
|
|
message += "Link to workout: " + link + "\n\n"
|
|
message += "Best Regards, the Rowsandall Team"
|
|
|
|
if options:
|
|
message += "\n\n" + str(options)
|
|
|
|
email = EmailMessage(subject, message,
|
|
'Rowsandall <info@rowsandall.com>',
|
|
[fullemail])
|
|
|
|
res = email.send()
|
|
|
|
return 1
|
|
|
|
# Reads a "rowingdata" object, plus some error protections
|
|
|
|
|
|
def rdata(file, rower=rrower()):
|
|
""" Reads rowingdata data or returns 0 on Error """
|
|
try:
|
|
res = rrdata(file, rower=rower)
|
|
except IOError:
|
|
try:
|
|
res = rrdata(file + '.gz', rower=rower)
|
|
except IOError:
|
|
res = 0
|
|
|
|
return res
|
|
|
|
|
|
|
|
def make_new_workout_from_email(rr, f2, name, cntr=0):
|
|
""" This one is used in processemail """
|
|
workouttype = 'rower'
|
|
|
|
try:
|
|
f2 = f2.name
|
|
fileformat = get_file_type('media/' + f2)
|
|
except IOError:
|
|
f2 = f2.name + '.gz'
|
|
fileformat = get_file_type('media/' + f2)
|
|
except AttributeError:
|
|
fileformat = get_file_type('media/' + f2)
|
|
|
|
if len(fileformat) == 3 and fileformat[0] == 'zip':
|
|
f_to_be_deleted = f2
|
|
with zipfile.ZipFile('media/' + f2) as z:
|
|
f2 = z.extract(z.namelist()[0], path='media/')[6:]
|
|
fileformat = fileformat[2]
|
|
|
|
if fileformat == 'unknown':
|
|
fcopy = "copy_of_"+f2
|
|
with open(f2, 'r') as f_in, open(fcopy, 'w') as f_out:
|
|
shutil.copyfileobj(f_in,f_out)
|
|
if settings.DEBUG:
|
|
res = handle_sendemail_unrecognized.delay(fcopy,
|
|
"roosendaalsander@gmail.com")
|
|
|
|
else:
|
|
res = queuehigh.enqueue(handle_sendemail_unrecognized,
|
|
fcopy, "roosendaalsander@gmail.com")
|
|
|
|
return 0
|
|
|
|
summary = ''
|
|
# handle non-Painsled
|
|
if fileformat != 'csv':
|
|
f3, summary, oarlength, inboard = dataprep.handle_nonpainsled(
|
|
'media/' + f2, fileformat, summary)
|
|
else:
|
|
f3 = 'media/' + f2
|
|
inboard = 0.88
|
|
oarlength = 2.89
|
|
|
|
row = rdata(f3)
|
|
if row == 0:
|
|
return 0
|
|
|
|
# change filename
|
|
if f2[:5] != 'media':
|
|
timestr = time.strftime("%Y%m%d-%H%M%S")
|
|
f2 = 'media/' + timestr + str(cntr) + 'o.csv'
|
|
|
|
try:
|
|
avglat = row.df[' latitude'].mean()
|
|
avglon = row.df[' longitude'].mean()
|
|
if avglat != 0 or avglon != 0:
|
|
workouttype = 'water'
|
|
except KeyError:
|
|
pass
|
|
|
|
row.write_csv(f2, gzip=True)
|
|
dosummary = (fileformat != 'fit')
|
|
|
|
if name == '':
|
|
name = 'imported through email'
|
|
|
|
id, message = dataprep.save_workout_database(f2, rr,
|
|
workouttype=workouttype,
|
|
dosummary=dosummary,
|
|
inboard=inboard,
|
|
oarlength=oarlength,
|
|
title=name,
|
|
workoutsource=fileformat,
|
|
notes='imported through email')
|
|
|
|
return id
|