From 086bcd3acd1fa1aeb9dfdbbd5d8576086932e955 Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Tue, 16 Aug 2016 01:00:26 +0200 Subject: [PATCH 1/5] Add Mailbox.last_polling model field --- .../migrations/0006_mailbox_last_polling.py | 20 +++++++++++++++++++ django_mailbox/models.py | 11 ++++++++++ 2 files changed, 31 insertions(+) create mode 100644 django_mailbox/migrations/0006_mailbox_last_polling.py diff --git a/django_mailbox/migrations/0006_mailbox_last_polling.py b/django_mailbox/migrations/0006_mailbox_last_polling.py new file mode 100644 index 0000000..a46713b --- /dev/null +++ b/django_mailbox/migrations/0006_mailbox_last_polling.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.8 on 2016-08-15 22:39 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_mailbox', '0005_auto_20160523_2240'), + ] + + operations = [ + migrations.AddField( + model_name='mailbox', + name='last_polling', + field=models.DateTimeField(blank=True, null=True, help_text='The time of last successfull polling of message. It is blank for new mailbox or processing email via pipe only.', verbose_name='Last polling'), + ), + ] diff --git a/django_mailbox/models.py b/django_mailbox/models.py index ec71e77..07a0632 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -25,6 +25,7 @@ from django.core.files.base import ContentFile from django.core.mail.message import make_msgid from django.db import models from django.utils.translation import ugettext_lazy as _ +from django.utils.timezone import now from django_mailbox import utils from django_mailbox.signals import message_received @@ -96,6 +97,14 @@ class Mailbox(models.Model): default=True, ) + last_polling = models.DateTimeField( + _(u"Last polling"), + help_text=(_("The time of last successfull polling of message. " + "It is blank for new mailbox or processing email via pipe only.")), + blank=True, + null=True + ) + objects = models.Manager() active_mailboxes = ActiveMailboxManager() @@ -372,6 +381,8 @@ class Mailbox(models.Model): for message in connection.get_message(condition): msg = self.process_incoming_message(message) new_mail.append(msg) + self.last_polling = now() + self.save(update_fields=['last_polling']) return new_mail def __unicode__(self): From e8bcd56727199de75a0dcefe7590d3866a14f39d Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Tue, 16 Aug 2016 01:00:52 +0200 Subject: [PATCH 2/5] Add tests to update Mailbox.last_polling --- django_mailbox/tests/test_mailbox.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/django_mailbox/tests/test_mailbox.py b/django_mailbox/tests/test_mailbox.py index d06fc77..10fa904 100644 --- a/django_mailbox/tests/test_mailbox.py +++ b/django_mailbox/tests/test_mailbox.py @@ -1,3 +1,5 @@ +import os + from django.test import TestCase from django_mailbox.models import Mailbox @@ -18,3 +20,17 @@ class TestMailbox(TestCase): expected_protocol, actual_protocol, ) + + def test_last_polling_field_exists(self): + mailbox = Mailbox() + self.assertTrue(hasattr(mailbox, 'last_polling')) + + def test_get_new_mail_update_last_polling(self): + mailbox = Mailbox.objects.create(uri="mbox://" + os.path.join( + os.path.dirname(__file__), + 'messages', + 'generic_message.eml', + )) + self.assertEqual(mailbox.last_polling, None) + mailbox.get_new_mail() + self.assertNotEqual(mailbox.last_polling, None) From ef88900787a45779e02f78e4d7b19df6c110379e Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Tue, 16 Aug 2016 01:01:25 +0200 Subject: [PATCH 3/5] Add last_polling to MailboxAdmin --- django_mailbox/admin.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index bc3c25a..6bfebd4 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -43,7 +43,9 @@ class MailboxAdmin(admin.ModelAdmin): 'uri', 'from_email', 'active', + 'last_polling', ) + readonly_fields = ['last_polling', ] actions = [get_new_mail] From fc05ee650543884580222e43849f4e6f92972ade Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Tue, 16 Aug 2016 01:10:55 +0200 Subject: [PATCH 4/5] Fix backward compatibility to django<1.5 in Mailbox.get_new_mail --- django_mailbox/models.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 07a0632..97a1343 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -20,6 +20,7 @@ import uuid import six from six.moves.urllib.parse import parse_qs, unquote, urlparse +import django from django.conf import settings as django_settings from django.core.files.base import ContentFile from django.core.mail.message import make_msgid @@ -382,7 +383,10 @@ class Mailbox(models.Model): msg = self.process_incoming_message(message) new_mail.append(msg) self.last_polling = now() - self.save(update_fields=['last_polling']) + if django.VERSION >= (1, 5): # Django 1.5 introduces update_fields + self.save(update_fields=['last_polling']) + else: + self.save() return new_mail def __unicode__(self): From 251b12161c9d2804bfab0faccf56f3e6a018bae0 Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Tue, 16 Aug 2016 07:18:51 +0200 Subject: [PATCH 5/5] Fix grammar of Mailbox.last_polling.help_text --- django_mailbox/migrations/0006_mailbox_last_polling.py | 2 +- django_mailbox/models.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/django_mailbox/migrations/0006_mailbox_last_polling.py b/django_mailbox/migrations/0006_mailbox_last_polling.py index a46713b..7979428 100644 --- a/django_mailbox/migrations/0006_mailbox_last_polling.py +++ b/django_mailbox/migrations/0006_mailbox_last_polling.py @@ -15,6 +15,6 @@ class Migration(migrations.Migration): migrations.AddField( model_name='mailbox', name='last_polling', - field=models.DateTimeField(blank=True, null=True, help_text='The time of last successfull polling of message. It is blank for new mailbox or processing email via pipe only.', verbose_name='Last polling'), + field=models.DateTimeField(blank=True, help_text='The time of last successful polling for messages.It is blank for new mailboxes and is not set for mailboxes that only receive messages via a pipe.', null=True, verbose_name='Last polling'), ), ] diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 97a1343..89eb0ad 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -100,8 +100,9 @@ class Mailbox(models.Model): last_polling = models.DateTimeField( _(u"Last polling"), - help_text=(_("The time of last successfull polling of message. " - "It is blank for new mailbox or processing email via pipe only.")), + help_text=(_("The time of last successful polling for messages." + "It is blank for new mailboxes and is not set for " + "mailboxes that only receive messages via a pipe.")), blank=True, null=True )