fixes and moved send_template_email to emails.py
This commit is contained in:
111
rowers/emails.py
111
rowers/emails.py
@@ -1,31 +1,90 @@
|
|||||||
from django.core.mail.message import MIMEText
|
import os
|
||||||
#from email.mime.application import MIMEApplication
|
import time
|
||||||
from django.core.mail.message import MIMEMultipart
|
import gc
|
||||||
|
import gzip
|
||||||
|
import shutil
|
||||||
|
import numpy as np
|
||||||
|
import re
|
||||||
|
|
||||||
# boto3 email functionality
|
from scipy import optimize
|
||||||
import boto3
|
|
||||||
AWS_REGION = "us-west-2"
|
|
||||||
CHARSET = "UTF-8"
|
|
||||||
client = boto3.client('ses',region_name=AWS_REGION)
|
|
||||||
|
|
||||||
def sendemail(fromaddress,recipients,subjecttext,bodytext,attachment=None):
|
import rowingdata
|
||||||
msg = MIMEMultipart()
|
|
||||||
msg['Subject'] = subjecttext
|
|
||||||
msg['From'] = fromaddress
|
|
||||||
msg['To'] = recipients
|
|
||||||
part = MIMEText(bodytext)
|
|
||||||
msg.attach(part)
|
|
||||||
|
|
||||||
# if attachment:
|
from rowingdata import rowingdata as rdata
|
||||||
# part = MIMEApplication(open(attachment,'rb').read())
|
|
||||||
# part.add_header('Content-Disposition', 'attachment',
|
|
||||||
# filename=attachment)
|
|
||||||
# msg.attach(part)
|
|
||||||
|
|
||||||
result = client.send_raw_email(
|
from celery import app
|
||||||
Source=msg['From'],
|
import datetime
|
||||||
Destination=msg['To'],
|
import pytz
|
||||||
RawMessage=msg
|
import iso8601
|
||||||
)
|
|
||||||
|
|
||||||
return result
|
from matplotlib.backends.backend_agg import FigureCanvas
|
||||||
|
#from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
from rowsandall_app.settings import SITE_URL
|
||||||
|
from rowsandall_app.settings_dev import SITE_URL as SITE_URL_DEV
|
||||||
|
from rowsandall_app.settings import PROGRESS_CACHE_SECRET
|
||||||
|
from rowsandall_app.settings import SETTINGS_NAME
|
||||||
|
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
from django_rq import job
|
||||||
|
from django.utils import timezone
|
||||||
|
from django.utils.html import strip_tags
|
||||||
|
|
||||||
|
from django.core.mail import (
|
||||||
|
send_mail,
|
||||||
|
EmailMessage,EmailMultiAlternatives,
|
||||||
|
)
|
||||||
|
|
||||||
|
from django.template import Context
|
||||||
|
from django.db.utils import OperationalError
|
||||||
|
from jinja2 import Template,Environment,FileSystemLoader
|
||||||
|
env = Environment(loader = FileSystemLoader(["rowers/templates"]))
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -61,54 +61,11 @@ import requests
|
|||||||
import longtask
|
import longtask
|
||||||
import arrow
|
import arrow
|
||||||
|
|
||||||
|
siteurl = SITE_URL
|
||||||
|
|
||||||
# testing task
|
# testing task
|
||||||
|
|
||||||
from django.contrib.staticfiles import finders
|
from rowers.emails import send_template_email
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
@app.task
|
@app.task
|
||||||
def add(x, y):
|
def add(x, y):
|
||||||
@@ -359,10 +316,6 @@ def handle_sendemail_breakthrough(workoutid, useremail,
|
|||||||
else:
|
else:
|
||||||
debug = False
|
debug = False
|
||||||
|
|
||||||
siteurl = SITE_URL
|
|
||||||
if debug:
|
|
||||||
siteurl = SITE_URL_DEV
|
|
||||||
|
|
||||||
btvalues = pd.read_json(btvalues)
|
btvalues = pd.read_json(btvalues)
|
||||||
btvalues.sort_values('delta', axis=0, inplace=True)
|
btvalues.sort_values('delta', axis=0, inplace=True)
|
||||||
|
|
||||||
@@ -407,9 +360,6 @@ def handle_sendemail_hard(workoutid, useremail,
|
|||||||
else:
|
else:
|
||||||
debug = False
|
debug = False
|
||||||
|
|
||||||
siteurl = SITE_URL
|
|
||||||
if debug:
|
|
||||||
siteurl = SITE_URL_DEV
|
|
||||||
|
|
||||||
btvalues = pd.read_json(btvalues)
|
btvalues = pd.read_json(btvalues)
|
||||||
btvalues.sort_values('delta', axis=0, inplace=True)
|
btvalues.sort_values('delta', axis=0, inplace=True)
|
||||||
@@ -514,7 +464,10 @@ def handle_sendemail_unrecognizedowner(useremail, userfirstname,
|
|||||||
subject = "Unrecognized file from Rowsandall.com"
|
subject = "Unrecognized file from Rowsandall.com"
|
||||||
|
|
||||||
|
|
||||||
d = {'first_name':userfirstname}
|
d = {
|
||||||
|
'first_name':userfirstname,
|
||||||
|
'siteurl':siteurl,
|
||||||
|
}
|
||||||
|
|
||||||
from_email = 'Rowsandall <info@rowsandall.com>'
|
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"
|
subject = "File from Rowsandall.com"
|
||||||
|
|
||||||
|
|
||||||
d = {'first_name':first_name}
|
d = {'first_name':first_name,
|
||||||
|
'siteurl':siteurl,
|
||||||
|
}
|
||||||
|
|
||||||
from_email = 'Rowsandall <info@rowsandall.com>'
|
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"
|
subject = "File from Rowsandall.com"
|
||||||
|
|
||||||
|
|
||||||
d = {'first_name':first_name}
|
d = {'first_name':first_name,
|
||||||
|
'siteurl':siteurl,
|
||||||
|
}
|
||||||
|
|
||||||
from_email = 'Rowsandall <info@rowsandall.com>'
|
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 + ">"
|
fullemail = first_name + " " + last_name + " " + "<" + email + ">"
|
||||||
subject = "File from Rowsandall.com"
|
subject = "File from Rowsandall.com"
|
||||||
|
|
||||||
d = {'first_name':first_name}
|
d = {'first_name':first_name,
|
||||||
|
'siteurl':siteurl,
|
||||||
|
}
|
||||||
|
|
||||||
from_email = 'Rowsandall <info@rowsandall.com>'
|
from_email = 'Rowsandall <info@rowsandall.com>'
|
||||||
|
|
||||||
@@ -750,7 +709,7 @@ def handle_otwsetpower(self,f1, boattype, weightvalue,
|
|||||||
'workoutid':workoutid,
|
'workoutid':workoutid,
|
||||||
}
|
}
|
||||||
|
|
||||||
res = handle_template_email(from_email,[fullemail],
|
res = send_template_email(from_email,[fullemail],
|
||||||
subject,'otwpoweremail.html',d,
|
subject,'otwpoweremail.html',d,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
|
|
||||||
@@ -1109,6 +1068,7 @@ def handle_sendemail_request_accept(email, name, teamname, managername,
|
|||||||
'first_name':name,
|
'first_name':name,
|
||||||
'managername':managername,
|
'managername':managername,
|
||||||
'teamname':teamname,
|
'teamname':teamname,
|
||||||
|
'siteurl':siteurl,
|
||||||
}
|
}
|
||||||
res = send_template_email(from_email,[fullemail],subject,
|
res = send_template_email(from_email,[fullemail],subject,
|
||||||
'teamwelcomeemail.html',d,**kwargs)
|
'teamwelcomeemail.html',d,**kwargs)
|
||||||
@@ -1132,6 +1092,7 @@ def handle_sendemail_request_reject(email, name, teamname, managername,
|
|||||||
'first_name':name,
|
'first_name':name,
|
||||||
'managername':managername,
|
'managername':managername,
|
||||||
'teamname':teamname,
|
'teamname':teamname,
|
||||||
|
'siteurl':siteurl,
|
||||||
}
|
}
|
||||||
res = send_template_email(from_email,[fullemail],subject,
|
res = send_template_email(from_email,[fullemail],subject,
|
||||||
'teamrejectemail.html',d,**kwargs)
|
'teamrejectemail.html',d,**kwargs)
|
||||||
@@ -1154,7 +1115,8 @@ def handle_sendemail_member_dropped(email, name, teamname, managername,
|
|||||||
'first_name':name,
|
'first_name':name,
|
||||||
'managername':managername,
|
'managername':managername,
|
||||||
'teamname':teamname,
|
'teamname':teamname,
|
||||||
}
|
'siteurl':siteurl,
|
||||||
|
}
|
||||||
res = send_template_email(from_email,[fullemail],subject,
|
res = send_template_email(from_email,[fullemail],subject,
|
||||||
'teamdropemail.html',d,**kwargs)
|
'teamdropemail.html',d,**kwargs)
|
||||||
|
|
||||||
@@ -1178,6 +1140,7 @@ def handle_sendemail_team_removed(email, name, teamname, managername,
|
|||||||
'first_name':name,
|
'first_name':name,
|
||||||
'managername':managername,
|
'managername':managername,
|
||||||
'teamname':teamname,
|
'teamname':teamname,
|
||||||
|
'siteurl':siteurl,
|
||||||
}
|
}
|
||||||
res = send_template_email(from_email,[fullemail],subject,
|
res = send_template_email(from_email,[fullemail],subject,
|
||||||
'teamremoveemail.html',d,**kwargs)
|
'teamremoveemail.html',d,**kwargs)
|
||||||
@@ -1202,6 +1165,7 @@ def handle_sendemail_invite_reject(email, name, teamname, managername,
|
|||||||
'first_name':name,
|
'first_name':name,
|
||||||
'managername':managername,
|
'managername':managername,
|
||||||
'teamname':teamname,
|
'teamname':teamname,
|
||||||
|
'siteurl':siteurl,
|
||||||
}
|
}
|
||||||
res = send_template_email(from_email,[fullemail],subject,
|
res = send_template_email(from_email,[fullemail],subject,
|
||||||
'teaminviterejectemail.html',d,**kwargs)
|
'teaminviterejectemail.html',d,**kwargs)
|
||||||
@@ -1225,6 +1189,7 @@ def handle_sendemail_invite_accept(email, name, teamname, managername,
|
|||||||
'first_name':name,
|
'first_name':name,
|
||||||
'managername':managername,
|
'managername':managername,
|
||||||
'teamname':teamname,
|
'teamname':teamname,
|
||||||
|
'siteurl':siteurl,
|
||||||
}
|
}
|
||||||
res = send_template_email(from_email,[fullemail],subject,
|
res = send_template_email(from_email,[fullemail],subject,
|
||||||
'teaminviteacceptemail.html',d,**kwargs)
|
'teaminviteacceptemail.html',d,**kwargs)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{% extends "emailbase.html" %}
|
{% extends "emailbase.html" %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<p>Dear <strong>{{ name }}</strong>,</p>
|
<p>Dear <strong>{{ first_name }}</strong>,</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
{{ commenter_first_name }} {{ commenter_last_name }} has written
|
{{ commenter_first_name }} {{ commenter_last_name }} has written
|
||||||
|
|||||||
Reference in New Issue
Block a user