3 Commits

Author SHA1 Message Date
Stéphan Sainléger
dbb5711ae9 [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.
2026-11-10 22:57:57 +01:00
Stéphan Sainléger
533e12d376 [REF] project_average_acceptable_time: read posted value once
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m30s
tests / test (pull_request) Successful in 2m0s
The portal override looked the custom field up twice, once with ``get()``
for the condition and once with ``[]`` for the assignment. Read it once so
the code stays safe if the input is absent from the submitted form.
2026-11-09 00:33:08 +01:00
Stéphan Sainléger
82971af4d4 [FIX] project_average_acceptable_time: use Odoo 18 optional fields API
Odoo 18 replaced the ``OPTIONAL_BILLING_FIELDS`` class attribute with the
``_get_optional_fields()`` method. The portal controller still appended to
the removed attribute, raising ``AttributeError`` on every request to
``/my/account`` and making the page unavailable to all portal users.

Override ``_get_optional_fields()`` instead, as ``elabore_subscription_price``
already does, and add HTTP tests covering the page load and the default
project update on form submission.
2026-11-09 00:21:02 +01:00
4 changed files with 93 additions and 21 deletions

View File

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

View File

@@ -1,29 +1,24 @@
# Copyright 2020 Lokavaluto ()
# 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
class CustomCustomerPortal(CustomerPortal):
@route(["/my/account"], type="http", auth="user", website=True)
def account(self, redirect=None, **post):
self.OPTIONAL_BILLING_FIELDS.append(
def _get_optional_fields(self):
return super()._get_optional_fields() + [
"average_acceptable_time"
) # unecessary 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
response = super().account(redirect, **post)
if post and request.httprequest.method == "POST":
error, error_message = self.details_form_validate(post)
if not error:
# sudo: a portal user has no write access at all on
# project.project, and no read access on its default project
# unless it follows it
project_sudo = request.env.user.sudo().default_project_id
if project_sudo and post.get("average_acceptable_time"):
project_sudo.average_acceptable_time = post[
"average_acceptable_time"
]
return response
def on_account_update(self, values, partner):
res = super().on_account_update(values, partner)
average_acceptable_time = values.get("average_acceptable_time")
if average_acceptable_time:
# sudo: a portal user has no write access at all on
# project.project, and no read access on its default project
# unless it follows it
project_sudo = request.env.user.sudo().default_project_id
if project_sudo:
project_sudo.average_acceptable_time = average_acceptable_time
return res

View File

@@ -0,0 +1 @@
from . import test_portal_account

View File

@@ -0,0 +1,76 @@
# Copyright 2026 Stéphan Sainléger (Elabore)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.http import Request
from odoo.tests import HttpCase, tagged
from odoo.addons.mail.tests.common import mail_new_test_user
@tagged("-at_install", "post_install")
class TestPortalAccountAverageAcceptableTime(HttpCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.project = cls.env["project.project"].create(
{"name": "Portal average acceptable time"}
)
cls.portal_user = mail_new_test_user(
cls.env,
login="portal_average_acceptable_time",
name="Portal Average Acceptable Time",
groups="base.group_portal",
default_project_id=cls.project.id,
)
def _account_form_data(self, **overrides):
data = {
"name": self.portal_user.partner_id.name,
"email": self.portal_user.partner_id.email,
"phone": "+33123456789",
"street": "1 rue de la Paix",
"city": "Paris",
"zipcode": "75002",
"country_id": self.env.ref("base.fr").id,
"csrf_token": Request.csrf_token(self),
}
data.update(overrides)
return data
def test_account_page_renders(self):
"""The portal account page must load for a portal user."""
self.authenticate(self.portal_user.login, self.portal_user.login)
response = self.url_open("/my/account")
self.assertEqual(response.status_code, 200)
def test_account_post_updates_default_project(self):
"""Posting the form must store the value on the default project."""
self.authenticate(self.portal_user.login, self.portal_user.login)
response = self.url_open(
"/my/account",
data=self._account_form_data(average_acceptable_time="1.5"),
)
self.assertEqual(response.status_code, 200)
self.project.invalidate_recordset()
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)