1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-10 06:48:19 +02:00

adds testable repo with added features

This commit is contained in:
Joe 2021-02-03 23:05:48 -05:00
parent 62805f324e
commit c7997ebd9b
58 changed files with 2421 additions and 1541 deletions

View file

@ -13,18 +13,11 @@ class Command(BaseCommand):
def handle(self, *args, **options):
mailboxes = Mailbox.active_mailboxes.all()
if args:
mailboxes = mailboxes.filter(
name=' '.join(args)
)
mailboxes = mailboxes.filter(name=" ".join(args))
for mailbox in mailboxes:
logger.info(
'Gathering messages for %s',
mailbox.name
)
logger.info("Gathering messages for %s", mailbox.name)
messages = mailbox.get_new_mail()
for message in messages:
logger.info(
'Received %s (from %s)',
message.subject,
message.from_address
"Received %s (from %s)", message.subject, message.from_address
)

View file

@ -1,6 +1,7 @@
import email
import logging
import sys
try:
from email import utils
except ImportError:
@ -21,9 +22,9 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
'mailbox_name',
nargs='?',
help="The name of the mailbox that will receive the message"
"mailbox_name",
nargs="?",
help="The name of the mailbox that will receive the message",
)
def handle(self, mailbox_name=None, *args, **options):
@ -34,10 +35,7 @@ class Command(BaseCommand):
else:
mailbox = self.get_mailbox_for_message(message)
mailbox.process_incoming_message(message)
logger.info(
"Message received from %s",
message['from']
)
logger.info("Message received from %s", message["from"])
else:
logger.warning("Message not processable.")
@ -48,5 +46,5 @@ class Command(BaseCommand):
return mailbox
def get_mailbox_for_message(self, message):
email_address = utils.parseaddr(message['to'])[1][0:255]
email_address = utils.parseaddr(message["to"])[1][0:255]
return self.get_mailbox_by_name(email_address)

View file

@ -11,7 +11,7 @@ logging.basicConfig(level=logging.INFO)
class Command(BaseCommand):
""" Briefly, a bug existed in a migration that may have caused message
"""Briefly, a bug existed in a migration that may have caused message
attachments to become disassociated with their messages. This management
command will read through existing message attachments and attempt to
re-associate them with their original message.
@ -22,14 +22,13 @@ class Command(BaseCommand):
have been quite unlucky to have ran the bad migration).
"""
def handle(self, *args, **options):
attachment_hash_map = {}
attachments_without_messages = MessageAttachment.objects.filter(
message=None
).order_by(
'id'
)
).order_by("id")
if attachments_without_messages.count() < 1:
return
@ -40,13 +39,13 @@ class Command(BaseCommand):
md5.update(chunk)
attachment_hash_map[md5.hexdigest()] = attachment.pk
for message_record in Message.objects.all().order_by('id'):
for message_record in Message.objects.all().order_by("id"):
message = email.message_from_string(message_record.body)
if message.is_multipart():
for part in message.walk():
if part.get_content_maintype() == 'multipart':
if part.get_content_maintype() == "multipart":
continue
if part.get('Content-Disposition') is None:
if part.get("Content-Disposition") is None:
continue
md5 = hashlib.md5()
md5.update(part.get_payload(decode=True))
@ -61,11 +60,11 @@ class Command(BaseCommand):
"Associated message %s with attachment %s (%s)",
message_record.pk,
attachment.pk,
digest
digest,
)
else:
logger.info(
"%s(%s) not found in currently-stored attachments",
part.get_filename(),
digest
digest,
)