2016-05-07 21:05:46 +00:00
|
|
|
import six
|
|
|
|
from django.core.mail import send_mail
|
2016-05-25 06:23:32 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
from django.template.loader import render_to_string
|
|
|
|
|
2016-05-07 21:05:46 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
2016-05-25 06:23:32 +00:00
|
|
|
class BaseEmail(object):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.to = kwargs.get('to')
|
|
|
|
self.template_name = kwargs.get('template_name')
|
|
|
|
self.template_path = kwargs.get('template_path')
|
|
|
|
self.subject = kwargs.get('subject')
|
|
|
|
self.context = kwargs.get('context', {})
|
|
|
|
self.template_full_path = '%s%s' % (self.template_path, self.template_name)
|
2016-05-27 05:51:10 +00:00
|
|
|
text_content = render_to_string('%s.txt' % self.template_full_path, self.context)
|
|
|
|
html_content = render_to_string('%s.html' % self.template_full_path, self.context)
|
2016-05-25 06:23:32 +00:00
|
|
|
|
|
|
|
self.email = EmailMultiAlternatives(self.subject, text_content)
|
|
|
|
self.email.attach_alternative(html_content, "text/html")
|
2017-06-09 20:36:14 +00:00
|
|
|
self.email.from_email = kwargs.get(
|
|
|
|
'from',
|
|
|
|
'(ungleich) ungleich Support <info@ungleich.ch>'
|
|
|
|
)
|
2016-06-24 05:45:21 +00:00
|
|
|
self.email.to = [kwargs.get('to', 'info@ungleich.com')]
|
2016-05-25 06:23:32 +00:00
|
|
|
|
|
|
|
def send(self):
|
|
|
|
self.email.send()
|
|
|
|
|
|
|
|
|
2016-05-07 21:05:46 +00:00
|
|
|
class BaseMailer(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._slug = None
|
2016-12-13 15:54:05 +00:00
|
|
|
self.no_replay_mail = 'info@ungleich.ch'
|
2016-05-07 21:05:46 +00:00
|
|
|
|
|
|
|
if not hasattr(self, '_to'):
|
|
|
|
self._to = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def slug(self):
|
|
|
|
return self._slug
|
|
|
|
|
|
|
|
@slug.setter
|
|
|
|
def slug(self, val):
|
|
|
|
assert isinstance(val, six.string_types), "slug is not string: %r" % val
|
|
|
|
self._slug = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def registration(self):
|
|
|
|
return self.message
|
|
|
|
|
|
|
|
@registration.setter
|
|
|
|
def registration(self, val):
|
|
|
|
msg = "registration is not dict with fields subject,message"
|
|
|
|
assert type(val) is dict, msg
|
|
|
|
assert val.get('subject') and val.get('message'), msg
|
|
|
|
self._message, self._subject, self._from = (
|
|
|
|
val.get('message'), val.get('subject'), val.get('from'))
|
|
|
|
assert isinstance(self.slug, six.string_types), 'slug not set'
|
|
|
|
|
|
|
|
def send_mail(self, to=None):
|
|
|
|
if not to:
|
|
|
|
to = self._to
|
|
|
|
if not self.message:
|
|
|
|
raise NotImplementedError
|
|
|
|
send_mail(self._subject, self._message, self.no_replay_mail, [to])
|
|
|
|
|
|
|
|
|
|
|
|
class DigitalGlarusRegistrationMailer(BaseMailer):
|
|
|
|
message = settings.REGISTRATION_MESSAGE
|
|
|
|
|
|
|
|
def __init__(self, slug):
|
|
|
|
self.slug = slug
|
|
|
|
self.registration = self.message
|
|
|
|
self._message = self._message.format(slug=self._slug)
|
|
|
|
super().__init__()
|
2016-05-25 06:23:32 +00:00
|
|
|
|