From 4c901880bca5b4e8cd4b3aa68fb86133e622c375 Mon Sep 17 00:00:00 2001 From: Ross Poulton Date: Tue, 2 Sep 2014 18:36:00 +1000 Subject: [PATCH] Allow file attachments in storages other than local files (eg S3). Fixes GH-249. --- helpdesk/forms.py | 4 ++-- helpdesk/lib.py | 14 +++++++------- helpdesk/views/staff.py | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index da46bc31..80b03b02 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -268,7 +268,7 @@ class TicketForm(CustomFieldMixin, forms.Form): # Only files smaller than 512kb (or as defined in # settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email. try: - files.append(a.file.path) + files.append([a.filename, a.file]) except NotImplementedError: pass @@ -441,7 +441,7 @@ class PublicTicketForm(CustomFieldMixin, forms.Form): if file.size < getattr(settings, 'MAX_EMAIL_ATTACHMENT_SIZE', 512000): # Only files smaller than 512kb (or as defined in # settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email. - files.append(a.file.path) + files.append([a.filename, a.file]) context = safe_template_context(t) diff --git a/helpdesk/lib.py b/helpdesk/lib.py index 584f2c60..5d90c8cb 100644 --- a/helpdesk/lib.py +++ b/helpdesk/lib.py @@ -45,8 +45,8 @@ def send_templated_mail(template_name, email_context, recipients, sender=None, b fail_silently is passed to Django's mail routine. Set to 'True' to ignore any errors at send time. - files can be a list of file paths to be attached, or it can be left blank. - eg ('/tmp/file1.txt', '/tmp/image.png') + files can be a list of tuple. Each tuple should be a filename to attach, + along with the File objects to be read. files can be blank. """ from django.conf import settings @@ -123,11 +123,11 @@ def send_templated_mail(template_name, email_context, recipients, sender=None, b msg.attach_alternative(html_part, "text/html") if files: - if type(files) != list: - files = [files,] - - for file in files: - msg.attach_file(file) + for attachment in files: + file_to_attach = attachment[1] + file_to_attach.open() + msg.attach(filename=attachment[0], content=file_to_attach.read()) + file_to_attach.close() return msg.send(fail_silently) diff --git a/helpdesk/views/staff.py b/helpdesk/views/staff.py index a99f905a..73f88283 100644 --- a/helpdesk/views/staff.py +++ b/helpdesk/views/staff.py @@ -410,7 +410,7 @@ def update_ticket(request, ticket_id, public=False): if file.size < getattr(settings, 'MAX_EMAIL_ATTACHMENT_SIZE', 512000): # Only files smaller than 512kb (or as defined in # settings.MAX_EMAIL_ATTACHMENT_SIZE) are sent via email. - files.append(a.file.path) + files.append([a.filename, a.file]) if title != ticket.title: