From 188fc09ac1e3436a8aa851ef8c2b038369b2a4c9 Mon Sep 17 00:00:00 2001 From: docker-odoo Date: Mon, 25 Nov 2024 17:47:38 +0000 Subject: [PATCH] [MIG] portal_addresses: Migration to 18.0 --- portal_addresses/controllers/main.py | 102 +++++++++++++++++++++- portal_addresses/static/src/js/address.js | 4 +- portal_addresses/views/templates.xml | 1 - 3 files changed, 101 insertions(+), 6 deletions(-) diff --git a/portal_addresses/controllers/main.py b/portal_addresses/controllers/main.py index 349f9af78..c44d877b9 100644 --- a/portal_addresses/controllers/main.py +++ b/portal_addresses/controllers/main.py @@ -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): @@ -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 @@ -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, + }) diff --git a/portal_addresses/static/src/js/address.js b/portal_addresses/static/src/js/address.js index 6d89d9768..ae4ca566c 100644 --- a/portal_addresses/static/src/js/address.js +++ b/portal_addresses/static/src/js/address.js @@ -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 diff --git a/portal_addresses/views/templates.xml b/portal_addresses/views/templates.xml index a2cc2a260..6066d4e65 100644 --- a/portal_addresses/views/templates.xml +++ b/portal_addresses/views/templates.xml @@ -74,4 +74,3 @@ -