Private
Public Access
1
0

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:
Sander Roosendaal
2017-10-22 14:38:37 +02:00
parent f7ab21560d
commit f36911d73b

View File

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