Skip to content

Commit

Permalink
[MIG] portal_addresses: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
docker-odoo authored and JrAdhoc committed Dec 23, 2024
1 parent cfbd2a4 commit 188fc09
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
102 changes: 100 additions & 2 deletions portal_addresses/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
import json
from odoo import http
from odoo.http import request
from odoo.addons.website_sale.controllers.main import WebsiteSale
from odoo.tools import clean_context


class WebsiteSalePortal(WebsiteSale):
Expand All @@ -29,8 +31,9 @@ def shop_address(
:rtype: str
"""
partner_id = partner_id and int(partner_id)
order_sudo = request.website.sale_get_order()

order_sudo = request.env['sale.order'].new({
'partner_id': request.env.user.partner_id.commercial_partner_id.id
})
# Retrieve the partner whose address to update, if any, and its address type.
partner_sudo, address_type = self._prepare_address_update(
order_sudo, partner_id=partner_id, address_type=address_type
Expand Down Expand Up @@ -81,3 +84,98 @@ def portal_addresses(self, **post):
return 'ok'
return request.render(
"portal_addresses.addresses", values)

@http.route(
'/portal/address/submit', type='http', methods=['POST'], auth='public', website=True,
sitemap=False
)
def shop_address_submit(
self, partner_id=None, address_type='billing', use_delivery_as_billing=None, callback=None,
required_fields=None, **form_data
):
""" Create or update an address.
If it succeeds, it returns the URL to redirect (client-side) to. If it fails (missing or
invalid information), it highlights the problematic form input with the appropriate error
message.
:param str partner_id: The partner whose address to update with the address form, if any.
:param str address_type: The type of the address: 'billing' or 'delivery'.
:param str use_delivery_as_billing: Whether the provided address should be used as both the
billing and the delivery address. 'true' or 'false'.
:param str callback: The URL to redirect to in case of successful address creation/update.
:param str required_fields: The additional required address values, as a comma-separated
list of `res.partner` fields.
:param dict form_data: The form data to process as address values.
:return: A JSON-encoded feedback, with either the success URL or an error message.
:rtype: str
"""
order_sudo = request.env['sale.order'].new({
'partner_id': request.env.user.partner_id.commercial_partner_id.id
})
use_delivery_as_billing = False
partner_sudo, address_type = self._prepare_address_update(
order_sudo, partner_id=partner_id and int(partner_id), address_type=address_type
)
# Parse form data into address values, and extract incompatible data as extra form data.
address_values, extra_form_data = self._parse_form_data(form_data)

is_main_address = order_sudo.partner_id.id == partner_sudo.id
# Validate the address values and highlights the problems in the form, if any.
invalid_fields, missing_fields, error_messages = self._validate_address_values(
address_values,
partner_sudo,
address_type,
use_delivery_as_billing,
required_fields,
is_main_address=is_main_address,
**extra_form_data,
)
if error_messages:
return json.dumps({
'invalid_fields': list(invalid_fields | missing_fields),
'messages': error_messages,
})

is_new_address = False
if not partner_sudo: # Creation of a new address.
is_new_address = True
self._complete_address_values(
address_values, address_type, use_delivery_as_billing, order_sudo
)
create_context = clean_context(request.env.context)
create_context.update({
'tracking_disable': True,
'no_vat_validation': True, # Already verified in _validate_address_values
})
partner_sudo = request.env['res.partner'].sudo().with_context(
create_context
).create(address_values)
elif not self._are_same_addresses(address_values, partner_sudo):
partner_sudo.write(address_values) # Keep the same partner if nothing changed.

partner_fnames = set()
if is_main_address: # Main address updated.
partner_fnames.add('partner_id') # Force the re-computation of partner-based fields.

if address_type == 'billing':
partner_fnames.add('partner_invoice_id')
if is_new_address and order_sudo.only_services:
# The delivery address is required to make the order.
partner_fnames.add('partner_shipping_id')
callback = callback or self._get_extra_billing_info_route(order_sudo)
elif address_type == 'delivery':
partner_fnames.add('partner_shipping_id')
if use_delivery_as_billing:
partner_fnames.add('partner_invoice_id')

if is_new_address or order_sudo.only_services:
callback = callback or '/shop/checkout?try_skip_step=true'
else:
callback = callback or '/shop/checkout'

self._handle_extra_form_data(extra_form_data, address_values)

return json.dumps({
'successUrl': callback,
})
4 changes: 1 addition & 3 deletions portal_addresses/static/src/js/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ websiteSaleAddress.include({
if (!ev.defaultPrevented && !submitButton.disabled) {
ev.preventDefault();
if(ev.currentTarget.closest('form').action.includes('portal/address') ){
debugger;
submitButton.disabled = true;
const spinner = document.createElement('span');
spinner.classList.add('fa', 'fa-cog', 'fa-spin');
submitButton.appendChild(spinner);

const result = await this.http.post(
'/shop/address/submit',
'/portal/address/submit',
new FormData(this.addressForm),
)
if (result.successUrl) {
debugger;
window.location = '/portal/addresses';
} else {
// Highlight missing/invalid form values
Expand Down
1 change: 0 additions & 1 deletion portal_addresses/views/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,3 @@
</template>

</odoo>

0 comments on commit 188fc09

Please sign in to comment.