From ee11172fc80fb072d13cf23a4b2dc81155ea1d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeppe=20Ish=C3=83oy?= Date: Thu, 22 Dec 2016 18:47:20 +0100 Subject: [PATCH] Added new fields to Message and MessageAttachment --- django_mailbox/models.py | 61 ++++++++++++++++++++++++++++++++++++++-- rtd_requirements.txt | 1 + 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 89eb0adb..03a03a2b 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -7,8 +7,12 @@ from email.encoders import encode_base64 from email.message import Message as EmailMessage -from email.utils import formatdate, parseaddr +from email.utils import formatdate, parseaddr, parsedate_tz, mktime_tz, parsedate from quopri import encode as encode_quopri +from datetime import datetime +from time import mktime +from bs4 import BeautifulSoup + import base64 import email import logging @@ -16,6 +20,7 @@ import os.path import sys import uuid +import string import six from six.moves.urllib.parse import parse_qs, unquote, urlparse @@ -281,7 +286,8 @@ def _get_dehydrated_message(self, msg, record): _, extension = os.path.splitext(filename) if not extension: extension = '.bin' - + if msg.get('Content-ID'): + cid = 'cid:%s' % msg.get('Content-ID')[1:len(msg.get('Content-ID'))-1] attachment = MessageAttachment() attachment.document.save( @@ -295,6 +301,7 @@ def _get_dehydrated_message(self, msg, record): attachment.message = record for key, value in msg.items(): attachment[key] = value + attachment.cid = cid attachment.save() placeholder = EmailMessage() @@ -361,6 +368,24 @@ def _process_message(self, message): msg.to_header = utils.convert_header_to_unicode( message['Delivered-To'] ) + #rulzart - Save the email date + if 'date' in message: + if django_settings.USE_TZ: + msg.date = datetime.utcfromtimestamp( + mktime_tz( + parsedate_tz( + message['date'] + ) + ) + ) + else: + msg.date = datetime.fromtimestamp( + mktime( + parsedate( + message['date'] + ) + ) + ) msg.save() message = self._get_dehydrated_message(message, msg) msg.set_body(message.as_string()) @@ -486,6 +511,14 @@ class Message(models.Model): upload_to="messages", help_text=_(u'Original full content of message') ) + + #rulzart - Added date field to optimize sorting speed + date = models.DateTimeField( + _(u'Date'), + null=True, + help_text=_(u'The datetime of the email object') + ) + objects = models.Manager() unread_messages = UnreadMessageManager() incoming_messages = IncomingMessageManager() @@ -578,6 +611,23 @@ def html(self): self.get_email_object(), 'text', 'html' ).replace('\n', '').strip() + #rulzart - added a quick way to get Cc addresses + @property + def get_cc(self): + return self.get_email_object()["Cc"] + + @property + def html_with_img(self): + """ + Returns the message body matching content type 'text/html' with correct image src. + """ + soup = BeautifulSoup(self.html) + for attachment in self.attachments.exclude(cid__isnull=True).exclude(cid__exact=''): + for img in soup.findAll('img'): + if img['src'] == attachment.cid: + img['src'] = '%s%s' % (django_settings.MEDIA_URL, attachment.document) + return str(soup) + def _rehydrate(self, msg): new = EmailMessage() settings = utils.get_settings() @@ -720,6 +770,13 @@ class MessageAttachment(models.Model): upload_to=utils.get_attachment_save_path, ) + cid = models.TextField( + _(u'Content-ID'), + null=True, + blank=True, + help_text='Used to help you find inline images if you need to display them' + ) + def delete(self, *args, **kwargs): """Deletes the attachment.""" self.document.delete() diff --git a/rtd_requirements.txt b/rtd_requirements.txt index 18684b76..51182a8b 100644 --- a/rtd_requirements.txt +++ b/rtd_requirements.txt @@ -1,2 +1,3 @@ django>=1.9,<1.10 six +beautifulsoup4 \ No newline at end of file