From b9f016f6c65093d13dd700455bb7b945ba3f0e16 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 8 Mar 2021 11:45:14 +0000 Subject: [PATCH] [FIX] *: markupify python content Will require a lot more conversions for this to work.. --- addons/purchase/models/purchase.py | 27 ++++++++++++------- addons/purchase/tests/test_purchase.py | 7 +++-- addons/purchase_stock/models/purchase.py | 14 +++++----- .../tests/test_purchase_order.py | 14 +++++++--- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 3be823de601..5d4b78330f9 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -4,6 +4,8 @@ from datetime import datetime, time from dateutil.relativedelta import relativedelta from functools import partial from itertools import groupby + +from markupsafe import escape, Markup from pytz import timezone, UTC from werkzeug.urls import url_encode @@ -308,9 +310,6 @@ class PurchaseOrder(models.Model): def onchange_partner_id_warning(self): if not self.partner_id or not self.env.user.has_group('purchase.group_warning_purchase'): return - warning = {} - title = False - message = False partner = self.partner_id @@ -705,7 +704,7 @@ class PurchaseOrder(models.Model): raise_exception=False, email_values={'email_to': self.env.user.email, 'recipient_ids': []}, notif_layout="mail.mail_notification_paynow") - return {'toast_message': _("A sample email has been sent to %s.") % self.env.user.email} + return {'toast_message': escape(_("A sample email has been sent to %s.", self.env.user.email))} def _send_reminder_open_composer(self,template_id): self.ensure_one() @@ -776,7 +775,7 @@ class PurchaseOrder(models.Model): if order.state in ['purchase', 'done'] and not order.mail_reminder_confirmed: order.mail_reminder_confirmed = True date = confirmed_date or self.date_planned.date() - order.message_post(body="%s confirmed the receipt will take place on %s." % (order.partner_id.name, date)) + order.message_post(body=_("%s confirmed the receipt will take place on %s.", order.partner_id.name, date)) def _approval_allowed(self): """Returns whether the order qualifies to be approved by the current user""" @@ -793,7 +792,7 @@ class PurchaseOrder(models.Model): for order in self: if order.state in ['purchase', 'done'] and not order.mail_reception_confirmed: order.mail_reception_confirmed = True - order.message_post(body="The order receipt has been acknowledged by %s." % order.partner_id.name) + order.message_post(body=_("The order receipt has been acknowledged by %s.", order.partner_id.name)) def _update_date_planned_for_lines(self, updated_dates): # create or update the activity @@ -812,9 +811,14 @@ class PurchaseOrder(models.Model): line._update_date_planned(date) def _create_update_date_activity(self, updated_dates): - note = _('

%s modified receipt dates for the following products:

') % self.partner_id.name + note = Markup('

%s

\n') % _('%s modified receipt dates for the following products:', self.partner_id.name) for line, date in updated_dates: - note += _('

  - %s from %s to %s

') % (line.product_id.display_name, line.date_planned.date(), date.date()) + note += Markup('

- %s

\n') % _( + '%(product)s from %(original_receipt_date)s to %(new_receipt_date)s', + product=line.product_id.display_name, + original_receipt_date=line.date_planned.date(), + new_receipt_date=date.date() + ) activity = self.activity_schedule( 'mail.mail_activity_data_warning', summary=_("Date Updated"), @@ -828,7 +832,12 @@ class PurchaseOrder(models.Model): def _update_update_date_activity(self, updated_dates, activity): for line, date in updated_dates: - activity.note += _('

  - %s from %s to %s

') % (line.product_id.display_name, line.date_planned.date(), date.date()) + activity.note += Markup('

- %s

\n') % _( + '%(product)s from %(original_receipt_date)s to %(new_receipt_date)s', + product=line.product_id.display_name, + original_receipt_date=line.date_planned.date(), + new_receipt_date=date.date() + ) class PurchaseOrderLine(models.Model): diff --git a/addons/purchase/tests/test_purchase.py b/addons/purchase/tests/test_purchase.py index 5d56c778b17..db1c93a2feb 100644 --- a/addons/purchase/tests/test_purchase.py +++ b/addons/purchase/tests/test_purchase.py @@ -159,7 +159,8 @@ class TestPurchase(AccountTestInvoicingCommon): ]) self.assertTrue(activity) self.assertIn( - '

partner_a modified receipt dates for the following products:

\xa0 - product_a from 2020-06-06 to %s

' % fields.Date.today(), + '

partner_a modified receipt dates for the following products:

\n' + '

- product_a from 2020-06-06 to %s

