From 462fdd3e496e28cc7c9d99a4f18ce80cb8b6aba6 Mon Sep 17 00:00:00 2001 From: Jace Manshadi Date: Tue, 24 Jan 2023 06:08:47 -0800 Subject: [PATCH] updating location of logic for getmail command (#264) * moving logic for processing all the new emails from all mailboxes to a method under Mailbox so it can be called from other modules * moving logger lines into handle method as requested --- django_mailbox/management/commands/getmail.py | 23 ++----------------- django_mailbox/models.py | 21 +++++++++++++++++ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/django_mailbox/management/commands/getmail.py b/django_mailbox/management/commands/getmail.py index 31b2dbd..f657509 100644 --- a/django_mailbox/management/commands/getmail.py +++ b/django_mailbox/management/commands/getmail.py @@ -5,26 +5,7 @@ from django.core.management.base import BaseCommand from django_mailbox.models import Mailbox -logger = logging.getLogger(__name__) -logging.basicConfig(level=logging.INFO) - - class Command(BaseCommand): def handle(self, *args, **options): - mailboxes = Mailbox.active_mailboxes.all() - if args: - mailboxes = mailboxes.filter( - name=' '.join(args) - ) - for mailbox in mailboxes: - 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 - ) + logging.basicConfig(level=logging.INFO) + Mailbox.get_new_mail_all_mailboxes(args) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 42296e9..871b0cc 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -462,6 +462,27 @@ class Mailbox(models.Model): else: self.save() + @staticmethod + def get_new_mail_all_mailboxes(args=None): + mailboxes = Mailbox.active_mailboxes.all() + if args: + mailboxes = mailboxes.filter( + name=' '.join(args) + ) + for mailbox in mailboxes: + 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 + ) + + def __str__(self): return self.name