lingo: use wrapper for remote invoices notifications (#22756)
Fix creation and payment limit dates parsing.
This commit is contained in:
parent
0b5e16ba41
commit
b493ae7406
|
@ -21,7 +21,6 @@ import json
|
|||
import logging
|
||||
import urlparse
|
||||
|
||||
from dateutil import parser
|
||||
from decimal import Decimal
|
||||
|
||||
import eopayment
|
||||
|
@ -229,15 +228,14 @@ class Regie(models.Model):
|
|||
return 'invoice-%s' % self.slug
|
||||
|
||||
def get_notification_id(self, invoice):
|
||||
return '%s:%s' % (self.get_notification_namespace(), invoice['id'])
|
||||
return '%s:%s' % (self.get_notification_namespace(), invoice.id)
|
||||
|
||||
def get_notification_reminder_id(self, invoice):
|
||||
return '%s:reminder-%s' % (self.get_notification_namespace(), invoice['id'])
|
||||
return '%s:reminder-%s' % (self.get_notification_namespace(), invoice.id)
|
||||
|
||||
def notify_invoice(self, user, invoice):
|
||||
now = timezone.now()
|
||||
today = timezone.now().date()
|
||||
remind_delta = timezone.timedelta(days=settings.LINGO_NEW_INVOICES_REMIND_DELTA)
|
||||
pay_limit_date = timezone.make_aware(dateparse.parse_datetime(invoice['pay_limit_date']))
|
||||
active_items_cell = ActiveItems.objects.first()
|
||||
if active_items_cell:
|
||||
items_page_url = active_items_cell.page.get_online_url()
|
||||
|
@ -245,16 +243,16 @@ class Regie(models.Model):
|
|||
items_page_url = ''
|
||||
notification_id = self.get_notification_id(invoice)
|
||||
notification_reminder_id = self.get_notification_reminder_id(invoice)
|
||||
if pay_limit_date < now:
|
||||
if invoice.payment_limit_date < today:
|
||||
# invoice is out of date
|
||||
Notification.objects.find(user, notification_id).forget()
|
||||
Notification.objects.find(user, notification_reminder_id).forget()
|
||||
else:
|
||||
# invoice can be paid
|
||||
if pay_limit_date > now + remind_delta:
|
||||
message = _('Invoice %s to pay') % invoice['label']
|
||||
if invoice.payment_limit_date >= today + remind_delta:
|
||||
message = _('Invoice %s to pay') % invoice.subject
|
||||
else:
|
||||
message = _('Reminder: invoice %s to pay') % invoice['label']
|
||||
message = _('Reminder: invoice %s to pay') % invoice.subject
|
||||
notification_id = notification_reminder_id
|
||||
if not Notification.objects.find(user, notification_id).exists():
|
||||
self.notify_remote_invoice_by_email(user, invoice)
|
||||
|
@ -262,7 +260,7 @@ class Regie(models.Model):
|
|||
summary=message,
|
||||
id=notification_id,
|
||||
url=items_page_url,
|
||||
end_timestamp=pay_limit_date)
|
||||
end_timestamp=invoice.payment_limit_date)
|
||||
return notification_id
|
||||
|
||||
def notify_new_remote_invoices(self):
|
||||
|
@ -277,9 +275,10 @@ class Regie(models.Model):
|
|||
except UserSAMLIdentifier.DoesNotExist:
|
||||
continue
|
||||
for invoice in items['invoices']:
|
||||
if Decimal(invoice['total_amount']) >= self.payment_min_amount:
|
||||
remote_invoice = build_remote_item(invoice, self)
|
||||
if remote_invoice.total_amount >= self.payment_min_amount:
|
||||
notification_ids.append(
|
||||
self.notify_invoice(user, invoice))
|
||||
self.notify_invoice(user, remote_invoice))
|
||||
# clear old notifications for invoice not in the source anymore
|
||||
Notification.objects.namespace(self.get_notification_namespace())\
|
||||
.exclude(external_id__in=notification_ids) \
|
||||
|
@ -291,19 +290,18 @@ class Regie(models.Model):
|
|||
text_body_template = 'lingo/combo/invoice_email_notification_body.txt'
|
||||
html_body_template = 'lingo/combo/invoice_email_notification_body.html'
|
||||
|
||||
remote_item = build_remote_item(invoice, self)
|
||||
payment_url = reverse('view-item', kwargs={'regie_id': self.id,
|
||||
'item_crypto_id': remote_item.crypto_id})
|
||||
ctx = {'item': remote_item}
|
||||
'item_crypto_id': invoice.crypto_id})
|
||||
ctx = {'item': invoice}
|
||||
ctx.update({'payment_url': urlparse.urljoin(settings.SITE_BASE_URL, payment_url)})
|
||||
subject = render_to_string([subject_template], ctx).strip()
|
||||
text_body = render_to_string([text_body_template], ctx)
|
||||
html_body = render_to_string([html_body_template], ctx)
|
||||
message = EmailMultiAlternatives(subject, text_body, to=[user.email])
|
||||
message.attach_alternative(html_body, 'text/html')
|
||||
if invoice['has_pdf']:
|
||||
invoice_pdf = self.get_invoice_pdf(user, invoice['id'])
|
||||
message.attach('%s.pdf' % invoice['id'], invoice_pdf.content, 'application/pdf')
|
||||
if invoice.has_pdf:
|
||||
invoice_pdf = self.get_invoice_pdf(user, invoice.id)
|
||||
message.attach('%s.pdf' % invoice.id, invoice_pdf.content, 'application/pdf')
|
||||
message.send()
|
||||
|
||||
|
||||
|
@ -378,8 +376,8 @@ class RemoteItem(object):
|
|||
online_payment, paid, payment_date, no_online_payment_reason):
|
||||
self.id = id
|
||||
self.regie = regie
|
||||
self.creation_date = parser.parse(creation_date)
|
||||
self.payment_limit_date = parser.parse(payment_limit_date)
|
||||
self.creation_date = dateparse.parse_date(creation_date)
|
||||
self.payment_limit_date = dateparse.parse_date(payment_limit_date)
|
||||
self.total_amount = Decimal(total_amount)
|
||||
self.amount = Decimal(amount)
|
||||
self.display_id = display_id or self.id
|
||||
|
|
|
@ -390,10 +390,9 @@ def test_send_new_remote_invoices_by_email(mock_get, user_saml, admin, app, remo
|
|||
mocked_objects = mock.Mock()
|
||||
mocked_objects.get.return_value = mock.Mock(user=admin)
|
||||
user_saml.objects = mocked_objects
|
||||
datetime_format = '%Y-%m-%dT%H:%M:%S'
|
||||
invoice_now = now()
|
||||
creation_date = (invoice_now - timedelta(days=1)).strftime(datetime_format)
|
||||
pay_limit_date = (invoice_now + timedelta(days=30)).strftime(datetime_format)
|
||||
creation_date = (invoice_now - timedelta(days=1)).date().isoformat()
|
||||
pay_limit_date = (invoice_now + timedelta(days=30)).date().isoformat()
|
||||
FAKE_PENDING_INVOICES = {
|
||||
'data' : {'foo': {'invoices': [{'id': '01', 'label': '010101', 'paid': False,
|
||||
'amount': '37.26', 'total_amount': '37.26', 'online_payment': True,
|
||||
|
|
|
@ -277,12 +277,10 @@ def test_notification_id_and_origin(user):
|
|||
|
||||
@mock.patch('combo.utils.requests_wrapper.RequestsSession.request')
|
||||
def test_notify_remote_items(mock_get, app, user, user2, regie):
|
||||
|
||||
datetime_format = '%Y-%m-%dT%H:%M:%S'
|
||||
invoice_now = now()
|
||||
creation_date = (invoice_now - timedelta(days=1)).strftime(datetime_format)
|
||||
pay_limit_date = (invoice_now + timedelta(days=20)).strftime(datetime_format)
|
||||
new_pay_limit_date = (invoice_now + timedelta(days=5)).strftime(datetime_format)
|
||||
creation_date = (invoice_now - timedelta(days=1)).date().isoformat()
|
||||
pay_limit_date = (invoice_now + timedelta(days=20)).date().isoformat()
|
||||
new_pay_limit_date = (invoice_now + timedelta(days=5)).date().isoformat()
|
||||
FAKE_PENDING_INVOICES = {
|
||||
"data":
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue