Skip to content
Snippets Groups Projects
Commit 16ede322 authored by althaf shaik's avatar althaf shaik
Browse files

[FIX] account_tax_python: prevent traceback while computing python code in tax


Syntax Error generates when the user gives invalid python code in 'account_tax'
module and uses that tax while creating invoice.

Steps to produce:
 * Install 'account_tax_python' module
 * Go to configuration/taxes and create a new tax.
 * Select Tax Computation as 'python code' and give some special characters to
   python code field and save it.
 * Now create an invoice, add a  product and add the above created tax in taxes.
 * At this moment traceback raises.

By applying these changes will resolve this issue.

closes odoo/odoo#126940

Sentry: 4060222060
X-original-commit: 74642632
Signed-off-by: default avatarOlivier Colson (oco) <oco@odoo.com>
parent 38c453c8
No related branches found
No related tags found
No related merge requests found
......@@ -107,3 +107,13 @@ msgstr ""
#: model:ir.model,name:account_tax_python.model_account_tax_template
msgid "Templates for Taxes"
msgstr ""
#. module: account_tax_python
#: code:addons/account_tax_python/models/account_tax.py:0
#: code:addons/account_tax_python/models/account_tax.py:0
#, python-format
msgid ""
"You entered invalid code %r in %r taxes\n"
"\n"
"Error : %s"
msgstr ""
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models, fields, api
from odoo import models, fields, _
from odoo.tools.safe_eval import safe_eval
from odoo.exceptions import UserError
class AccountTaxPython(models.Model):
......@@ -35,7 +36,10 @@ class AccountTaxPython(models.Model):
if self.amount_type == 'code':
company = self.env.company
localdict = {'base_amount': base_amount, 'price_unit':price_unit, 'quantity': quantity, 'product':product, 'partner':partner, 'company': company}
safe_eval(self.python_compute, localdict, mode="exec", nocopy=True)
try:
safe_eval(self.python_compute, localdict, mode="exec", nocopy=True)
except Exception as e:
raise UserError(_("You entered invalid code %r in %r taxes\n\nError : %s") % (self.python_compute, self.name, e)) from e
return localdict['result']
return super(AccountTaxPython, self)._compute_amount(base_amount, price_unit, quantity, product, partner)
......@@ -47,7 +51,10 @@ class AccountTaxPython(models.Model):
for tax in self.filtered(lambda r: r.amount_type == 'code'):
localdict = self._context.get('tax_computation_context', {})
localdict.update({'price_unit': price_unit, 'quantity': quantity, 'product': product, 'partner': partner, 'company': company})
safe_eval(tax.python_applicable, localdict, mode="exec", nocopy=True)
try:
safe_eval(tax.python_applicable, localdict, mode="exec", nocopy=True)
except Exception as e:
raise UserError(_("You entered invalid code %r in %r taxes\n\nError : %s") % (tax.python_applicable, tax.name, e)) from e
if localdict.get('result', False):
taxes += tax
return super(AccountTaxPython, taxes).compute_all(price_unit, currency, quantity, product, partner, is_refund=is_refund, handle_price_include=handle_price_include, include_caba_tags=include_caba_tags)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment