diff --git a/l10n_be_cooperator_national_number/models/res_partner.py b/l10n_be_cooperator_national_number/models/res_partner.py index 47550c8d2..7beea89a8 100644 --- a/l10n_be_cooperator_national_number/models/res_partner.py +++ b/l10n_be_cooperator_national_number/models/res_partner.py @@ -2,7 +2,10 @@ # # SPDX-License-Identifier: AGPL-3.0-or-later +from collections import namedtuple + from odoo import api, models +from odoo.exceptions import ValidationError class ResPartner(models.Model): @@ -32,7 +35,26 @@ def get_be_national_register_number(self): return id_number.name return None - def update_belgian_national_number(self, national_number): + @api.model + def validate_be_national_register_number(self, national_number, error=False): + """Verify whether the national number is valid. Returns True if valid. + + If error is True, a ValidationError is raised instead. + """ + belgian_cat = self.sudo().get_be_national_register_number_id_category() + # The validation function expects an id_number record. We don't have + # that yet, so we'll mock one. + IdNumber = namedtuple("ResPartnerIdNumber", ["name"]) + id_number = IdNumber(national_number) + try: + belgian_cat.validate_id_number(id_number) + except ValidationError: + if error: + raise + return False + return True + + def update_be_national_register_number(self, national_number): self.ensure_one() result = None if national_number: diff --git a/l10n_be_cooperator_national_number/models/subscription_request.py b/l10n_be_cooperator_national_number/models/subscription_request.py index 52bc63129..217ff56ff 100644 --- a/l10n_be_cooperator_national_number/models/subscription_request.py +++ b/l10n_be_cooperator_national_number/models/subscription_request.py @@ -2,7 +2,6 @@ # # SPDX-License-Identifier: AGPL-3.0-or-later -from collections import namedtuple from odoo import _, api, fields, models from odoo.exceptions import UserError @@ -33,26 +32,18 @@ def _compute_require_national_number(self): request.company_id.get_require_national_number(request.is_company) ) - @api.model - def check_be_national_register_number(self, national_number): - national_number_id_category = self.env[ - "res.partner" - ].get_be_national_register_number_id_category() - # this function checks the value of id_number.name, not id_number - # directly. - id_number = namedtuple("id_number", ("name"))(national_number) - national_number_id_category.validate_id_number(id_number) - def validate_subscription_request(self): self.ensure_one() if self.require_national_number and not self.national_number: raise UserError(_("National Number is required.")) if self.national_number: - self.check_be_national_register_number(self.national_number) + self.env["res.partner"].validate_be_national_register_number( + self.national_number, error=True + ) invoice = super().validate_subscription_request() if not self.is_company: partner = invoice.partner_id - partner.update_belgian_national_number(self.national_number) + partner.update_be_national_register_number(self.national_number) return invoice def set_person_info(self, partner): diff --git a/l10n_be_cooperator_national_number/readme/newsfragments/136.misc.1.rst b/l10n_be_cooperator_national_number/readme/newsfragments/136.misc.1.rst new file mode 100644 index 000000000..5cb3378d3 --- /dev/null +++ b/l10n_be_cooperator_national_number/readme/newsfragments/136.misc.1.rst @@ -0,0 +1,4 @@ +``check_be_national_register_number`` moved from ``subscription.request`` to +``res.partner`` and renamed to ``validate_be_national_register_number``. Added +``error`` kwarg, and it is no longer necessary to call this method with +``sudo()``. diff --git a/l10n_be_cooperator_national_number/readme/newsfragments/136.misc.2.rst b/l10n_be_cooperator_national_number/readme/newsfragments/136.misc.2.rst new file mode 100644 index 000000000..9c3aecb7d --- /dev/null +++ b/l10n_be_cooperator_national_number/readme/newsfragments/136.misc.2.rst @@ -0,0 +1,2 @@ +``update_belgian_national_number`` renamed to +``update_be_national_register_number``. diff --git a/l10n_be_cooperator_website_national_number/controllers/main.py b/l10n_be_cooperator_website_national_number/controllers/main.py index 75b1104f4..6271768dd 100644 --- a/l10n_be_cooperator_website_national_number/controllers/main.py +++ b/l10n_be_cooperator_website_national_number/controllers/main.py @@ -1,4 +1,3 @@ -from odoo.exceptions import ValidationError from odoo.http import request from odoo.tools.translate import _ @@ -37,14 +36,16 @@ def _additional_validate(self, kwargs, logged, values, post_file): return result national_number = values.get("national_number") if national_number: - try: - # sudo is required to allow access to res.partner.id_category. - request.env[ - "subscription.request" - ].sudo().check_be_national_register_number(values["national_number"]) + valid = request.env["res.partner"].validate_be_national_register_number( + values["national_number"] + ) + if not valid: + values["error_msg"] = ( + _("%s is not a valid Belgian national registration number") + % values["national_number"] + ) + else: return True - except ValidationError as ve: - values["error_msg"] = str(ve) else: is_company = kwargs.get("is_company") == "on" if not request.env.company.get_require_national_number(is_company):