Skip to content

Commit

Permalink
[FIX] validate national number on website form
Browse files Browse the repository at this point in the history
* validate belgian national register number on website form.
* fix style of belgian national register number field.
* only display belgian national register number field if
  display_national_number is true.
  • Loading branch information
huguesdk committed May 3, 2024
1 parent 22e48d3 commit b0988ab
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
42 changes: 40 additions & 2 deletions l10n_be_cooperator_website_national_number/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from odoo.exceptions import ValidationError
from odoo.http import request
from odoo.tools.translate import _

from odoo.addons.cooperator_website.controllers.main import WebsiteSubscription

Expand All @@ -19,8 +21,44 @@ def get_values_from_user(self, values, is_company):
values["national_number"] = national_number
return values

def _get_display_national_number(self, is_company):
if is_company:
return False
return request.env.company.display_national_number

Check warning on line 27 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#L26-L27

Added lines #L26 - L27 were not covered by tests

def _get_require_national_number(self, is_company):
if is_company:
return False
return request.env.company.require_national_number

Check warning on line 32 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#L31-L32

Added lines #L31 - L32 were not covered by tests

def fill_values(self, values, is_company, logged, load_from_user=False):
values = super().fill_values(values, is_company, logged, load_from_user)
if not is_company and request.env.company.require_national_number:
values["national_number_required"] = True
values["display_national_number"] = self._get_display_national_number(

Check warning on line 36 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#L36

Added line #L36 was not covered by tests
is_company
)
values["national_number_required"] = self._get_require_national_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
is_company
)
return values

def _additional_validate(self, kwargs, logged, values, post_file):
result = super()._additional_validate(kwargs, logged, values, post_file)

Check warning on line 45 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#L45

Added line #L45 was not covered by tests
if result is not True:
return result
is_company = kwargs.get("is_company") == "on"
national_number = values.get("national_number")

Check warning on line 49 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#L47-L49

Added lines #L47 - L49 were not covered by tests
if national_number:
try:

Check warning on line 51 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#L51

Added line #L51 was not covered by tests
# sudo is required to allow access to res.partner.id_category.
request.env[

Check warning on line 53 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#L53

Added line #L53 was not covered by tests
"subscription.request"
].sudo().check_be_national_register_number(values["national_number"])
return True
except ValidationError as ve:
values["error_msg"] = str(ve)

Check warning on line 58 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#L56-L58

Added lines #L56 - L58 were not covered by tests
else:
if not self._get_require_national_number(is_company):
return True
values["error_msg"] = _("Some mandatory fields have not been filled.")
values["error"] = {"national_number"}
return False

Check warning on line 64 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#L61-L64

Added lines #L61 - L64 were not covered by tests
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<template id="national_number_template" name="national_number_template">
<div
class="form-group field-national_number"
t-attf-class="form-group #{error and 'national_number' in error and 'has-error' or ''}"
name="national_number_container"
>
<label for="national_number">National Number</label>
<div class="mb-3 field-national_number" name="national_number_container">
<label for="national_number" class="form-label">National Number</label>
<input
class="form-control form-control-sm"
id="national_number"
name="national_number"
t-attf-class="form-control form-control-sm{{ error and 'national_number' in error and ' is-invalid' or '' }}"
type="text"
t-att-readonly="logged"
name="national_number"
id="national_number"
t-att-required="national_number_required"
t-attf-value="#{national_number or ''}"
t-att-readonly="logged"
t-att-value="national_number"
placeholder="YY.MM.DD-NNN.MM"
/>
</div>
</template>
Expand All @@ -29,6 +27,7 @@
position="after"
>
<t
t-if="display_national_number"
t-call="l10n_be_cooperator_website_national_number.national_number_template"
/>
</xpath>
Expand Down

0 comments on commit b0988ab

Please sign in to comment.