From 7b1dfbc154f2bab0b4fdad07ed523753fd756f27 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 21 Mar 2018 22:28:21 +0100 Subject: [PATCH] lingo: send reminder notification only if first notification has been acked (#22668) --- combo/apps/lingo/models.py | 27 +++++++++++++++------------ tests/test_notification.py | 7 ++++--- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py index 9b53de7..fddd0e0 100644 --- a/combo/apps/lingo/models.py +++ b/combo/apps/lingo/models.py @@ -245,19 +245,22 @@ class Regie(models.Model): Notification.forget(user, notification_reminder_id) else: # invoice can be paid - if pay_limit_date > now + remind_delta: - message = _('Invoice %s to pay') % invoice['label'] - else: - message = _('Reminder: invoice %s to pay') % invoice['label'] - notification_id = notification_reminder_id if not Notification.objects.find(user, notification_id).exists(): self.notify_remote_invoice_by_email(user, invoice) - Notification.notify(user, - summary=message, - id=notification_id, - url=items_page_url, - end_timestamp=pay_limit_date) - return notification_id + notification = Notification.notify(user, + summary=_('Invoice %s to pay') % invoice['label'], + id=notification_id, + url=items_page_url, + end_timestamp=pay_limit_date) + if pay_limit_date <= now + remind_delta and notification.acked: + Notification.notify(user, + summary=_('Reminder: invoice %s to pay') % invoice['label'], + id=notification_reminder_id, + url=items_page_url, + end_timestamp=pay_limit_date) + return [notification_id, notification_reminder_id] + return [notification_id] + return [] def notify_new_remote_invoices(self): pending_invoices = self.get_remote_pending_invoices() @@ -269,7 +272,7 @@ class Regie(models.Model): continue for invoice in items['invoices']: if Decimal(invoice['total_amount']) >= self.payment_min_amount: - notification_ids.append( + notification_ids.extend( self.notify_invoice(user, invoice)) # clear old notifications for invoice not in the source anymore Notification.objects.namespace(self.get_notification_namespace())\ diff --git a/tests/test_notification.py b/tests/test_notification.py index 26c9ec9..82a0806 100644 --- a/tests/test_notification.py +++ b/tests/test_notification.py @@ -360,17 +360,18 @@ def test_notify_remote_items(mock_get, app, user, user2, regie): for user in FAKE_PENDING_INVOICES['data']: for invoice in FAKE_PENDING_INVOICES['data'][user]['invoices']: invoice['pay_limit_date'] = new_pay_limit_date + Notification.objects.all().ack() # create remind notifications regie.notify_new_remote_invoices() assert Notification.objects.exclude(external_id__startswith='invoice-%s:reminder-' % regie.slug) \ - .visible().count() == 0 + .visible().new().count() == 0 assert Notification.objects.filter(external_id__startswith='invoice-%s:reminder-' % regie.slug) \ .visible().new().count() == 2 assert Notification.objects.count() == 4 - # url appeared on new new reminder notifications - assert len([notif for notif in Notification.objects.all() if notif.url == page.get_online_url()]) == 2 + # url appeared on all notifications + assert len([notif for notif in Notification.objects.all() if notif.url == page.get_online_url()]) == 4 # be sure the are no more reminders created regie.notify_new_remote_invoices() -- 2.14.2