Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions django_mailbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Copy link
Collaborator

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.

attachment.save()

placeholder = EmailMessage()
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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())
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_ prefix is incoherent for other properties in model. I suggest remove get_.

return self.get_email_object()["Cc"]
Copy link
Collaborator

Choose a reason for hiding this comment

The 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=''):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest move this filtering as QuerySet method. I think it's good to add support for prefetch filtered attachments in some way too.

for img in soup.findAll('img'):
if img['src'] == attachment.cid:
img['src'] = '%s%s' % (django_settings.MEDIA_URL, attachment.document)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not attachment.document.url? See Using files in models.

return str(soup)

def _rehydrate(self, msg):
new = EmailMessage()
settings = utils.get_settings()
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions rtd_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
django>=1.9,<1.10
six
beautifulsoup4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rtd_requirements.txt consist requirements for build docs on http://django-mailbox.readthedocs.io/. If you like add new requirements to install use install_requires in /setup.py instead.