From 505bc8d1c906d34af0046fdf84effe051ea4225f Mon Sep 17 00:00:00 2001 From: George Cheng Date: Sun, 26 Jul 2020 04:55:46 +0000 Subject: [PATCH 1/3] Fix get_new_mail admin action. Mailbox.get_new_mail returns a generator which do not actually run if we do not iterate it. --- django_mailbox/admin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index e9a6ef75..045877d2 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -20,9 +20,11 @@ def get_new_mail(mailbox_admin, request, queryset): + count = 0 for mailbox in queryset.all(): logger.debug('Receiving mail for %s' % mailbox) - mailbox.get_new_mail() + count += sum(1 for i in mailbox.get_new_mail()) + logger.debug('Received %d %s.', count, 'mails' if count != 1 else 'mail') get_new_mail.short_description = _('Get new mail') From 0c6f6aa6cebce80be2adda2f9cf56c128b7c12d7 Mon Sep 17 00:00:00 2001 From: George Cheng Date: Sun, 26 Jul 2020 14:21:21 +0800 Subject: [PATCH 2/3] Move the bulk get_new_mail to queryset --- django_mailbox/admin.py | 6 +----- django_mailbox/models.py | 18 ++++++++++++++++-- django_mailbox/tests/test_mailbox.py | 10 ++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index 045877d2..4d489b1e 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -20,11 +20,7 @@ def get_new_mail(mailbox_admin, request, queryset): - count = 0 - for mailbox in queryset.all(): - logger.debug('Receiving mail for %s' % mailbox) - count += sum(1 for i in mailbox.get_new_mail()) - logger.debug('Received %d %s.', count, 'mails' if count != 1 else 'mail') + queryset.get_new_mail() get_new_mail.short_description = _('Get new mail') diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 4e5b1110..69a1d0c4 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -36,9 +36,23 @@ logger = logging.getLogger(__name__) +class MailboxQuerySet(models.QuerySet): + def get_new_mail(self): + count = 0 + for mailbox in self.all(): + logger.debug('Receiving mail for %s' % mailbox) + count += sum(1 for i in mailbox.get_new_mail()) + logger.debug('Received %d %s.', count, 'mails' if count != 1 else 'mail') + + +class MailboxManager(models.Manager): + def get_queryset(self): + return MailboxQuerySet(self.model, using=self._db) + + class ActiveMailboxManager(models.Manager): def get_queryset(self): - return super().get_queryset().filter( + return MailboxQuerySet(self.model, using=self._db).filter( active=True, ) @@ -106,7 +120,7 @@ class Mailbox(models.Model): null=True ) - objects = models.Manager() + objects = MailboxManager() active_mailboxes = ActiveMailboxManager() @property diff --git a/django_mailbox/tests/test_mailbox.py b/django_mailbox/tests/test_mailbox.py index b55e70ca..e233f753 100644 --- a/django_mailbox/tests/test_mailbox.py +++ b/django_mailbox/tests/test_mailbox.py @@ -34,3 +34,13 @@ def test_get_new_mail_update_last_polling(self): self.assertEqual(mailbox.last_polling, None) list(mailbox.get_new_mail()) self.assertNotEqual(mailbox.last_polling, None) + + def test_queryset_get_new_mail(self): + mailbox = Mailbox.objects.create(uri="mbox://" + os.path.join( + os.path.dirname(__file__), + 'messages', + 'generic_message.eml', + )) + Mailbox.objects.filter(pk=mailbox.pk).get_new_mail() + mailbox.refresh_from_db() + self.assertNotEqual(mailbox.last_polling, None) From b3c38be70edae82b9b560468c673666972f282c0 Mon Sep 17 00:00:00 2001 From: George Cheng Date: Sun, 26 Jul 2020 14:22:40 +0800 Subject: [PATCH 3/3] refine inferitance --- django_mailbox/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 69a1d0c4..5b7fbbcc 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -50,9 +50,9 @@ def get_queryset(self): return MailboxQuerySet(self.model, using=self._db) -class ActiveMailboxManager(models.Manager): +class ActiveMailboxManager(MailboxManager): def get_queryset(self): - return MailboxQuerySet(self.model, using=self._db).filter( + return super().get_queryset().filter( active=True, )