1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-09 22:38:19 +02:00
django-mailbox/django_mailbox/management/commands/getmail.py

44 lines
1.2 KiB
Python
Raw Normal View History

import logging
2012-06-27 20:45:01 -07:00
from django.core.management.base import BaseCommand
from django_mailbox.models import Mailbox
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
2012-06-27 20:45:01 -07:00
class Command(BaseCommand):
2019-05-09 12:25:19 +02:00
args = ""
help = ""
def add_arguments(self, parser):
2019-05-09 12:41:53 +02:00
parser.add_argument('-nl', '--nologs', action='store_true', default=False, dest='nologs',
help='Disable logging (default False)')
2019-05-09 12:25:19 +02:00
2012-06-27 20:45:01 -07:00
def handle(self, *args, **options):
mailboxes = Mailbox.active_mailboxes.all()
2019-05-09 12:41:53 +02:00
logging = True
2019-05-09 12:44:41 +02:00
if options['nologs']:
2019-05-09 12:41:53 +02:00
logging = False
2012-06-27 20:45:01 -07:00
if args:
mailboxes = mailboxes.filter(
name=' '.join(args)
)
2012-06-27 20:45:01 -07:00
for mailbox in mailboxes:
2019-05-09 12:43:32 +02:00
if logging:
logger.info(
2019-05-09 12:25:19 +02:00
'Gathering messages for %s',
mailbox.name
)
2019-05-09 12:25:19 +02:00
messages = mailbox.get_new_mail()
for message in messages:
2019-05-09 12:41:53 +02:00
if logging:
2019-05-09 12:25:19 +02:00
logger.info(
'Received %s (from %s)',
message.subject,
message.from_address
)