Skip to content

Commit

Permalink
Merge PR #136 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by carmenbianca
  • Loading branch information
OCA-git-bot committed Jun 14, 2024
2 parents 8df73a6 + 15f2d84 commit fd4098a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
24 changes: 23 additions & 1 deletion 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,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:
Expand Down
17 changes: 4 additions & 13 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,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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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()``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``update_belgian_national_number`` renamed to
``update_be_national_register_number``.
17 changes: 9 additions & 8 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 @@ -33,14 +32,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):
Expand Down

0 comments on commit fd4098a

Please sign in to comment.