-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added new fields to Message and MessageAttachment #127
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,20 @@ | |
|
||
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 | ||
import mimetypes | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not know if it's a good place for signature by. It can confuse. Commits are marked with authorship. |
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return self.get_email_object()["Cc"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be handled quietly with the lack of such information. |
||
|
||
@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=''): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest move this filtering as |
||
for img in soup.findAll('img'): | ||
if img['src'] == attachment.cid: | ||
img['src'] = '%s%s' % (django_settings.MEDIA_URL, attachment.document) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
||
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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
django>=1.9,<1.10 | ||
six | ||
beautifulsoup4 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raises exceptions of undefinied variable if
bool(msg.get('Content-ID')) == False
.