' % fields.Date.today(), activity.note, ) @@ -167,6 +168,8 @@ class TestPurchase(AccountTestInvoicingCommon): po._update_date_planned_for_lines([(po.order_line[1], fields.Datetime.today())]) self.assertEqual(po.order_line[1].date_planned, fields.Datetime.today()) self.assertIn( - '

partner_a modified receipt dates for the following products:

\xa0 - product_a from 2020-06-06 to %s

\xa0 - product_b from 2020-06-06 to %s

' % (fields.Date.today(), fields.Date.today()), + '

partner_a modified receipt dates for the following products:

\n' + '

- product_a from 2020-06-06 to %(today)s

\n' + '

- product_b from 2020-06-06 to %(today)s

' % {'today': fields.Date.today()}, activity.note, ) diff --git a/addons/purchase_stock/models/purchase.py b/addons/purchase_stock/models/purchase.py index 9de8c8290f9..e20e65d84c7 100644 --- a/addons/purchase_stock/models/purchase.py +++ b/addons/purchase_stock/models/purchase.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from markupsafe import Markup +from dateutil.relativedelta import relativedelta + from odoo import api, fields, models, SUPERUSER_ID, _ from odoo.tools.float_utils import float_compare -from datetime import datetime -from dateutil.relativedelta import relativedelta from odoo.exceptions import UserError from odoo.addons.purchase.models.purchase import PurchaseOrder as Purchase @@ -241,11 +242,12 @@ class PurchaseOrder(models.Model): """ validated_picking = self.picking_ids.filtered(lambda p: p.state == 'done') if validated_picking: - activity.note += _("

Those dates couldn’t be modified accordingly on the receipt %s which had already been validated.

") % validated_picking[0].name + message = _("Those dates couldn’t be modified accordingly on the receipt %s which had already been validated.", validated_picking[0].name) elif not self.picking_ids: - activity.note += _("

Corresponding receipt not found.

") + message = _("Corresponding receipt not found.") else: - activity.note += _("

Those dates have been updated accordingly on the receipt %s.

") % self.picking_ids[0].name + message = _("Those dates have been updated accordingly on the receipt %s.", self.picking_ids[0].name) + activity.note += Markup('

{}

').format(message) def _create_update_date_activity(self, updated_dates): activity = super()._create_update_date_activity(updated_dates) @@ -255,7 +257,7 @@ class PurchaseOrder(models.Model): # remove old picking info to update it note_lines = activity.note.split('

') note_lines.pop() - activity.note = '

'.join(note_lines) + activity.note = Markup('

').join(note_lines) super()._update_update_date_activity(updated_dates, activity) self._add_picking_info(activity) diff --git a/addons/purchase_stock/tests/test_purchase_order.py b/addons/purchase_stock/tests/test_purchase_order.py index 7d98d24792e..9471a81cb55 100644 --- a/addons/purchase_stock/tests/test_purchase_order.py +++ b/addons/purchase_stock/tests/test_purchase_order.py @@ -229,8 +229,10 @@ class TestPurchaseOrder(ValuationReconciliationTestCommon): ('res_id', '=', po.id), ]) self.assertTrue(activity) - self.assertIn( - '

partner_a modified receipt dates for the following products:

\xa0 - Large Desk from %s to %s

Those dates have been updated accordingly on the receipt %s.

' % (today.date(), tomorrow.date(), po.picking_ids.name), + self.assertEqual( + '

partner_a modified receipt dates for the following products:

\n' + '

- Large Desk from %s to %s

\n' + '

Those dates have been updated accordingly on the receipt %s.

' % (today.date(), tomorrow.date(), po.picking_ids.name), activity.note, ) @@ -243,8 +245,12 @@ class TestPurchaseOrder(ValuationReconciliationTestCommon): old_date = po.order_line[1].date_planned po._update_date_planned_for_lines([(po.order_line[1], tomorrow)]) self.assertEqual(po.order_line[1].date_planned, old_date) - self.assertIn( - '

partner_a modified receipt dates for the following products:

\xa0 - Large Desk from %s to %s

\xa0 - Conference Chair from %s to %s

Those dates couldn’t be modified accordingly on the receipt %s which had already been validated.

' % (today.date(), tomorrow.date(), today.date(), tomorrow.date(), po.picking_ids.name), + self.assertEqual( + '

partner_a modified receipt dates for the following products:

\n' + '

- Large Desk from %s to %s

\n' + '

- Conference Chair from %s to %s

\n' + '

Those dates couldn’t be modified accordingly on the receipt %s which had already been validated.

' % ( + today.date(), tomorrow.date(), today.date(), tomorrow.date(), po.picking_ids.name), activity.note, )