From 9c0cac66e25a751ae0d929d0f8dc1df7540c5aff Mon Sep 17 00:00:00 2001 From: Mike Varga Date: Fri, 14 Oct 2022 13:50:40 -0400 Subject: [PATCH] Utilize read-only method in `get_new_mail` --- django_mailbox/models.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 0f0ac9d..314d0d4 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -17,6 +17,7 @@ import mimetypes import os.path import sys import uuid +from datetime import timedelta from tempfile import NamedTemporaryFile import django @@ -35,6 +36,8 @@ from django_mailbox.transports import Pop3Transport, ImapTransport, \ logger = logging.getLogger(__name__) +INITIAL_IMPORT_LOOKBACK_DAYS = 365 + class MailboxQuerySet(models.QuerySet): def get_new_mail(self): @@ -420,7 +423,12 @@ class Mailbox(models.Model): connection = self.get_connection() if not connection: return - for message in connection.get_message(condition): + + since = self.last_polling + if since is None: + since = now() - timedelta(days=INITIAL_IMPORT_LOOKBACK_DAYS) + + for message in connection.get_new_message_ro(since=since): msg = self.process_incoming_message(message) if not msg is None: yield msg