mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
Adding docstrings to models.
This commit is contained in:
parent
50282414aa
commit
3d5b816150
1 changed files with 62 additions and 1 deletions
|
|
@ -156,22 +156,27 @@ class Mailbox(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def port(self):
|
def port(self):
|
||||||
|
"""Returns the port to use for fetching messages."""
|
||||||
return self._protocol_info.port
|
return self._protocol_info.port
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def username(self):
|
def username(self):
|
||||||
|
"""Returns the username to use for fetching messages."""
|
||||||
return unquote(self._protocol_info.username)
|
return unquote(self._protocol_info.username)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def password(self):
|
def password(self):
|
||||||
|
"""Returns the password to use for fetching messages."""
|
||||||
return unquote(self._protocol_info.password)
|
return unquote(self._protocol_info.password)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def location(self):
|
def location(self):
|
||||||
|
"""Returns the location (domain and path) of messages."""
|
||||||
return self._domain if self._domain else '' + self._protocol_info.path
|
return self._domain if self._domain else '' + self._protocol_info.path
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def type(self):
|
def type(self):
|
||||||
|
"""Returns the 'transport' name for this mailbox."""
|
||||||
scheme = self._protocol_info.scheme.lower()
|
scheme = self._protocol_info.scheme.lower()
|
||||||
if '+' in scheme:
|
if '+' in scheme:
|
||||||
return scheme.split('+')[0]
|
return scheme.split('+')[0]
|
||||||
|
|
@ -179,10 +184,12 @@ class Mailbox(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def use_ssl(self):
|
def use_ssl(self):
|
||||||
|
"""Returns whether or not this mailbox's connection uses SSL."""
|
||||||
return '+ssl' in self._protocol_info.scheme.lower()
|
return '+ssl' in self._protocol_info.scheme.lower()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def archive(self):
|
def archive(self):
|
||||||
|
"""Returns (if specified) the folder to archive messages to."""
|
||||||
archive_folder = self._query_string.get('archive', None)
|
archive_folder = self._query_string.get('archive', None)
|
||||||
if not archive_folder:
|
if not archive_folder:
|
||||||
return None
|
return None
|
||||||
|
|
@ -190,12 +197,19 @@ class Mailbox(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def folder(self):
|
def folder(self):
|
||||||
|
"""Returns (if specified) the folder to fetch mail from."""
|
||||||
folder = self._query_string.get('folder', None)
|
folder = self._query_string.get('folder', None)
|
||||||
if not folder:
|
if not folder:
|
||||||
return None
|
return None
|
||||||
return folder[0]
|
return folder[0]
|
||||||
|
|
||||||
def get_connection(self):
|
def get_connection(self):
|
||||||
|
"""Returns the transport instance for this mailbox.
|
||||||
|
|
||||||
|
These will always be instances of
|
||||||
|
`django_mailbox.transports.base.EmailTransport`.
|
||||||
|
|
||||||
|
"""
|
||||||
if not self.uri:
|
if not self.uri:
|
||||||
return None
|
return None
|
||||||
elif self.type == 'imap':
|
elif self.type == 'imap':
|
||||||
|
|
@ -235,6 +249,7 @@ class Mailbox(models.Model):
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
def process_incoming_message(self, message):
|
def process_incoming_message(self, message):
|
||||||
|
"""Process a message incoming to this mailbox."""
|
||||||
msg = self._process_message(message)
|
msg = self._process_message(message)
|
||||||
msg.outgoing = False
|
msg.outgoing = False
|
||||||
msg.save()
|
msg.save()
|
||||||
|
|
@ -247,6 +262,7 @@ class Mailbox(models.Model):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def record_outgoing_message(self, message):
|
def record_outgoing_message(self, message):
|
||||||
|
"""Record an outgoing message associated with this mailbox."""
|
||||||
msg = self._process_message(message)
|
msg = self._process_message(message)
|
||||||
msg.outgoing = True
|
msg.outgoing = True
|
||||||
msg.save()
|
msg.save()
|
||||||
|
|
@ -366,6 +382,7 @@ class Mailbox(models.Model):
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def get_new_mail(self):
|
def get_new_mail(self):
|
||||||
|
"""Connect to this transport and fetch new messages."""
|
||||||
new_mail = []
|
new_mail = []
|
||||||
connection = self.get_connection()
|
connection = self.get_connection()
|
||||||
if not connection:
|
if not connection:
|
||||||
|
|
@ -493,6 +510,16 @@ class Message(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def from_address(self):
|
def from_address(self):
|
||||||
|
"""Returns the address (as a list) from which this message was received
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
This was once (and probably should be) a string rather than a list,
|
||||||
|
but in a pull request received long, long ago it was changed;
|
||||||
|
presumably to make the interface identical to that of
|
||||||
|
`to_addresses`.
|
||||||
|
|
||||||
|
"""
|
||||||
if self.from_header:
|
if self.from_header:
|
||||||
return [parseaddr(self.from_header)[1].lower()]
|
return [parseaddr(self.from_header)[1].lower()]
|
||||||
else:
|
else:
|
||||||
|
|
@ -500,6 +527,7 @@ class Message(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def to_addresses(self):
|
def to_addresses(self):
|
||||||
|
"""Returns a list of addresses to which this message was sent."""
|
||||||
addresses = []
|
addresses = []
|
||||||
for address in self.to_header.split(','):
|
for address in self.to_header.split(','):
|
||||||
if address:
|
if address:
|
||||||
|
|
@ -608,18 +636,48 @@ class Message(models.Model):
|
||||||
return new
|
return new
|
||||||
|
|
||||||
def get_body(self):
|
def get_body(self):
|
||||||
|
"""Returns the `body` field of this record.
|
||||||
|
|
||||||
|
This will automatically base64-decode the message contents
|
||||||
|
if they are encoded as such.
|
||||||
|
|
||||||
|
"""
|
||||||
if self.encoded:
|
if self.encoded:
|
||||||
return base64.b64decode(self.body.encode('ascii'))
|
return base64.b64decode(self.body.encode('ascii'))
|
||||||
return self.body.encode('utf-8')
|
return self.body.encode('utf-8')
|
||||||
|
|
||||||
def set_body(self, body):
|
def set_body(self, body):
|
||||||
|
"""Set the `body` field of this record.
|
||||||
|
|
||||||
|
This will automatically base64-encode the message contents to
|
||||||
|
circumvent a limitation in earlier versions of Django in which
|
||||||
|
no fields existed for storing arbitrary bytes.
|
||||||
|
|
||||||
|
"""
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
body = body.encode('utf-8')
|
body = body.encode('utf-8')
|
||||||
self.encoded = True
|
self.encoded = True
|
||||||
self.body = base64.b64encode(body).decode('ascii')
|
self.body = base64.b64encode(body).decode('ascii')
|
||||||
|
|
||||||
def get_email_object(self):
|
def get_email_object(self):
|
||||||
""" Returns an `email.message.Message` instance for this message."""
|
"""Returns an `email.message.Message` instance representing the
|
||||||
|
contents of this message and all attachments.
|
||||||
|
|
||||||
|
See [email.Message.Message]_ for more information as to what methods
|
||||||
|
and properties are available on `email.message.Message` instances.
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
|
||||||
|
Depending upon the storage methods in use (specifically --
|
||||||
|
whether ``DJANGO_MAILBOX_STORE_ORIGINAL_MESSAGE`` is set
|
||||||
|
to ``True``, this may either create a "rehydrated" message
|
||||||
|
using stored attachments, or read the message contents stored
|
||||||
|
on-disk.
|
||||||
|
|
||||||
|
.. [email.Message.Message]: Python's `email.message.Message` docs
|
||||||
|
(https://docs.python.org/2/library/email.message.html)
|
||||||
|
|
||||||
|
"""
|
||||||
if self.eml:
|
if self.eml:
|
||||||
self.eml.open()
|
self.eml.open()
|
||||||
body = self.eml.file.read()
|
body = self.eml.file.read()
|
||||||
|
|
@ -632,6 +690,7 @@ class Message(models.Model):
|
||||||
return self._rehydrate(flat)
|
return self._rehydrate(flat)
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
|
"""Delete this message and all stored attachments."""
|
||||||
for attachment in self.attachments.all():
|
for attachment in self.attachments.all():
|
||||||
# This attachment is attached only to this message.
|
# This attachment is attached only to this message.
|
||||||
attachment.delete()
|
attachment.delete()
|
||||||
|
|
@ -662,6 +721,7 @@ class MessageAttachment(models.Model):
|
||||||
)
|
)
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
|
"""Deletes the attachment."""
|
||||||
self.document.delete()
|
self.document.delete()
|
||||||
return super(MessageAttachment, self).delete(*args, **kwargs)
|
return super(MessageAttachment, self).delete(*args, **kwargs)
|
||||||
|
|
||||||
|
|
@ -687,6 +747,7 @@ class MessageAttachment(models.Model):
|
||||||
self._set_dehydrated_headers(rehydrated)
|
self._set_dehydrated_headers(rehydrated)
|
||||||
|
|
||||||
def get_filename(self):
|
def get_filename(self):
|
||||||
|
"""Returns the original filename of this attachment."""
|
||||||
file_name = self._get_rehydrated_headers().get_filename()
|
file_name = self._get_rehydrated_headers().get_filename()
|
||||||
if isinstance(file_name, six.text_type):
|
if isinstance(file_name, six.text_type):
|
||||||
return file_name
|
return file_name
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue