[IMP] pms: add form validation

This commit is contained in:
Sara Lago
2023-02-01 19:09:33 +01:00
committed by Darío Lodeiros
parent 251154ae74
commit 8af8d42772
5 changed files with 128 additions and 236 deletions

View File

@@ -449,22 +449,19 @@ class PortalPrecheckin(CustomerPortal):
checkin_partner = (
request.env["pms.checkin.partner"].sudo().browse(checkin_partner_id)
)
print(kw)
values = kw
values.update(
{
"checkin_partner": checkin_partner,
}
)
print(kw)
print(values)
# if not kw.get("first") and kw.get("checkin_pos") and not kw.get("back"):
# error, error_message = self.form_validate(kw, None)
# if not kw.get("first") and not kw.get("back") and not error:
# kw.update({"checkin_partner_id": checkin_partner_id})
if values.get("residence_state_id") == "placeholder":
values["residence_state_id"] = False
if values.get("residence_country_id") == "placeholder":
values["residence_country_id"] = False
@@ -485,7 +482,7 @@ class PortalPrecheckin(CustomerPortal):
# else:
# return request.render("pms.portal_not_checkin", values)
#request.env["pms.checkin.partner"]._save_data_from_portal(kw)
request.env["pms.checkin.partner"]._save_data_from_portal(kw)
folio = request.env["pms.folio"].sudo().browse(folio_id)
reservation = request.env["pms.reservation"].sudo().browse(reservation_id)
@@ -524,137 +521,6 @@ class PortalPrecheckin(CustomerPortal):
values.update({"no_breadcrumbs": True, "error": {}, "web_url": web_url.value})
return request.render("pms.portal_my_folio_invitations", values)
def form_validate(self, data, counter):
error, error_message = {}, {}
if data.get("checkin_pos") != "-1":
error, error_message = self.form_document_validate(data, counter)
mobile = "mobile"
if data.get("mobile"):
if not re.match(
r"^(\d{3}[\-\s]?\d{2}[\-\s]?\d{2}[\-\s]?\d{2}[\-\s]?|"
r"\d{3}[\-\s]?\d{3}[\-\s]?\d{3})$",
data[mobile],
):
error[mobile] = "error"
error_message[mobile] = "Invalid phone"
email = "email"
if data.get("email") and not tools.single_email_re.match(data.get("email")):
error[email] = "error"
error_message[email] = "Email format is wrong"
if not data.get("document_number"):
error["document_number"] = "error"
error_message["document_number"] = "Document number is mandatory"
if not data.get("document_type"):
error["document_type"] = "error"
error_message["document_type"] = "Document type is mandatory"
if not data.get("document_expedition_date"):
error["document_expedition_date"] = "error"
error_message[
"document_expedition_date"
] = "Document expedition date is mandatory"
if not data.get("birthdate_date"):
error["birthdate_date"] = "error"
error_message["birthdate_date"] = "Birth date is mandatory"
if not data.get("nationality_id"):
error["nationality_id"] = "error"
error_message["nationality_id"] = "Nationality is mandatory"
if (
not data.get("residence_street")
or not data.get("residence_city")
or not data.get("residence_zip")
or data.get("residence_country_id") == "placeholder"
or data.get("residence_state_id") == "placeholder"
):
error["address"] = "error"
error_message["address"] = "Address data is mandatory"
return error, error_message
def form_document_validate(self, data, counter):
error = dict()
error_message = {}
data.keys()
document_number = "document_number"
document_type = "document_type"
document_expedition_date = "document_expedition_date"
if data.get("document_expedition_date") and not data.get("document_number"):
error[document_expedition_date] = "error"
error_message[
document_expedition_date
] = "Document Number not entered and Document Type is not selected"
if data.get("document_number"):
if not data[document_type]:
error[document_type] = "error"
error_message[document_type] = "Document Type is not selected"
if data[document_type] == "D":
if (
len(data.get("document_number")) == 9
or len(data.get("document_number")) == 10
):
if not re.match(
r"^\d{8}[ -]?[a-zA-Z]$", data.get("document_number")
):
error[document_number] = "error"
error_message[document_number] = "The DNI format is wrong"
letters = {
0: "T",
1: "R",
2: "W",
3: "A",
4: "G",
5: "M",
6: "Y",
7: "F",
8: "P",
9: "D",
10: "X",
11: "B",
12: "N",
13: "J",
14: "Z",
15: "S",
16: "Q",
17: "V",
18: "H",
19: "L",
20: "C",
21: "K",
22: "E",
}
dni_number = data.get("document_number")[0:8]
dni_letter = data.get("document_number")[
len(data.get("document_number"))
- 1 : len(data.get("document_number"))
]
if letters.get(int(dni_number) % 23) != dni_letter.upper():
error[document_number] = "error"
error_message[document_number] = "DNI format is invalid"
else:
error[document_number] = "error"
error_message[document_number] = "DNI is invalid"
if data[document_type] == "C" and not re.match(
r"^\d{8}[ -]?[a-zA-Z]$", data.get("document_number")
):
error[document_number] = "error"
error_message[document_number] = "The Driving License format is wrong"
if data[document_type] == "N" and not re.match(
r"^[X|Y]{1}[ -]?\d{7,8}[ -]?[a-zA-Z]$", data.get("document_number")
):
error[document_number] = "error"
error_message[
document_number
] = "The Spanish Residence Permit format is wrong"
if data[document_type] == "X" and not re.match(
r"^[X|Y]{1}[ -]?\d{7,8}[ -]?[a-zA-Z]$", data.get("document_number")
):
error[document_number] = "error"
error_message[
document_number
] = "The European Residence Permit format is wrong"
elif data.get("document_type"):
error[document_number] = "error"
error_message[document_number] = "Document Number not entered"
return error, error_message
@http.route(
["/my/precheckin/send_invitation"],
auth="public",

View File

@@ -799,6 +799,7 @@ class PmsCheckinPartner(models.Model):
):
today = fields.datetime.today()
datetime_doc_date = datetime.strptime(doc_date, DEFAULT_SERVER_DATE_FORMAT)
print(datetime_doc_date)
if datetime_doc_date < today:
return datetime_doc_date
datetime_birthdate = datetime.strptime(birthdate, DEFAULT_SERVER_DATE_FORMAT)
@@ -879,25 +880,31 @@ class PmsCheckinPartner(models.Model):
values.pop("checkin_partner")
if values.get("nationality"):
values.update({"nationality_id": int(values.get("nationality_id"))})
else:
values.update({"nationality_id": False})
if not values.get("document_type"):
values.update({"document_type": False})
else:
doc_type_name = values.get("document_type")
doc_type = (
self.sudo()
.env["res.partner.id_category"]
.search([("name", "=", doc_type_name)])
)
values.update({"document_type": doc_type.id})
doc_type = (
self.sudo()
.env["res.partner.id_category"]
.browse(int(values.get("document_type")))
)
if values.get("document_type"):
values.update({"document_type": int(values.get("document_type"))})
if values.get("residence_state_id"):
values.update({"residence_state_id": int(values.get("residence_state_id"))})
if values.get("residence_country_id"):
values.update(
{"residence_country_id": int(values.get("residence_country_id"))}
)
if values.get("document_expedition_date"):
values.update(
{
"document_expedition_date":
datetime.strptime(values.get("document_expedition_date"), "%d/%m/%Y").strftime("%Y-%m-%d"),
"birthdate_date":
datetime.strptime(values.get("birthdate_date"), "%d/%m/%Y").strftime("%Y-%m-%d"),
}
)
doc_date = values.get("document_expedition_date")
birthdate = values.get("birthdate_date")
document_expedition_date = (

View File

@@ -222,18 +222,18 @@
class="form-control"
name='document_type'
id="doc_type"
t-attf-onchange="removeErrorClass(this)"
>
<option value="">Select an option</option>
<t t-foreach="doc_type_ids" t-as='doc_type'>
<option
t-att-value="doc_type.name"
t-att-selected="doc_type.name == document_type"
t-att-value="doc_type.id"
>
<t t-esc='doc_type.name' />
</option>
</t>
</select>
<span id="document_type_error" class="d-none text-danger">Doc. Type is a required field</span>
</div>
<div
name="document_number_div"
@@ -246,10 +246,13 @@
<input
type="text"
name="document_number"
id="document_number"
t-attf-class="form-control"
maxlength="14"
t-attf-oninput="removeErrorClass(this)"
/>
<span id="docNumber-error" class="d-none text-danger">Doc. Number is a required field</span>
<span id="docNumberFormat-error" class="d-none text-danger">Doc. Number Format is wrong</span>
</div>
<div t-attf-class="form-group col-12 col-md-6">
@@ -281,7 +284,7 @@
> Birth Date *</label>
<input
type="text"
name="birthdate"
name="birthdate_date"
id="birthdateId"
t-attf-class="form-control"
data-date-format="dd/mm/yyyy"
@@ -291,6 +294,12 @@
<span id="birthdate-error" class="d-none text-danger">Birthdate cannot be older than today</span>
</div>
<div class="d-none" id="countryDiv">
<t t-foreach="country_ids" t-as='country_id'>
<span t-att-id="country_id.name" t-att-country_name="country_id.name" t-att-country_id="country_id.id"/>
</t>
</div>
<input class="d-none" id="nationality_id" name="nationality_id"/>
<div t-attf-class="form-group col-12 col-md-6">
<label
class="col-form-label"
@@ -319,6 +328,7 @@
<input
type="phone"
name="mobile"
id="mobileInput"
t-attf-class="form-control"
/>
@@ -332,6 +342,7 @@
<input
type="phone"
name="phone"
id="phoneInput"
t-attf-class="form-control"
/>
@@ -345,8 +356,11 @@
<input
type="email"
name="email"
id="emailInput"
t-attf-class="form-control"
t-attf-oninput="removeErrorClass(this)"
/>
<span id="email-error" class="d-none text-danger">Email format is incorrect</span>
</div>
<div t-attf-class="form-group col-md-12 pt-md-5">
@@ -358,8 +372,11 @@
type="text"
placeholder="Street"
name="residence_street"
id="residence_street"
t-attf-class="form-control"
t-attf-oninput="removeErrorClass(this)"
/>
<span id="street-error" class="d-none text-danger">Residence Address is a required field</span>
</div>
<div t-attf-class="form-group col-md-12">
<input
@@ -373,15 +390,17 @@
<div class="dropdown">
<input
name="residence_city_zip"
name="residence_zip"
class="dropdown-input"
id="myZipInput"
t-attf-class="form-control"
type="text"
placeholder="Type your zip code here" id="myZipInput"
placeholder="Type your zip code here"
onclick="showZips(true)"
oninput="filterZips()"
autocomplete="off"
/>
<span id="zip-error" class="d-none text-danger">Zip is a required field</span>
<div id="myZipDropdown" class="dropdown-content d-none">
<t t-foreach="zip_ids" t-as='zip_id'>
@@ -406,14 +425,16 @@
type="text"
placeholder="City"
name="residence_city"
id="residence-city"
id="residence_city"
t-attf-class="form-control"
t-attf-oninput="removeErrorClass(this)"
/>
<span id="city-error" class="d-none text-danger">Residence City is a required field</span>
</div>
<input class="d-none" id="residence_country_id" name="residence_country_id"/>
<div t-attf-class="form-group col-12 col-md-6">
<div class="dropdown">
<input
name="residence_country_id"
class="dropdown-input"
t-attf-class="form-control"
type="text"
@@ -434,10 +455,10 @@
</div>
</div>
</div>
<input class="d-none" id="residence_state_id" name="residence_state_id"/>
<div t-attf-class="form-group col-12 col-md-6 pb-md-3">
<div class="dropdown">
<input
name="residence_state_id"
class="dropdown-input"
t-attf-class="form-control"
type="text"
@@ -555,11 +576,6 @@
var folio_id = document.getElementById("folio").value
var checkin_partner_id = document.getElementById("checkin").value
var access_token = document.getElementById("input_access_token").value
var checkin_pos = document.getElementById("input_checkin_pos").value;
<!-- TODO: add reservation_id in url -->
window.history.pushState(null, '', '/my/precheckin/'+folio_id+'/checkin/'+checkin_partner_id +'?access_token='+ access_token);
function showNationCountries(show){
if(show){
@@ -570,8 +586,6 @@
}
function showCountries(show){
if(show){
document.getElementById("myCountryDropdown").classList.remove("d-none");
@@ -674,18 +688,27 @@
function selectNationality(element){
var nationality = element.innerText;
document.getElementById("myInput").value = nationality;
var nationalityId = document.getElementById(nationality).getAttribute("country_id");
var nationalityInput = document.getElementById("nationality_id");
nationalityInput.value = nationalityId;
this.showNationCountries(false);
}
function selectCountry(element){
var country = element.innerText;
document.getElementById("myCountryInput").value = country.trim();
var countryInput = document.getElementById("residence_country_id");
countryInput.value = element.id;
this.showCountries(false);
this.filterStatesByCountry(element.id);
}
function selectState(element){
var state = element.innerText;
document.getElementById("myStateInput").value = state.trim();
var stateInput = document.getElementById("residence_state_id");
stateInput.value = element.id;
this.showStates(false);
}
function completeAddressData(element){
@@ -698,16 +721,21 @@
Array.from(document.getElementById("myCountryDropdown").getElementsByTagName("a")).forEach(element => {
if (element.id == country_id) {
element.click(element);
var countryInput = document.getElementById("residence_country_id");
countryInput.value = country_id;
}
});
Array.from(document.getElementById("myStateDropdown").getElementsByTagName("a")).forEach(element => {
if (element.id == state_id) {
element.click(element);
element.click(element);
var stateInput = document.getElementById("residence_state_id");
stateInput.value = state_id;
}
});
document.getElementById('residence-city').value = city_name;
document.getElementById('residence_city').value = city_name;
this.showZips(false);
}
function changeDatepickerArrow(element){
@@ -731,16 +759,20 @@
element.nextElementSibling.classList.add("d-none");
}
function validateAndSend(event){
var send = true;
var firstname = document.getElementById("firstname");
var lastname = document.getElementById("lastname");
if(!firstname.value){
firstname.classList.add("error");
document.getElementById("name-error").classList.remove("d-none");
send = false;
}
if( !lastname.value){
lastname.classList.add("error");
document.getElementById("lastname-error").classList.remove("d-none");
send = false;
}
@@ -748,10 +780,21 @@
if(!gender.value){
gender.classList.add("error");
document.getElementById("name-error").classList.remove("d-none");
send = false;
}
var document_type = document.getElementById("doc_type");
var document_number = document.getElementById("document_number");
if(!document_type.value){
document.getElementById("doc_type").classList.add("error");
document.getElementById("document_type_error").classList.remove("d-none");
send = false;
}
if(!document_number.value){
document.getElementById("document_number").classList.add("error");
document.getElementById("docNumber-error").classList.remove("d-none");
send = false;
}
<!-- var document_type = document.getElementById("document_type").value;-->
<!-- var document_number = document.getElementById("document_number").value;-->
<!-- var document_expedition_date = document.getElementById("document_expedition_date").value;-->
var birthdate = document.getElementById("birthdateId").value;
var birthdateParts = birthdate.split("/");
var birthdateDate = new Date(birthdateParts[2], birthdateParts[1]-1, birthdateParts[0]);
@@ -759,37 +802,58 @@
if (todayDate.getTime() &lt;= birthdateDate.getTime()){
document.getElementById("birthdateId").classList.add("error");
document.getElementById("birthdate-error").classList.remove("d-none");
send = false;
}
var nationality = document.getElementById("myInput").value;
var lastname2 = document.getElementById("lastname2").value;
var nationalityId = document.getElementById(nationality)
var nationalityId = document.getElementById(nationality).getAttribute("country_id");
if(nationalityId){
nationalityId = nationalityId.getAttribute("country_id");
}
if(!nationalityId){
document.getElementById("myInput").classList.add("error");
document.getElementById("nationalityError").classList.remove("d-none");
send = false;
}else{
if(nationalityId == 68){
if(!lastname2){
document.getElementById("myInput").classList.add("error");
document.getElementById("lastname2").classList.add("error");
document.getElementById("nationalitySpainError").classList.remove("d-none");
document.getElementById("sndlastnameError").classList.remove("d-none");
if(!lastname2){
document.getElementById("myInput").classList.add("error");
document.getElementById("lastname2").classList.add("error");
document.getElementById("nationalitySpainError").classList.remove("d-none");
document.getElementById("sndlastnameError").classList.remove("d-none");
send = false;
}
}
}
<!-- var mobile = document.getElementById("mobile").value;-->
<!-- var phone = document.getElementById("phone").value;-->
<!-- var email = document.getElementById("email").value;-->
<!-- var residence_street = document.getElementById("residence_street").value;-->
<!-- var residence_street2 = document.getElementById("residence_street2").value;-->
<!-- var residence_city_zip = document.getElementById("residence_city_zip").value;-->
<!-- var residence_city = document.getElementById("residence_city").value;-->
<!-- var residence_country_id = document.getElementById("residence_country_id").value;-->
<!-- var residence_state_id = document.getElementById("residence_state_id").value;-->
var mobile = document.getElementById("mobileInput").value;
var phone = document.getElementById("phoneInput").value;
var email = document.getElementById("emailInput").value;
let regex = /^[a-zA-Z0-9.!#$%&amp;*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
if(email){
if(!regex.test(email)){
document.getElementById("emailInput").classList.add("error");
document.getElementById("email-error").classList.remove("d-none");
send = false;
}
}
var residence_street = document.getElementById("residence_street");
if(!residence_street.value){
document.getElementById("residence_street").classList.add("error");
document.getElementById("street-error").classList.remove("d-none");
send = false;
}
<!-- document.getElementById("checkinForm").submit();-->
var residence_city = document.getElementById("residence_city");
if(!residence_city.value){
document.getElementById("residence_city").classList.add("error");
document.getElementById("city-error").classList.remove("d-none");
send = false;
}
if(send){
document.getElementById("checkinForm").submit();
}
}
</script>
</template>

View File

@@ -42,7 +42,6 @@
"views/pms_ine_tourism_type_category.xml",
"views/res_partner_id_number_view.xml",
"views/pms_checkin_partner_views.xml",
"views/precheckin_portal_templates.xml",
"wizards/traveller_report.xml",
"wizards/wizard_ine.xml",
],

View File

@@ -1,44 +0,0 @@
<odoo>
<template
id="inherit_pms_l10n_es_portal_my_precheckin_detail"
inherit_id="pms.portal_my_precheckin_detail"
>
<xpath expr="//div[@name='document_type_div']" position="replace">
<div t-attf-class="form-group col-md-6">
<label class="col-form-label" for="document_type">Doc. Type</label>
<div class="d-none"><p id="docTypeId"><t
t-esc="checkin_partner.document_type"
/></p></div>
<select class="form-control" name='document_type' id="doc_type">
<option value="">Select an option</option>
<t t-foreach="doc_type_ids" t-as='doc_type'>
<option
t-att-value="doc_type.name"
t-att-selected="doc_type.name == document_type"
>
<t t-esc='doc_type.name' />
</option>
</t>
</select>
<!--<t t-if="error_message">
<span
class="text-danger"
t-esc="error_message.get('document_type')"
/>
</t>-->
</div>
</xpath>
<xpath expr="//div[@name='document_number_div']" position="replace">
<div t-attf-class="col-12 col-md-6">
<label class="col-form-label" for="document_number">Doc. Number</label>
<input type="text" name="document_number" t-attf-class="form-control" />
<!--<t t-if="error_message">
<span
class="text-danger"
t-esc="error_message.get('document_number')"
/>
</t>-->
</div>
</xpath>
</template>
</odoo>