Private
Public Access
1
0

refactored, not tested

This commit is contained in:
Sander Roosendaal
2017-10-22 13:01:16 +02:00
parent a866b253a1
commit f7ab21560d
2 changed files with 69 additions and 212 deletions

View File

@@ -5,22 +5,19 @@ import os
import zipfile
from django.core.management.base import BaseCommand
import time
from time import strftime
from django.conf import settings
from django.core.management.base import BaseCommand
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
from rowers.mailprocessing import make_new_workout_from_email, send_confirm
# If you find a solution that does not need the two paths, please comment!
sys.path.append('$path_to_root_of_project$')
@@ -28,15 +25,58 @@ sys.path.append('$path_to_root_of_project$/$project_name$')
os.environ['DJANGO_SETTINGS_MODULE'] = '$project_name$.settings'
def rdata(file, rower=rrower()):
def rdata(file_obj, rower=rrower()):
""" Read rowing data file and return 0 if file doesn't exist"""
try:
res = rrdata(file, rower=rower)
res = rrdata(file_obj, rower=rower)
except IOError:
res = 0
return res
def processattachment(rr, f2, title, uploadoptions):
wid = [
make_new_workout_from_email(rr, f2[6:], title)
]
if wid:
res += wid
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
uploads.do_sync(w, uploadoptions)
uploads.make_private(w, uploadoptions)
if 'make_plot' in uploadoptions:
plottype = uploadoptions['plottype']
f1 = w.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=imagename
)
try:
if wid:
dd = send_confirm(
rr.user, title, link,
uploadoptions
)
time.sleep(10)
except:
try:
time.sleep(10)
if wid:
dd = send_confirm(
rr.user, title, link,
uploadoptions
)
except:
pass
return wid
class Command(BaseCommand):
"""Run the Email processing command """
@@ -46,7 +86,6 @@ class Command(BaseCommand):
cntr = 0
for a in attachments:
extension = a.document.name[-3:].lower()
donotdelete = 0
m = Message.objects.get(id=a.message_id)
body = "\n".join(m.text.splitlines())
uploadoptions = uploads.upload_options(body)
@@ -64,85 +103,16 @@ class Command(BaseCommand):
for f in z.namelist():
f2 = z.extract(f, path='media/')
title = os.path.basename(f2)
wid = [
make_new_workout_from_email(rr, f2[6:], title)
]
res += wid
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
uploads.do_sync(w, uploadoptions)
uploads.make_private(w, uploadoptions)
if 'make_plot' in uploadoptions:
plottype = uploadoptions['plottype']
f1 = w.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=imagename)
try:
if wid != 1:
dd = send_confirm(rr.user, title, link,
uploadoptions)
time.sleep(10)
except:
try:
time.sleep(10)
if wid != 1:
dd = send_confirm(rr.user, title, link,
uploadoptions)
except:
pass
wid = processattachment(rr, f2, title, uploadoptions)
else:
# move attachment and make workout
try:
wid = [
make_new_workout_from_email(rr,
a.document,
name)
]
res += wid
link = 'http://rowsandall.com/rowers/workout/' + \
str(wid[0]) + '/edit'
if uploadoptions:
w = Workout.objects.get(id=wid[0])
r = w.user
uploads.do_sync(w, uploadoptions)
uploads.make_private(w, uploadoptions)
if 'make_plot' in uploadoptions:
plottype = uploadoptions['plottype']
f1 = w.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=imagename)
except:
# replace with code to process error
res += ['fail: ' + name]
donotdelete = 1
wid = 1
try:
if wid != 1:
dd = send_confirm(rr.user, name, link,
uploadoptions)
time.sleep(10)
except:
pass
wid = processattachment(rr, a.document, name, uploadoptions)
# We're done with the attachment. It can be deleted
try:
a.delete()
except IOError:
pass
# remove attachment
# if donotdelete == 0:
if m.attachments.exists() is False:
# no attachments, so can be deleted