Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions django_mailbox/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@


def get_new_mail(mailbox_admin, request, queryset):
for mailbox in queryset.all():
logger.debug('Receiving mail for %s' % mailbox)
mailbox.get_new_mail()
queryset.get_new_mail()


get_new_mail.short_description = _('Get new mail')
Expand Down
18 changes: 16 additions & 2 deletions django_mailbox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,21 @@
logger = logging.getLogger(__name__)


class ActiveMailboxManager(models.Manager):
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(MailboxManager):
def get_queryset(self):
return super().get_queryset().filter(
active=True,
Expand Down Expand Up @@ -106,7 +120,7 @@ class Mailbox(models.Model):
null=True
)

objects = models.Manager()
objects = MailboxManager()
active_mailboxes = ActiveMailboxManager()

@property
Expand Down
10 changes: 10 additions & 0 deletions django_mailbox/tests/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)