Improve on mail view

This commit is contained in:
Kumi 2021-05-30 07:39:36 +02:00
parent 20499f81ae
commit c291147a7c
2 changed files with 49 additions and 7 deletions

View file

@ -1 +0,0 @@
from django.core.mail import send_mail

View file

@ -1,11 +1,54 @@
from django.views.generic.base import ContextMixin
from django.template.loader import render_to_string
from django.template.exceptions import TemplateDoesNotExist
from django.core.mail import EmailMultiAlternatives
from html.parser import HTMLParser
from bs4 import BeautifulSoup
class MailView(ContextMixin):
def render_html(**kwargs):
context = self.get_context_data(**kwargs)
return render_to_string(self.html_template_name, context)
def __init__(self, template_name=None, html_template_name=None, text_template_name=None):
self.html_template_name = html_template_name
self.text_template_name = text_template_name
def render_text(**kwargs):
context = self.get_context_data(**kwargs)
return render_to_string(self.text_template_name, context)
if self._template_name := template_name:
if len(template_name.split("/")[-1].split(".")[-1] in ("html", "txt")):
basename = template_name.rsplit(".", 1)[0]
else:
basename = template_name
for ext in ("html", "txt"):
try:
path = f"{basename}.{ext}"
render_to_string(path)
if ext == "html":
self.html_template_name = self.html_template_name or path
else:
self.text_template_name = self.text_template_name or path
except TemplateDoesNotExist:
pass
def render_to_html(self, **kwargs):
if self.html_template_name:
context = self.get_context_data(**kwargs)
return render_to_string(self.html_template_name, context)
else:
return None
def render_to_text(self, from_html=False, **kwargs):
if self.text_template_name:
context = self.get_context_data(**kwargs)
return render_to_string(self.text_template_name, context)
else:
if from_html and html := self.render_to_html(**kwargs):
return BeautifulSoup(html).get_text()
return None
def send(self, subject, recipient, context={}, attachments=[], sender=None, cc=[], bcc=[], text_from_html=False):
text = self.render_to_text(text_from_html, **context)
email = EmailMultiAlternatives(subject, text, sender, [recipient], cc=cc, bcc=bcc, attachments=attachments)
if html := self.render_to_html(**context):
email.attach_alternative(html, "text/html")
email.send()