clarify variable naming
I have made variable names longer to make it easier to follow the logic of the code. Also removed a few unused ones. Not tested for functionality, yet.
This commit is contained in:
@@ -10,8 +10,9 @@ from time import strftime
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django_mailbox.models import Message, MessageAttachment
|
||||
from rowers.models import Workout, Rower
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from rowers.models import Workout, Rower
|
||||
|
||||
from rowingdata import rower as rrower
|
||||
from rowingdata import rowingdata as rrdata
|
||||
@@ -28,100 +29,105 @@ os.environ['DJANGO_SETTINGS_MODULE'] = '$project_name$.settings'
|
||||
def rdata(file_obj, rower=rrower()):
|
||||
""" Read rowing data file and return 0 if file doesn't exist"""
|
||||
try:
|
||||
res = rrdata(file_obj, rower=rower)
|
||||
result = rrdata(file_obj, rower=rower)
|
||||
except IOError:
|
||||
res = 0
|
||||
result = 0
|
||||
|
||||
return res
|
||||
return result
|
||||
|
||||
def processattachment(rr, f2, title, uploadoptions):
|
||||
wid = [
|
||||
make_new_workout_from_email(rr, f2[6:], title)
|
||||
def processattachment(rower, filename, title, uploadoptions):
|
||||
workoutid = [
|
||||
make_new_workout_from_email(rower, filename[6:], title)
|
||||
]
|
||||
if wid:
|
||||
res += wid
|
||||
link = 'http://rowsandall.com/rowers/workout/' + \
|
||||
str(wid[0]) + '/edit'
|
||||
if workoutid:
|
||||
link = 'https://rowsandall.com'+reverse(
|
||||
rower.defaultlandingpage,
|
||||
kwargs = {
|
||||
'id':workoutid,
|
||||
}
|
||||
)
|
||||
|
||||
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)
|
||||
workout = Workout.objects.get(id=workoutid[0])
|
||||
uploads.do_sync(workout, uploadoptions)
|
||||
uploads.make_private(workout, uploadoptions)
|
||||
if 'make_plot' in uploadoptions:
|
||||
plottype = uploadoptions['plottype']
|
||||
f1 = w.csvfilename[6:-4]
|
||||
workoutcsvfilename = workout.csvfilename[6:-4]
|
||||
timestr = strftime("%Y%m%d-%H%M%S")
|
||||
imagename = f1 + timestr + '.png'
|
||||
resu = uploads.make_plot(
|
||||
r, w, f1,
|
||||
w.csvfilename,
|
||||
plottype, name,
|
||||
imagename = workoutcsvfilename + timestr + '.png'
|
||||
result = uploads.make_plot(
|
||||
workout.user, workout, workoutcsvfilename,
|
||||
workout.csvfilename,
|
||||
plottype, title,
|
||||
imagename=imagename
|
||||
)
|
||||
try:
|
||||
if wid:
|
||||
dd = send_confirm(
|
||||
rr.user, title, link,
|
||||
if workoutid:
|
||||
email_sent = send_confirm(
|
||||
rower.user, title, link,
|
||||
uploadoptions
|
||||
)
|
||||
time.sleep(10)
|
||||
except:
|
||||
try:
|
||||
time.sleep(10)
|
||||
if wid:
|
||||
dd = send_confirm(
|
||||
rr.user, title, link,
|
||||
if workoutid:
|
||||
email_sent = send_confirm(
|
||||
rower.user, title, link,
|
||||
uploadoptions
|
||||
)
|
||||
except:
|
||||
pass
|
||||
|
||||
return wid
|
||||
return workoutid
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""Run the Email processing command """
|
||||
def handle(self, *args, **options):
|
||||
res = []
|
||||
attachments = MessageAttachment.objects.all()
|
||||
cntr = 0
|
||||
for a in attachments:
|
||||
extension = a.document.name[-3:].lower()
|
||||
m = Message.objects.get(id=a.message_id)
|
||||
body = "\n".join(m.text.splitlines())
|
||||
for attachment in attachments:
|
||||
extension = attachment.document.name[-3:].lower()
|
||||
message = Message.objects.get(id=attachment.message_id)
|
||||
body = "\n".join(message.text.splitlines())
|
||||
uploadoptions = uploads.upload_options(body)
|
||||
from_address = m.from_address[0].lower()
|
||||
name = m.subject
|
||||
cntr += 1
|
||||
from_address = message.from_address[0].lower()
|
||||
name = message.subject
|
||||
# get a list of users
|
||||
# theusers = User.objects.filter(email=from_address)
|
||||
ther = [
|
||||
rowers = [
|
||||
r for r in Rower.objects.all() if r.user.email.lower() == from_address
|
||||
]
|
||||
for rr in ther:
|
||||
for rower in rowers:
|
||||
if extension == 'zip':
|
||||
z = zipfile.ZipFile(a.document)
|
||||
for f in z.namelist():
|
||||
f2 = z.extract(f, path='media/')
|
||||
title = os.path.basename(f2)
|
||||
wid = processattachment(rr, f2, title, uploadoptions)
|
||||
zip_file = zipfile.ZipFile(attachment.document)
|
||||
for filename in zip_file.namelist():
|
||||
datafile = zip_file.extract(filename, path='media/')
|
||||
title = os.path.basename(datafile)
|
||||
workoutid = processattachment(
|
||||
rower, datafile, title, uploadoptions
|
||||
)
|
||||
else:
|
||||
# move attachment and make workout
|
||||
wid = processattachment(rr, a.document, name, uploadoptions)
|
||||
workoutid = processattachment(
|
||||
rower, attachment.document, name, uploadoptions
|
||||
)
|
||||
|
||||
# We're done with the attachment. It can be deleted
|
||||
try:
|
||||
a.delete()
|
||||
attachment.delete()
|
||||
except IOError:
|
||||
pass
|
||||
|
||||
if m.attachments.exists() is False:
|
||||
if message.attachments.exists() is False:
|
||||
# no attachments, so can be deleted
|
||||
m.delete()
|
||||
message.delete()
|
||||
|
||||
mm = Message.objects.all()
|
||||
for m in mm:
|
||||
if m.attachments.exists() is False:
|
||||
m.delete()
|
||||
messages = Message.objects.all()
|
||||
for message in messages:
|
||||
if message.attachments.exists() is False:
|
||||
message.delete()
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
'Successfully processed email attachments'))
|
||||
|
||||
Reference in New Issue
Block a user