From 30a43390d0678d6e3fc6ef826507cf2d433a33e6 Mon Sep 17 00:00:00 2001 From: Amin Dandache Date: Thu, 7 Sep 2017 13:50:43 +0200 Subject: [PATCH 1/2] improve attachment save with mailqueue storage --- mailqueue/models.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/mailqueue/models.py b/mailqueue/models.py index 6344d70..b1d53e9 100644 --- a/mailqueue/models.py +++ b/mailqueue/models.py @@ -4,6 +4,7 @@ from django.conf import settings +from django.core.files.base import ContentFile from django.core.mail import EmailMultiAlternatives from django.db import models from django.utils.encoding import python_2_unicode_compatible @@ -69,8 +70,21 @@ def add_attachment(self, attachment): if self.pk is None: self._save_without_sending() - Attachment.objects.create(email=self, file_attachment=attachment, - original_filename=attachment.file.name.split('/')[-1]) + original_filename = attachment.file.name.split('/')[-1] + file_content = ContentFile(attachment.read()) + + new_attachment = Attachment() + new_attachment.file_attachment.save(original_filename, file_content, save=False) + new_attachment.email = self + new_attachment.original_filename = original_filename + try: + new_attachment.save() + except Exception as e: + logger.error(e) + new_attachment.file_attachment.delete() + + # Attachment.objects.create(email=self, file_attachment=attachment, + # original_filename=attachment.file.name.split('/')[-1]) def _save_without_sending(self, *args, **kwargs): """ From dcd283c5279c0d958bab115c87b1a75daa2076a0 Mon Sep 17 00:00:00 2001 From: Amin Dandache Date: Thu, 7 Sep 2017 15:28:31 +0200 Subject: [PATCH 2/2] remove commented old code --- mailqueue/models.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/mailqueue/models.py b/mailqueue/models.py index b1d53e9..81dfa5b 100644 --- a/mailqueue/models.py +++ b/mailqueue/models.py @@ -83,9 +83,6 @@ def add_attachment(self, attachment): logger.error(e) new_attachment.file_attachment.delete() - # Attachment.objects.create(email=self, file_attachment=attachment, - # original_filename=attachment.file.name.split('/')[-1]) - def _save_without_sending(self, *args, **kwargs): """ Saves the MailerMessage instance without sending the e-mail. This ensures