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

@@ -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