Skip to content

Commit

Permalink
[FIX] website_portal_address: filter state by country
Browse files Browse the repository at this point in the history
- fix hidden address when choose contact
  • Loading branch information
mathben committed Mar 9, 2020
1 parent a8a5053 commit 9f96122
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
42 changes: 34 additions & 8 deletions website_portal_address/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Copyright 2016-Today Serpent Consulting Services Pvt. Ltd.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

import logging

from odoo.http import request, route
from odoo.addons.website_portal_contact.controllers.main import (
WebsiteAccount as PortalController,
)
from odoo.addons.website_portal_contact.controllers.main import WebsiteAccount

_logger = logging.getLogger(__name__)


class WebsiteAccount(PortalController):
class WebsiteAccount(WebsiteAccount):
@route(
"/my/contacts/<model('res.partner'):contact>",
auth="user",
website=True,
)
def portal_my_contacts_read(self, contact):
values = self._prepare_portal_layout_values()
def portal_my_contacts_read(self, contact, access_token=None, **kw):
values = self._contact_get_page_view_values(contact, access_token, **kw)
values.update(
{
"state_id": request.env["res.country.state"].sudo().search([]),
"country_id": request.env["res.country"].sudo().search([]),
"contact": contact,
"fields": self._contacts_fields(),
}
)
return request.render(
Expand All @@ -39,3 +39,29 @@ def _contacts_fields(self):
"state_id",
]
return res

def _contacts_clean_values(self, values, contact=False):
"""Set values to a write-compatible format"""
if "state_id" not in values:
# Force erase the data
values["state_id"] = False
elif values["state_id"]:
try:
values["state_id"] = int(values["state_id"].split('-', 1)[1])
except ValueError:
_logger.warning('Cannot parse "state_id" : %s' % (values["state_id"]))

if "country_id" in values and values["country_id"]:
try:
values["country_id"] = int(values["country_id"])
except ValueError:
_logger.warning(
'Cannot parse "country_id" : %s' % (values["country_id"]))

result = {k: v or False for k, v in values.items()}
result.setdefault("type", "contact")
if not contact or contact.id != request.env.user.commercial_partner_id.id:
result.setdefault(
"parent_id", request.env.user.commercial_partner_id.id
)
return result
49 changes: 39 additions & 10 deletions website_portal_address/static/src/js/portal_address.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
/* Copyright 2012-Today Serpent Consulting Services PVT. LTD. (http://www.serpentcs.com)
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
odoo.define("website_portal_contact.tour", function (require) {
odoo.define("website_portal_address.website_portal_address", function (require) {
"use strict";
var ajax = require("web.ajax");
$(document).ready(function() {
$('input[type="radio"]').click(function() {
if($(this).attr("value") === "contact") {
$(".contact_address_website").addClass('hidden');
}
else {
$(".contact_address_website").removeClass('hidden');
$(document).ready(function () {
var first_execution = true;

$('input[type="radio"]').click(function () {
if ($(this).attr("value") === "contact") {
$(".contact_address_website").hide();
} else {
$(".contact_address_website").show();
}
});
$('input[type="radio"]').each(function() {
if($(this).attr('checked') === 'checked') {

$('input[type="radio"]').each(function () {
if ($(this).attr('checked') === 'checked') {
$(this).click();
}
});

$('#country_id').change(function () {
var option_count = 0;
$("#state_id option").each(function () {
var country_id = parseInt($('#country_id').val());
var state_country_id = parseInt($(this).val().split("-", 1));
if (country_id == state_country_id) {
option_count++;
$(this).removeAttr("disabled").show();
} else {
$(this).attr("disabled", "disabled").hide();
}
});
// Ignore first execution, it's override t-att-selected
if (first_execution) {
first_execution = false;
} else {
$("#state_id").val($("#state_id option:first").val());
}
// Hide state part if empty
if (option_count > 0) {
$('#state_id').parent().show();
} else {
$('#state_id').parent().hide();
}
}).change();
});

});
9 changes: 5 additions & 4 deletions website_portal_address/views/contact_address.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<div class="form-group">
<div class="col-sm-5">
<label class="col-form-label" for="country_id">Country</label>
<select placeholder="Country" name="country_id"
<select placeholder="Country" name="country_id" id="country_id"
class="form-control">
<option value="">Country...</option>
<t t-foreach="country_id or []" t-as="country">
Expand All @@ -101,12 +101,13 @@
<div class="col-sm-5">
<label class="col-form-label" for="state_id">State / Province
</label>
<select placeholder="State" name="state_id"
<select placeholder="State" name="state_id" id="state_id"
class="form-control">
<option value="">State...</option>
<t t-foreach="state_id or []" t-as="state">
<option t-att-value="state.id"
t-att-selected='state.id == contact["state_id"]["id"]'>
<option
t-att-value="'%s-%s' % (state.country_id.id, state.id)"
t-att-selected='state.id == contact["state_id"]["id"]'>
<t t-esc="state.name"/>
</option>
</t>
Expand Down

0 comments on commit 9f96122

Please sign in to comment.