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 6, 2024
1 parent 22e48d3 commit 14f7b78
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
1 change: 0 additions & 1 deletion l10n_be_cooperator_website_national_number/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Known issues / Roadmap
======================

- National number should be editable from the cooperator portal. Indeed, if the national number is required and was not provided on the first subscription, a cooperator will not be able to subscribe more shares from the website form.
- Note that since the check on the Belgian national number is enforced upon validation, it is possible to submit the request form with an invalid national number.

Bug Tracker
===========
Expand Down
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
national_number = values.get("national_number")

Check warning on line 48 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-L48

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

Check warning on line 50 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#L50

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

Check warning on line 52 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#L52

Added line #L52 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 57 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#L55-L57

Added lines #L55 - L57 were not covered by tests
else:
is_company = kwargs.get("is_company") == "on"

Check warning on line 59 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#L59

Added line #L59 was not covered by tests
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,2 +1 @@
- National number should be editable from the cooperator portal. Indeed, if the national number is required and was not provided on the first subscription, a cooperator will not be able to subscribe more shares from the website form.
- Note that since the check on the Belgian national number is enforced upon validation, it is possible to submit the request form with an invalid national number.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -388,7 +387,6 @@ <h1 class="title">Belgium: Cooperator Website National Number</h1>
<h1><a class="toc-backref" href="#toc-entry-1">Known issues / Roadmap</a></h1>
<ul class="simple">
<li>National number should be editable from the cooperator portal. Indeed, if the national number is required and was not provided on the first subscription, a cooperator will not be able to subscribe more shares from the website form.</li>
<li>Note that since the check on the Belgian national number is enforced upon validation, it is possible to submit the request form with an invalid national number.</li>
</ul>
</div>
<div class="section" id="bug-tracker">
Expand Down
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.CC"
/>
</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 14f7b78

Please sign in to comment.