Skip to content

Commit

Permalink
Merge pull request #31 from deployed/default_attachments
Browse files Browse the repository at this point in the history
Sent default attachements together with email template
  • Loading branch information
czubik8805 authored Nov 20, 2020
2 parents c87c4c9 + 720801b commit 9b7d97f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Assumptions
Changelog
=========

1.1.8
-----
* send related attachments together with email templates

1.1.7
-----
* added missing translations [pl]
Expand Down
37 changes: 32 additions & 5 deletions emailtemplates/email.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# coding=utf-8
import os
import logging
import six
import os
from smtplib import SMTPException

from django.conf import settings
Expand Down Expand Up @@ -109,13 +108,19 @@ def __compile_template(self):
if not self.compiled_template:
self.compiled_template = Template(self.template)

def get_context(self):
self.context.update({
"default_attachments": self.get_default_attachments(as_links=True)
})
return self.context

def render_message(self):
self.__compile_template()
try:
message = self.compiled_template.render(self.context) #
message = self.compiled_template.render(self.get_context()) #
except AttributeError:
# NOTE: for template from string Context() is still required!
message = self.compiled_template.render(Context(self.context))
message = self.compiled_template.render(Context(self.get_context()))
self.message = message

def get_message_object(self, send_to, attachment_paths, *args, **kwargs):
Expand Down Expand Up @@ -153,6 +158,25 @@ def send_email(self, send_to, attachment_paths=None, fail_silently=True, *args,

return self.sent

def get_default_attachments(self, as_links=False):
"""
Prepare default attachments data (files will be include into email as attachments)
"""
attachments = []
try:
tmp = self.get_template_object()
except ObjectDoesNotExist:
return attachments

for attachment in tmp.attachments.filter(send_as_link=as_links):
if as_links:
attachments.append(attachment.attachment_file.url)
else:
attachments.append(
(os.path.basename(attachment.attachment_file.name), attachment.attachment_file.read())
)
return attachments

def send(self, to, attachment_paths=None, *args, **kwargs):
"""This function does all the operations on eft object, that are necessary to send email.
Usually one would use eft object like this:
Expand All @@ -162,9 +186,12 @@ def send(self, to, attachment_paths=None, *args, **kwargs):
eft.send_email(['email@example.com'])
return eft.sent
"""
attachments = self.get_default_attachments(as_links=False)
attachments.extend(kwargs.get('attachments', []))

self.get_object()
self.render_message()
self.send_email(to, attachment_paths, *args, **kwargs)
self.send_email(to, attachment_paths, attachments=attachments, *args, **kwargs)
if self.sent:
logger.info(u"Mail has been sent to: %s ", to)
return self.sent
3 changes: 3 additions & 0 deletions emailtemplates/locale/pl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ msgstr "Pokaż podgląd wiadomości"
msgid "Actions"
msgstr "Akcje"

msgid "Send as link"
msgstr "Wyślij jako link"

#: emailtemplates/apps.py:8
msgid "E-MAIL TEMPLATES"
msgstr "Szblony wiadomości E-mail"
Expand Down
23 changes: 23 additions & 0 deletions emailtemplates/migrations/0008_auto_20201120_1056.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1.2 on 2020-11-20 10:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('emailtemplates', '0007_auto_20201113_1354'),
]

operations = [
migrations.AddField(
model_name='emailattachment',
name='send_as_link',
field=models.BooleanField(default=True, verbose_name='Send as link'),
),
migrations.AddField(
model_name='massemailattachment',
name='send_as_link',
field=models.BooleanField(default=True, verbose_name='Send as link'),
),
]
1 change: 1 addition & 0 deletions emailtemplates/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def save(self, *args, **kwargs):
class BaseEmailAttachment(models.Model):
name = models.CharField(_("name"), blank=True, max_length=50)
attachment_file = models.FileField(_(u"Attachment file"), upload_to="emails/attachments/")
send_as_link = models.BooleanField(verbose_name=_("Send as link"), default=True)

class Meta:
abstract = True
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name='django-emailtemplates',
version='1.1.7',
version='1.1.8',
packages=find_packages(),
include_package_data=True,
license='MIT License',
Expand Down

0 comments on commit 9b7d97f

Please sign in to comment.