forked from mirror/django-mailbox
Merge pull request #112 from ad-m/add-last-polling
Add last success polling - see #111
This commit is contained in:
commit
0e9f226b35
4 changed files with 54 additions and 0 deletions
|
|
@ -43,7 +43,9 @@ class MailboxAdmin(admin.ModelAdmin):
|
|||
'uri',
|
||||
'from_email',
|
||||
'active',
|
||||
'last_polling',
|
||||
)
|
||||
readonly_fields = ['last_polling', ]
|
||||
actions = [get_new_mail]
|
||||
|
||||
|
||||
|
|
|
|||
20
django_mailbox/migrations/0006_mailbox_last_polling.py
Normal file
20
django_mailbox/migrations/0006_mailbox_last_polling.py
Normal file
|
|
@ -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, 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'),
|
||||
),
|
||||
]
|
||||
|
|
@ -20,11 +20,13 @@ 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
|
||||
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 +98,15 @@ class Mailbox(models.Model):
|
|||
default=True,
|
||||
)
|
||||
|
||||
last_polling = models.DateTimeField(
|
||||
_(u"Last polling"),
|
||||
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
|
||||
)
|
||||
|
||||
objects = models.Manager()
|
||||
active_mailboxes = ActiveMailboxManager()
|
||||
|
||||
|
|
@ -372,6 +383,11 @@ 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()
|
||||
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):
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue