Skip to content

Commit

Permalink
[REF] l10n_be_cooperator_national_number: Move method to res.partner
Browse files Browse the repository at this point in the history
Also implemented a slight fix such that `sudo()` is not necessary to
call this method.

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Jun 13, 2024
1 parent 349408f commit 23f900a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
23 changes: 23 additions & 0 deletions l10n_be_cooperator_national_number/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -32,6 +35,26 @@ def get_be_national_register_number(self):
return id_number.name
return None

@api.model
def check_be_national_register_number(self, national_number, error=False):
"""Verify whether the national number is valid. Returns True if invalid,
which is consistent with validate_l10n_be_national_registry_number.
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 True
return False

def update_belgian_national_number(self, national_number):
self.ensure_one()
result = None
Expand Down
15 changes: 3 additions & 12 deletions l10n_be_cooperator_national_number/models/subscription_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -33,22 +32,14 @@ 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"].check_be_national_register_number(
self.national_number, error=True
)
invoice = super().validate_subscription_request()
if not self.is_company:
partner = invoice.partner_id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``check_be_national_register_number`` moved from ``subscription.request`` to
``res.partner``. Added ``error`` kwarg, and it is no longer necessary to call
this method with ``sudo()``.
17 changes: 8 additions & 9 deletions l10n_be_cooperator_website_national_number/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from odoo.exceptions import ValidationError
from odoo.http import request
from odoo.tools.translate import _

Expand Down Expand Up @@ -37,14 +36,14 @@ 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"])
return True
except ValidationError as ve:
values["error_msg"] = str(ve)
failed = request.env["res.partner"].check_be_national_register_number(
values["national_number"]
)
if failed:
values["error_msg"] = (
_("%s is not a valid Belgian national registration number")
% values["national_number"]
)
else:
is_company = kwargs.get("is_company") == "on"
if not request.env.company.get_require_national_number(is_company):
Expand Down

0 comments on commit 23f900a

Please sign in to comment.