2014-08-22 20:02:58 -07:00
|
|
|
import logging
|
|
|
|
|
|
2012-06-27 20:45:01 -07:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
|
|
|
|
|
from django_mailbox.models import Mailbox
|
|
|
|
|
|
2014-08-22 20:02:58 -07:00
|
|
|
|
|
|
|
|
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):
|
2012-10-09 05:52:04 +00:00
|
|
|
mailboxes = Mailbox.active_mailboxes.all()
|
2019-05-09 12:41:53 +02:00
|
|
|
logging = True
|
|
|
|
|
if option['nologs']:
|
|
|
|
|
logging = False
|
|
|
|
|
|
2012-06-27 20:45:01 -07:00
|
|
|
if args:
|
2014-08-22 20:02:58 -07:00
|
|
|
mailboxes = mailboxes.filter(
|
|
|
|
|
name=' '.join(args)
|
|
|
|
|
)
|
2012-06-27 20:45:01 -07:00
|
|
|
for mailbox in mailboxes:
|
2019-05-09 12:41:53 +02:00
|
|
|
if logging
|
2014-08-22 20:02:58 -07:00
|
|
|
logger.info(
|
2019-05-09 12:25:19 +02:00
|
|
|
'Gathering messages for %s',
|
|
|
|
|
mailbox.name
|
2014-08-22 20:02:58 -07:00
|
|
|
)
|
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
|
|
|
|
|
)
|