Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][REF] l10n_be_cooperator_national_number: Move method, rename (other) method #136

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 54 in l10n_be_cooperator_national_number/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

l10n_be_cooperator_national_number/models/res_partner.py#L54

Added line #L54 was not covered by tests
return True

def update_be_national_register_number(self, national_number):
carmenbianca marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -37,14 +36,16 @@
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(

Check warning on line 39 in l10n_be_cooperator_website_national_number/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

l10n_be_cooperator_website_national_number/controllers/main.py#L39

Added line #L39 was not covered by tests
values["national_number"]
)
if not valid:
values["error_msg"] = (

Check warning on line 43 in l10n_be_cooperator_website_national_number/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

l10n_be_cooperator_website_national_number/controllers/main.py#L43

Added line #L43 was not covered by tests
_("%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
Loading