mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-10 06:48:19 +02:00
Added explicit getmail command arguments to parse one or more mailbox names and added maximum email read count
This commit is contained in:
parent
462fdd3e49
commit
221585a254
3 changed files with 19 additions and 10 deletions
|
|
@ -6,6 +6,11 @@ from django_mailbox.models import Mailbox
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument('-mb', '--mailboxes', type=str, nargs='+', help='Write mailbox names')
|
||||||
|
parser.add_argument('-mr', '--max_read', type=int,
|
||||||
|
help='Write maximum number of email to read from each mailbox')
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
Mailbox.get_new_mail_all_mailboxes(args)
|
Mailbox.get_new_mail_all_mailboxes(options)
|
||||||
|
|
|
||||||
|
|
@ -446,15 +446,19 @@ class Mailbox(models.Model):
|
||||||
save=False
|
save=False
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_new_mail(self, condition=None):
|
def get_new_mail(self, condition=None, max_read=None):
|
||||||
"""Connect to this transport and fetch new messages."""
|
"""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:
|
||||||
return
|
return
|
||||||
for message in connection.get_message(condition):
|
for message in connection.get_message(condition):
|
||||||
|
if max_read is not None and max_read <= 0:
|
||||||
|
break
|
||||||
msg = self.process_incoming_message(message)
|
msg = self.process_incoming_message(message)
|
||||||
if not msg is None:
|
if msg is not None:
|
||||||
|
if max_read is not None:
|
||||||
|
max_read -= 1
|
||||||
yield msg
|
yield msg
|
||||||
self.last_polling = now()
|
self.last_polling = now()
|
||||||
if django.VERSION >= (1, 5): # Django 1.5 introduces update_fields
|
if django.VERSION >= (1, 5): # Django 1.5 introduces update_fields
|
||||||
|
|
@ -463,18 +467,18 @@ class Mailbox(models.Model):
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_new_mail_all_mailboxes(args=None):
|
def get_new_mail_all_mailboxes(options: dict):
|
||||||
mailboxes = Mailbox.active_mailboxes.all()
|
mailboxes = Mailbox.active_mailboxes.all()
|
||||||
if args:
|
if options.get('mailboxes'):
|
||||||
mailboxes = mailboxes.filter(
|
mailboxes = mailboxes.filter(
|
||||||
name=' '.join(args)
|
name__in=options['mailboxes']
|
||||||
)
|
)
|
||||||
for mailbox in mailboxes:
|
for mailbox in mailboxes:
|
||||||
logger.info(
|
logger.info(
|
||||||
'Gathering messages for %s',
|
'Gathering messages for %s',
|
||||||
mailbox.name
|
mailbox.name
|
||||||
)
|
)
|
||||||
messages = mailbox.get_new_mail()
|
messages = mailbox.get_new_mail(max_read=options.get('max_read'))
|
||||||
for message in messages:
|
for message in messages:
|
||||||
logger.info(
|
logger.info(
|
||||||
'Received %s (from %s)',
|
'Received %s (from %s)',
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,11 @@ Using a cron job
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
You can easily consume incoming mail by running the management command named
|
You can easily consume incoming mail by running the management command named
|
||||||
``getmail`` (optionally with an argument of the name of the mailbox you'd like
|
``getmail`` (optionally with an argument of the name(s) of the mailbox(es) you'd like
|
||||||
to get the mail for).::
|
to get the mail for. You can also pass maximum number of email count to read).::
|
||||||
|
|
||||||
python manage.py getmail
|
python manage.py getmail
|
||||||
|
python manage.py getmail --mailboxes mailbox1 mailbox2 --max_read 10
|
||||||
|
|
||||||
.. _receiving-mail-from-exim4-or-postfix:
|
.. _receiving-mail-from-exim4-or-postfix:
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue