mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
PEP-8 Cleanup.
This commit is contained in:
parent
9f77c9221a
commit
9cdb2d9d1b
10 changed files with 120 additions and 96 deletions
|
|
@ -12,13 +12,13 @@ from django.core.files import File
|
|||
from django.core.files.temp import NamedTemporaryFile
|
||||
from django.db import models
|
||||
from django_mailbox.transports import Pop3Transport, ImapTransport,\
|
||||
MaildirTransport, MboxTransport, BabylTransport, MHTransport, \
|
||||
MMDFTransport
|
||||
MaildirTransport, MboxTransport, BabylTransport, MHTransport, \
|
||||
MMDFTransport
|
||||
from django_mailbox.signals import message_received
|
||||
|
||||
SKIPPED_EXTENSIONS = getattr(
|
||||
settings,
|
||||
'DJANGO_MAILBOX_SKIPPED_EXTENSIONS',
|
||||
settings,
|
||||
'DJANGO_MAILBOX_SKIPPED_EXTENSIONS',
|
||||
['.p7s']
|
||||
)
|
||||
STRIP_UNALLOWED_MIMETYPES = getattr(
|
||||
|
|
@ -40,59 +40,58 @@ ALTERED_MESSAGE_HEADER = getattr(
|
|||
'X-Django-Mailbox-Altered-Message'
|
||||
)
|
||||
|
||||
|
||||
class ActiveMailboxManager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return super(ActiveMailboxManager, self).get_query_set().filter(
|
||||
active=True,
|
||||
)
|
||||
|
||||
|
||||
class Mailbox(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
uri = models.CharField(
|
||||
max_length=255,
|
||||
help_text="""
|
||||
Example: imap+ssl://myusername:mypassword@someserver
|
||||
<br />
|
||||
<br />
|
||||
Internet transports include 'imap' and 'pop3'; common local file transports include 'maildir', 'mbox', and less commonly 'babyl', 'mh', and 'mmdf'.
|
||||
<br />
|
||||
<br />
|
||||
Be sure to urlencode your username and password should they
|
||||
contain illegal characters (like @, :, etc).
|
||||
""",
|
||||
blank=True,
|
||||
null=True,
|
||||
default=None,
|
||||
)
|
||||
max_length=255,
|
||||
help_text=(
|
||||
"Example: imap+ssl://myusername:mypassword@someserver <br />"
|
||||
"<br />"
|
||||
"Internet transports include 'imap' and 'pop3'; "
|
||||
"common local file transports include 'maildir', 'mbox', "
|
||||
"and less commonly 'babyl', 'mh', and 'mmdf'. <br />"
|
||||
"<br />"
|
||||
"Be sure to urlencode your username and password should they "
|
||||
"contain illegal characters (like @, :, etc)."
|
||||
),
|
||||
blank=True,
|
||||
null=True,
|
||||
default=None,
|
||||
)
|
||||
from_email = models.CharField(
|
||||
max_length=255,
|
||||
help_text="""
|
||||
Example: MailBot <mailbot@yourdomain.com>
|
||||
<br />
|
||||
'From' header to set for outgoing email.
|
||||
<br />
|
||||
<br />
|
||||
If you do not use this e-mail inbox for outgoing mail, this
|
||||
setting is unnecessary.
|
||||
<br />
|
||||
If you send e-mail without setting this, your 'From' header will
|
||||
be set to match the setting `DEFAULT_FROM_EMAIL`.
|
||||
""",
|
||||
blank=True,
|
||||
null=True,
|
||||
default=None,
|
||||
)
|
||||
max_length=255,
|
||||
help_text=(
|
||||
"Example: MailBot <mailbot@yourdomain.com><br />"
|
||||
"'From' header to set for outgoing email.<br />"
|
||||
"<br />"
|
||||
"If you do not use this e-mail inbox for outgoing mail, this "
|
||||
"setting is unnecessary.<br />"
|
||||
"If you send e-mail without setting this, your 'From' header will'"
|
||||
"be set to match the setting `DEFAULT_FROM_EMAIL`."
|
||||
),
|
||||
blank=True,
|
||||
null=True,
|
||||
default=None,
|
||||
)
|
||||
active = models.BooleanField(
|
||||
help_text="""
|
||||
Check this e-mail inbox for new e-mail messages during polling
|
||||
cycles. This checkbox does not have an effect upon whether
|
||||
mail is collected here when this mailbox receives mail from a
|
||||
pipe, and does not affect whether e-mail messages can be
|
||||
dispatched from this mailbox.
|
||||
""",
|
||||
blank=True,
|
||||
default=True,
|
||||
)
|
||||
help_text=(
|
||||
"Check this e-mail inbox for new e-mail messages during polling "
|
||||
"cycles. This checkbox does not have an effect upon whether "
|
||||
"mail is collected here when this mailbox receives mail from a "
|
||||
"pipe, and does not affect whether e-mail messages can be "
|
||||
"dispatched from this mailbox. "
|
||||
),
|
||||
blank=True,
|
||||
default=True,
|
||||
)
|
||||
|
||||
objects = models.Manager()
|
||||
active_mailboxes = ActiveMailboxManager()
|
||||
|
|
@ -137,17 +136,17 @@ class Mailbox(models.Model):
|
|||
return None
|
||||
elif self.type == 'imap':
|
||||
conn = ImapTransport(
|
||||
self.location,
|
||||
port=self.port if self.port else None,
|
||||
ssl=self.use_ssl
|
||||
)
|
||||
self.location,
|
||||
port=self.port if self.port else None,
|
||||
ssl=self.use_ssl
|
||||
)
|
||||
conn.connect(self.username, self.password)
|
||||
elif self.type == 'pop3':
|
||||
conn = Pop3Transport(
|
||||
self.location,
|
||||
port=self.port if self.port else None,
|
||||
ssl=self.use_ssl
|
||||
)
|
||||
self.location,
|
||||
port=self.port if self.port else None,
|
||||
ssl=self.use_ssl
|
||||
)
|
||||
conn.connect(self.username, self.password)
|
||||
elif self.type == 'maildir':
|
||||
conn = MaildirTransport(self.location)
|
||||
|
|
@ -207,7 +206,9 @@ class Mailbox(models.Model):
|
|||
msg.body = message.as_string()
|
||||
if message['in-reply-to']:
|
||||
try:
|
||||
msg.in_reply_to = Message.objects.filter(message_id=message['in-reply-to'])[0]
|
||||
msg.in_reply_to = Message.objects.filter(
|
||||
message_id=message['in-reply-to']
|
||||
)[0]
|
||||
except IndexError:
|
||||
pass
|
||||
msg.save()
|
||||
|
|
@ -219,13 +220,17 @@ class Mailbox(models.Model):
|
|||
continue
|
||||
filename = part.get_filename()
|
||||
# ignore SMIME extension
|
||||
filename_basename, filename_extension = os.path.splitext(filename)
|
||||
filename_basename, filename_extension = os.path.splitext(
|
||||
filename
|
||||
)
|
||||
buffer_space = 40
|
||||
if len(filename) > 100 - buffer_space:
|
||||
# Ensure that there're at least a few chars available afterward
|
||||
# for duplication things like _1, _2 ... _99 and the FileField's
|
||||
# upload_to path.
|
||||
filename_basename = filename_basename[0:100-len(filename_extension)-buffer_space]
|
||||
# Ensure that there're at least a few chars available
|
||||
# afterward for duplication things like _1, _2 ... _99 and
|
||||
# the FileField's upload_to path.
|
||||
filename_basename = filename_basename[
|
||||
0:100-len(filename_extension)-buffer_space
|
||||
]
|
||||
filename = filename_basename + filename_extension
|
||||
if filename_extension in SKIPPED_EXTENSIONS:
|
||||
continue
|
||||
|
|
@ -257,30 +262,34 @@ class Mailbox(models.Model):
|
|||
class Meta:
|
||||
verbose_name_plural = "Mailboxes"
|
||||
|
||||
|
||||
class IncomingMessageManager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return super(IncomingMessageManager, self).get_query_set().filter(
|
||||
outgoing=False,
|
||||
)
|
||||
|
||||
|
||||
class OutgoingMessageManager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return super(OutgoingMessageManager, self).get_query_set().filter(
|
||||
outgoing=True,
|
||||
)
|
||||
|
||||
|
||||
class UnreadMessageManager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return super(UnreadMessageManager, self).get_query_set().filter(
|
||||
read=None
|
||||
)
|
||||
|
||||
|
||||
class Message(models.Model):
|
||||
mailbox = models.ForeignKey(Mailbox, related_name='messages')
|
||||
subject = models.CharField(max_length=255)
|
||||
message_id = models.CharField(max_length=255)
|
||||
in_reply_to = models.ForeignKey(
|
||||
'django_mailbox.Message',
|
||||
'django_mailbox.Message',
|
||||
related_name='replies',
|
||||
blank=True,
|
||||
null=True,
|
||||
|
|
@ -313,13 +322,13 @@ class Message(models.Model):
|
|||
@property
|
||||
def address(self):
|
||||
"""Property allowing one to get the relevant address(es).
|
||||
|
||||
|
||||
In earlier versions of this library, the model had an `address` field
|
||||
storing the e-mail address from which a message was received. During
|
||||
later refactorings, it became clear that perhaps storing sent messages
|
||||
would also be useful, so the address field was replaced with two
|
||||
separate fields.
|
||||
|
||||
|
||||
"""
|
||||
if self.outgoing:
|
||||
return self.to_addresses()
|
||||
|
|
@ -335,15 +344,15 @@ class Message(models.Model):
|
|||
addresses = []
|
||||
for address in self.to_header.split(','):
|
||||
addresses.append(
|
||||
rfc822.parseaddr(
|
||||
address
|
||||
)[1]
|
||||
)
|
||||
rfc822.parseaddr(
|
||||
address
|
||||
)[1]
|
||||
)
|
||||
return addresses
|
||||
|
||||
def reply(self, message):
|
||||
"""Sends a message as a reply to this message instance.
|
||||
|
||||
|
||||
Although Django's e-mail processing will set both Message-ID
|
||||
and Date upon generating the e-mail message, we will not be able
|
||||
to retrieve that information through normal channels, so we must
|
||||
|
|
@ -359,10 +368,10 @@ class Message(models.Model):
|
|||
message.extra_headers['In-Reply-To'] = self.message_id
|
||||
message.send()
|
||||
return self.mailbox.record_outgoing_message(
|
||||
email.message_from_string(
|
||||
message.message().as_string()
|
||||
)
|
||||
email.message_from_string(
|
||||
message.message().as_string()
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
|
|
@ -373,7 +382,8 @@ class Message(models.Model):
|
|||
body = ''
|
||||
if message.is_multipart():
|
||||
for part in message.get_payload():
|
||||
if part.get('content-type', '').split(';')[0] == 'text/plain':
|
||||
mime_type = part.get('content-type', '').split(';')[0]
|
||||
if mime_type == 'text/plain':
|
||||
body = body + get_body_from_message(part)
|
||||
else:
|
||||
body = body + message.get_payload()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue