[FIX] project_average_acceptable_time: use on_account_update hook
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m23s
tests / test (pull_request) Successful in 2m39s

The portal override validated the submitted data a second time after
``super().account()`` returned. When the partner cannot edit its VAT (child
contact or issued invoices), the country select is disabled and not
submitted; Odoo core injects the missing ``country_id`` in its own copy of
the posted data, but the raw payload re-validated by the module still
lacked it, raising ``KeyError: 'country_id'`` and a 500 on save.

Move the default-project update to ``on_account_update()``, which Odoo
calls after a successful validation, and drop the duplicate validation.
Add a regression test with a child contact whose country field is disabled.
This commit is contained in:
Stéphan Sainléger
2026-11-10 22:57:57 +01:00
parent 533e12d376
commit dbb5711ae9
3 changed files with 35 additions and 18 deletions

View File

@@ -3,7 +3,7 @@
{ {
"name": "project_average_acceptable_time", "name": "project_average_acceptable_time",
"version": "18.0.1.1.1", "version": "18.0.1.1.2",
"author": "Elabore", "author": "Elabore",
"website": "https://git.elabore.coop/elabore/project-tools", "website": "https://git.elabore.coop/elabore/project-tools",
"maintainer": "Clément Thomas", "maintainer": "Clément Thomas",

View File

@@ -1,6 +1,6 @@
# Copyright 2020 Lokavaluto () # Copyright 2020 Lokavaluto ()
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.http import request, route from odoo.http import request
from odoo.addons.portal.controllers.portal import CustomerPortal from odoo.addons.portal.controllers.portal import CustomerPortal
@@ -11,19 +11,14 @@ class CustomCustomerPortal(CustomerPortal):
"average_acceptable_time" "average_acceptable_time"
] # unnecessary save in res partner, but necessary to avoid error on form post ] # unnecessary save in res partner, but necessary to avoid error on form post
@route(["/my/account"], type="http", auth="user", website=True) def on_account_update(self, values, partner):
def account(self, redirect=None, **post): res = super().on_account_update(values, partner)
response = super().account(redirect, **post) average_acceptable_time = values.get("average_acceptable_time")
if average_acceptable_time:
if post and request.httprequest.method == "POST": # sudo: a portal user has no write access at all on
error, error_message = self.details_form_validate(post) # project.project, and no read access on its default project
if not error: # unless it follows it
# sudo: a portal user has no write access at all on project_sudo = request.env.user.sudo().default_project_id
# project.project, and no read access on its default project if project_sudo:
# unless it follows it project_sudo.average_acceptable_time = average_acceptable_time
project_sudo = request.env.user.sudo().default_project_id return res
average_acceptable_time = post.get("average_acceptable_time")
if project_sudo and average_acceptable_time:
project_sudo.average_acceptable_time = average_acceptable_time
return response

View File

@@ -52,3 +52,25 @@ class TestPortalAccountAverageAcceptableTime(HttpCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.project.invalidate_recordset() self.project.invalidate_recordset()
self.assertEqual(self.project.average_acceptable_time, 1.5) self.assertEqual(self.project.average_acceptable_time, 1.5)
def test_account_post_when_country_cannot_be_edited(self):
"""The form must save when the country select is disabled.
A contact with a parent cannot edit its VAT, so the country select
is disabled and is not submitted with the form. Odoo core injects
the missing ``country_id`` in its own copy of the posted data, so
the controller must not validate the raw payload a second time.
"""
company = self.env["res.partner"].create(
{"name": "Portal company", "is_company": True}
)
self.portal_user.partner_id.write(
{"parent_id": company.id, "country_id": self.env.ref("base.fr").id}
)
self.authenticate(self.portal_user.login, self.portal_user.login)
data = self._account_form_data(average_acceptable_time="2.0")
data.pop("country_id")
response = self.url_open("/my/account", data=data)
self.assertEqual(response.status_code, 200)
self.project.invalidate_recordset()
self.assertEqual(self.project.average_acceptable_time, 2.0)