From e4db31f0ba1b6be5fc90f60d608074ccad07ae5d Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Tue, 20 Mar 2018 08:29:22 +0100 Subject: [PATCH] fixes and moved send_template_email to emails.py --- rowers/emails.py | 111 ++++++++++++++++++------ rowers/tasks.py | 83 +++++------------- rowers/templates/teamresponseemail.html | 2 +- 3 files changed, 110 insertions(+), 86 deletions(-) diff --git a/rowers/emails.py b/rowers/emails.py index a5682019..dff06210 100644 --- a/rowers/emails.py +++ b/rowers/emails.py @@ -1,31 +1,90 @@ -from django.core.mail.message import MIMEText -#from email.mime.application import MIMEApplication -from django.core.mail.message import MIMEMultipart +import os +import time +import gc +import gzip +import shutil +import numpy as np +import re -# boto3 email functionality -import boto3 -AWS_REGION = "us-west-2" -CHARSET = "UTF-8" -client = boto3.client('ses',region_name=AWS_REGION) +from scipy import optimize -def sendemail(fromaddress,recipients,subjecttext,bodytext,attachment=None): - msg = MIMEMultipart() - msg['Subject'] = subjecttext - msg['From'] = fromaddress - msg['To'] = recipients - part = MIMEText(bodytext) - msg.attach(part) +import rowingdata - # if attachment: - # part = MIMEApplication(open(attachment,'rb').read()) - # part.add_header('Content-Disposition', 'attachment', - # filename=attachment) - # msg.attach(part) +from rowingdata import rowingdata as rdata - result = client.send_raw_email( - Source=msg['From'], - Destination=msg['To'], - RawMessage=msg - ) +from celery import app +import datetime +import pytz +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 diff --git a/rowers/tasks.py b/rowers/tasks.py index 92d0f3f2..c5625658 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -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 ' @@ -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 ' @@ -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 ' @@ -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 ' @@ -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) diff --git a/rowers/templates/teamresponseemail.html b/rowers/templates/teamresponseemail.html index 3b7d181a..9ee293bc 100644 --- a/rowers/templates/teamresponseemail.html +++ b/rowers/templates/teamresponseemail.html @@ -1,7 +1,7 @@ {% extends "emailbase.html" %} {% block body %} -

Dear {{ name }},

+

Dear {{ first_name }},

{{ commenter_first_name }} {{ commenter_last_name }} has written