Add Attachment.content_type

This commit is contained in:
medmunds
2020-09-10 15:08:52 -07:00
committed by Mike Edmunds
parent 5b04ae30f2
commit be7f2516b0
2 changed files with 10 additions and 0 deletions

View File

@@ -285,6 +285,7 @@ class Attachment:
if self.content is None: if self.content is None:
self.content = attachment.as_bytes() self.content = attachment.as_bytes()
self.mimetype = attachment.get_content_type() self.mimetype = attachment.get_content_type()
self.content_type = attachment["Content-Type"] # includes charset if provided
content_disposition = attachment.get_content_disposition() content_disposition = attachment.get_content_disposition()
if content_disposition == 'inline' or (not content_disposition and 'Content-ID' in attachment): if content_disposition == 'inline' or (not content_disposition and 'Content-ID' in attachment):
@@ -294,6 +295,7 @@ class Attachment:
self.cid = unquote(self.content_id) # without the <, > self.cid = unquote(self.content_id) # without the <, >
else: else:
(self.name, self.content, self.mimetype) = attachment (self.name, self.content, self.mimetype) = attachment
self.content_type = self.mimetype
self.name = force_non_lazy(self.name) self.name = force_non_lazy(self.name)
self.content = force_non_lazy(self.content) self.content = force_non_lazy(self.content)
@@ -304,6 +306,8 @@ class Attachment:
self.mimetype, _ = mimetypes.guess_type(self.name) self.mimetype, _ = mimetypes.guess_type(self.name)
if self.mimetype is None: if self.mimetype is None:
self.mimetype = DEFAULT_ATTACHMENT_MIME_TYPE self.mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
if self.content_type is None:
self.content_type = self.mimetype
@property @property
def b64content(self): def b64content(self):

View File

@@ -4,6 +4,7 @@ import base64
import copy import copy
import pickle import pickle
from email.mime.image import MIMEImage from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from django.http import QueryDict from django.http import QueryDict
from django.test import SimpleTestCase, RequestFactory, override_settings from django.test import SimpleTestCase, RequestFactory, override_settings
@@ -217,6 +218,11 @@ class NormalizedAttachmentTests(SimpleTestCase):
self.assertFalse(att.inline) self.assertFalse(att.inline)
self.assertIsNone(att.content_id) # ignored for non-inline Attachment self.assertIsNone(att.content_id) # ignored for non-inline Attachment
def test_content_type(self):
att = Attachment(MIMEText("text", "plain", "iso8859-1"), "ascii")
self.assertEqual(att.mimetype, "text/plain")
self.assertEqual(att.content_type, 'text/plain; charset="iso8859-1"')
class LazyCoercionTests(SimpleTestCase): class LazyCoercionTests(SimpleTestCase):
"""Test utils.is_lazy and force_non_lazy*""" """Test utils.is_lazy and force_non_lazy*"""