Private
Public Access
1
0

fixes and moved send_template_email to emails.py

This commit is contained in:
Sander Roosendaal
2018-03-20 08:29:22 +01:00
parent 4885101c50
commit e4db31f0ba
3 changed files with 110 additions and 86 deletions

View File

@@ -61,54 +61,11 @@ import requests
import longtask
import arrow
siteurl = SITE_URL
# testing task
from django.contrib.staticfiles import finders
def textify(html):
# Remove html tags and continuous whitespaces
text_only = re.sub('[ \t]+', ' ', strip_tags(html))
# Strip single spaces in the beginning of each line
return text_only.replace('\n ', '\n').strip()
def send_template_email(from_email,to_email,subject,
template,context,
*args,**kwargs):
htmly = env.get_template(template)
html_content = htmly.render(context)
text_content = textify(html_content)
msg = EmailMultiAlternatives(subject, text_content, from_email, to_email)
msg.attach_alternative(html_content, "text/html")
if 'attach_file' in kwargs:
fileobj = kwargs['attach_file']
if os.path.isfile(fileobj):
msg.attach_file(fileobj)
else:
try:
fileobj2 = fileobj
with gzip.open(fileobj+'.gz','rb') as f_in, open(fileobj2,'wb') as f_out:
shutil.copyfileobj(f_in,f_out)
msg.attach_file(fileobj2)
os.remove(fileobj2)
except IOError:
pass
if 'emailbounced' in kwargs:
emailbounced = kwargs['emailbounced']
else:
emailbounced = False
if not emailbounced:
res = msg.send()
else:
return 0
return res
from rowers.emails import send_template_email
@app.task
def add(x, y):
@@ -359,10 +316,6 @@ def handle_sendemail_breakthrough(workoutid, useremail,
else:
debug = False
siteurl = SITE_URL
if debug:
siteurl = SITE_URL_DEV
btvalues = pd.read_json(btvalues)
btvalues.sort_values('delta', axis=0, inplace=True)
@@ -407,9 +360,6 @@ def handle_sendemail_hard(workoutid, useremail,
else:
debug = False
siteurl = SITE_URL
if debug:
siteurl = SITE_URL_DEV
btvalues = pd.read_json(btvalues)
btvalues.sort_values('delta', axis=0, inplace=True)
@@ -514,7 +464,10 @@ def handle_sendemail_unrecognizedowner(useremail, userfirstname,
subject = "Unrecognized file from Rowsandall.com"
d = {'first_name':userfirstname}
d = {
'first_name':userfirstname,
'siteurl':siteurl,
}
from_email = 'Rowsandall <info@rowsandall.com>'
@@ -534,7 +487,9 @@ def handle_sendemailtcx(first_name, last_name, email, tcxfile,**kwargs):
subject = "File from Rowsandall.com"
d = {'first_name':first_name}
d = {'first_name':first_name,
'siteurl':siteurl,
}
from_email = 'Rowsandall <info@rowsandall.com>'
@@ -582,7 +537,9 @@ def handle_sendemailsummary(first_name, last_name, email, csvfile, **kwargs):
subject = "File from Rowsandall.com"
d = {'first_name':first_name}
d = {'first_name':first_name,
'siteurl':siteurl,
}
from_email = 'Rowsandall <info@rowsandall.com>'
@@ -608,7 +565,9 @@ def handle_sendemailcsv(first_name, last_name, email, csvfile,**kwargs):
fullemail = first_name + " " + last_name + " " + "<" + email + ">"
subject = "File from Rowsandall.com"
d = {'first_name':first_name}
d = {'first_name':first_name,
'siteurl':siteurl,
}
from_email = 'Rowsandall <info@rowsandall.com>'
@@ -750,7 +709,7 @@ def handle_otwsetpower(self,f1, boattype, weightvalue,
'workoutid':workoutid,
}
res = handle_template_email(from_email,[fullemail],
res = send_template_email(from_email,[fullemail],
subject,'otwpoweremail.html',d,
**kwargs)
@@ -1109,6 +1068,7 @@ def handle_sendemail_request_accept(email, name, teamname, managername,
'first_name':name,
'managername':managername,
'teamname':teamname,
'siteurl':siteurl,
}
res = send_template_email(from_email,[fullemail],subject,
'teamwelcomeemail.html',d,**kwargs)
@@ -1132,6 +1092,7 @@ def handle_sendemail_request_reject(email, name, teamname, managername,
'first_name':name,
'managername':managername,
'teamname':teamname,
'siteurl':siteurl,
}
res = send_template_email(from_email,[fullemail],subject,
'teamrejectemail.html',d,**kwargs)
@@ -1154,7 +1115,8 @@ def handle_sendemail_member_dropped(email, name, teamname, managername,
'first_name':name,
'managername':managername,
'teamname':teamname,
}
'siteurl':siteurl,
}
res = send_template_email(from_email,[fullemail],subject,
'teamdropemail.html',d,**kwargs)
@@ -1178,6 +1140,7 @@ def handle_sendemail_team_removed(email, name, teamname, managername,
'first_name':name,
'managername':managername,
'teamname':teamname,
'siteurl':siteurl,
}
res = send_template_email(from_email,[fullemail],subject,
'teamremoveemail.html',d,**kwargs)
@@ -1202,6 +1165,7 @@ def handle_sendemail_invite_reject(email, name, teamname, managername,
'first_name':name,
'managername':managername,
'teamname':teamname,
'siteurl':siteurl,
}
res = send_template_email(from_email,[fullemail],subject,
'teaminviterejectemail.html',d,**kwargs)
@@ -1225,6 +1189,7 @@ def handle_sendemail_invite_accept(email, name, teamname, managername,
'first_name':name,
'managername':managername,
'teamname':teamname,
'siteurl':siteurl,
}
res = send_template_email(from_email,[fullemail],subject,
'teaminviteacceptemail.html',d,**kwargs)