1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-09 22:38:19 +02:00

adds testable repo with added features

This commit is contained in:
Joe 2021-02-03 23:05:48 -05:00
parent 62805f324e
commit c7997ebd9b
58 changed files with 2421 additions and 1541 deletions

View file

@ -11,127 +11,112 @@ logger = logging.getLogger(__name__)
def get_settings():
return {
'strip_unallowed_mimetypes': getattr(
settings,
'DJANGO_MAILBOX_STRIP_UNALLOWED_MIMETYPES',
False
"strip_unallowed_mimetypes": getattr(
settings, "DJANGO_MAILBOX_STRIP_UNALLOWED_MIMETYPES", False
),
'allowed_mimetypes': getattr(
settings,
'DJANGO_MAILBOX_ALLOWED_MIMETYPES',
[
'text/plain',
'text/html'
]
"allowed_mimetypes": getattr(
settings, "DJANGO_MAILBOX_ALLOWED_MIMETYPES", ["text/plain", "text/html"]
),
'text_stored_mimetypes': getattr(
"text_stored_mimetypes": getattr(
settings,
'DJANGO_MAILBOX_TEXT_STORED_MIMETYPES',
[
'text/plain',
'text/html'
]
"DJANGO_MAILBOX_TEXT_STORED_MIMETYPES",
["text/plain", "text/html"],
),
'altered_message_header': getattr(
"altered_message_header": getattr(
settings,
'DJANGO_MAILBOX_ALTERED_MESSAGE_HEADER',
'X-Django-Mailbox-Altered-Message'
"DJANGO_MAILBOX_ALTERED_MESSAGE_HEADER",
"X-Django-Mailbox-Altered-Message",
),
'attachment_interpolation_header': getattr(
"attachment_interpolation_header": getattr(
settings,
'DJANGO_MAILBOX_ATTACHMENT_INTERPOLATION_HEADER',
'X-Django-Mailbox-Interpolate-Attachment'
"DJANGO_MAILBOX_ATTACHMENT_INTERPOLATION_HEADER",
"X-Django-Mailbox-Interpolate-Attachment",
),
'attachment_upload_to': getattr(
"attachment_upload_to": getattr(
settings,
'DJANGO_MAILBOX_ATTACHMENT_UPLOAD_TO',
'mailbox_attachments/%Y/%m/%d/'
"DJANGO_MAILBOX_ATTACHMENT_UPLOAD_TO",
"mailbox_attachments/%Y/%m/%d/",
),
'store_original_message': getattr(
settings,
'DJANGO_MAILBOX_STORE_ORIGINAL_MESSAGE',
False
"store_original_message": getattr(
settings, "DJANGO_MAILBOX_STORE_ORIGINAL_MESSAGE", False
),
'compress_original_message': getattr(
settings,
'DJANGO_MAILBOX_COMPRESS_ORIGINAL_MESSAGE',
False
"compress_original_message": getattr(
settings, "DJANGO_MAILBOX_COMPRESS_ORIGINAL_MESSAGE", False
),
'original_message_compression': getattr(
settings,
'DJANGO_MAILBOX_ORIGINAL_MESSAGE_COMPRESSION',
6
"original_message_compression": getattr(
settings, "DJANGO_MAILBOX_ORIGINAL_MESSAGE_COMPRESSION", 6
),
'default_charset': getattr(
"default_charset": getattr(
settings,
'DJANGO_MAILBOX_default_charset',
'iso8859-1',
)
"DJANGO_MAILBOX_default_charset",
"iso8859-1",
),
}
def convert_header_to_unicode(header):
default_charset = get_settings()['default_charset']
default_charset = get_settings()["default_charset"]
def _decode(value, encoding):
if isinstance(value, str):
return value
if not encoding or encoding == 'unknown-8bit':
if not encoding or encoding == "unknown-8bit":
encoding = default_charset
return value.decode(encoding, 'replace')
return value.decode(encoding, "replace")
try:
return ''.join(
return "".join(
[
(
_decode(bytestr, encoding)
) for bytestr, encoding in email.header.decode_header(header)
(_decode(bytestr, encoding))
for bytestr, encoding in email.header.decode_header(header)
]
)
except UnicodeDecodeError:
logger.exception(
'Errors encountered decoding header %s into encoding %s.',
"Errors encountered decoding header %s into encoding %s.",
header,
default_charset,
)
return header.decode(default_charset, 'replace')
return header.decode(default_charset, "replace")
def get_body_from_message(message, maintype, subtype):
"""
Fetchs the body message matching main/sub content type.
"""
body = ''
body = ""
for part in message.walk():
if part.get('content-disposition', '').startswith('attachment;'):
if part.get("content-disposition", "").startswith("attachment;"):
continue
if part.get_content_maintype() == maintype and \
part.get_content_subtype() == subtype:
if (
part.get_content_maintype() == maintype
and part.get_content_subtype() == subtype
):
charset = part.get_content_charset()
this_part = part.get_payload(decode=True)
if charset:
try:
this_part = this_part.decode(charset, 'replace')
this_part = this_part.decode(charset, "replace")
except LookupError:
this_part = this_part.decode('ascii', 'replace')
this_part = this_part.decode("ascii", "replace")
logger.warning(
'Unknown encoding %s encountered while decoding '
'text payload. Interpreting as ASCII with '
'replacement, but some data may not be '
'represented as the sender intended.',
charset
"Unknown encoding %s encountered while decoding "
"text payload. Interpreting as ASCII with "
"replacement, but some data may not be "
"represented as the sender intended.",
charset,
)
except ValueError:
this_part = this_part.decode('ascii', 'replace')
this_part = this_part.decode("ascii", "replace")
logger.warning(
'Error encountered while decoding text '
'payload from an incorrectly-constructed '
'e-mail; payload was converted to ASCII with '
'replacement, but some data may not be '
'represented as the sender intended.'
"Error encountered while decoding text "
"payload from an incorrectly-constructed "
"e-mail; payload was converted to ASCII with "
"replacement, but some data may not be "
"represented as the sender intended."
)
else:
this_part = this_part.decode('ascii', 'replace')
this_part = this_part.decode("ascii", "replace")
body += this_part
@ -141,8 +126,8 @@ def get_body_from_message(message, maintype, subtype):
def get_attachment_save_path(instance, filename):
settings = get_settings()
path = settings['attachment_upload_to']
if '%' in path:
path = settings["attachment_upload_to"]
if "%" in path:
path = datetime.datetime.utcnow().strftime(path)
return os.path.join(