From 7c56a3208ccaeb2971541f8decc890bd4b29dae9 Mon Sep 17 00:00:00 2001 From: David Moreno Date: Fri, 5 Jul 2024 16:56:01 +0200 Subject: [PATCH] [MIG] delivery_postlogistics: Migration to 17.0 --- delivery_postlogistics/__manifest__.py | 2 +- .../models/delivery_carrier.py | 23 ++++++++++---- .../models/stock_picking.py | 6 ++-- .../models/stock_quant_package.py | 4 +-- .../postlogistics/web_service.py | 20 ++++++------- delivery_postlogistics/tests/common.py | 10 ++++++- .../cassettes/test_missing_language.yaml | 6 ++-- .../fixtures/cassettes/test_store_label.yaml | 6 ++-- delivery_postlogistics/views/delivery.xml | 30 ++++++++++++------- .../views/postlogistics_license.xml | 2 +- delivery_postlogistics/views/stock.xml | 9 ++++-- .../views/stock_package_type_view.xml | 11 ++++--- test-requirements.txt | 2 ++ 13 files changed, 83 insertions(+), 48 deletions(-) create mode 100644 test-requirements.txt diff --git a/delivery_postlogistics/__manifest__.py b/delivery_postlogistics/__manifest__.py index c0a72e8028..d4c4d7cd48 100644 --- a/delivery_postlogistics/__manifest__.py +++ b/delivery_postlogistics/__manifest__.py @@ -9,7 +9,7 @@ "license": "AGPL-3", "category": "Delivery", "complexity": "normal", - "depends": ["delivery", "mail", "base", "stock"], + "depends": ["mail", "base", "stock_delivery"], "website": "https://github.com/OCA/delivery-carrier", "data": [ "security/ir.model.access.csv", diff --git a/delivery_postlogistics/models/delivery_carrier.py b/delivery_postlogistics/models/delivery_carrier.py index 29c83b5dbd..d29f7009ef 100644 --- a/delivery_postlogistics/models/delivery_carrier.py +++ b/delivery_postlogistics/models/delivery_carrier.py @@ -19,10 +19,14 @@ class DeliveryCarrier(models.Model): postlogistics_default_package_type_id = fields.Many2one( "stock.package.type", domain=[("package_carrier_type", "=", "postlogistics")] ) - + postlogistics_token_url = fields.Char( + string="Endpoint Token", + default="https://api-int.post.ch", + required=True, + ) postlogistics_endpoint_url = fields.Char( string="Endpoint URL", - default="https://wedecint.post.ch/", + default="https://dcapi.apis-int.post.ch", required=True, ) postlogistics_client_id = fields.Char( @@ -31,6 +35,11 @@ class DeliveryCarrier(models.Model): postlogistics_client_secret = fields.Char( string="Client Secret", groups="base.group_system" ) + postlogistics_scope = fields.Char( + string="Scope", + default="DCAPI_BARCODE_READ", + groups="base.group_system", + ) postlogistics_logo = fields.Binary( string="Company Logo on Post labels", help="Optional company logo to show on label.\n" @@ -102,14 +111,16 @@ class DeliveryCarrier(models.Model): def onchange_prod_environment(self): """ Auto change the end point url following the environment - - Test: https://wedecint.post.ch/ - - Prod: https://wedec.post.ch/ + - Test: https://api-int.post.ch/ and https://dcapi.apis.post.ch/ + - Prod: https://api.post.ch/ and https://dcapi.apis-int.post.ch/ """ for carrier in self: if carrier.prod_environment: - carrier.postlogistics_endpoint_url = "https://wedec.post.ch/" + carrier.postlogistics_token_url = "https://api.post.ch/" + carrier.postlogistics_endpoint_url = "https://dcapi.apis.post.ch/" else: - carrier.postlogistics_endpoint_url = "https://wedecint.post.ch/" + carrier.postlogistics_token_url = "https://api-int.post.ch/" + carrier.postlogistics_endpoint_url = "https://dcapi.apis-int.post.ch/" def postlogistics_get_tracking_link(self, picking): return ( diff --git a/delivery_postlogistics/models/stock_picking.py b/delivery_postlogistics/models/stock_picking.py index e2f2223f30..f971a6d0c3 100644 --- a/delivery_postlogistics/models/stock_picking.py +++ b/delivery_postlogistics/models/stock_picking.py @@ -109,7 +109,7 @@ def postlogistics_cod_amount(self): if not order: return 0.0 if len(order) > 1: - raise exceptions.Warning( + raise exceptions.UserError( _( "The cash on delivery amount must be manually specified " "on the packages when a package contains products " @@ -118,7 +118,7 @@ def postlogistics_cod_amount(self): ) # check if the package delivers the whole sales order if len(order.picking_ids) > 1: - raise exceptions.Warning( + raise exceptions.UserError( _( "The cash on delivery amount must be manually specified " "on the packages when a sales order is delivered " @@ -229,7 +229,7 @@ def _generate_postlogistics_label( # This ensures the label pushed recored correctly in Odoo self._cr.commit() # pylint: disable=invalid-commit error_message = "\n".join(label["errors"] for label in failed_label_results) - raise exceptions.Warning(error_message) + raise exceptions.UserError(error_message) return labels def generate_postlogistics_shipping_labels(self, package_ids=None): diff --git a/delivery_postlogistics/models/stock_quant_package.py b/delivery_postlogistics/models/stock_quant_package.py index fe98a66aad..0c8d32752e 100644 --- a/delivery_postlogistics/models/stock_quant_package.py +++ b/delivery_postlogistics/models/stock_quant_package.py @@ -42,7 +42,7 @@ def postlogistics_cod_amount(self): pickings = self._get_origin_pickings() if len(pickings) > 1: - raise exceptions.Warning( + raise exceptions.UserError( _( "The cash on delivery amount must be manually specified " "on the packages when a sales order is delivered " @@ -54,7 +54,7 @@ def postlogistics_cod_amount(self): if not order: return 0.0 if len(order) > 1: - raise exceptions.Warning( + raise exceptions.UserError( _( "The cash on delivery amount must be manually specified " "on the packages when a package contains products " diff --git a/delivery_postlogistics/postlogistics/web_service.py b/delivery_postlogistics/postlogistics/web_service.py index 73500d6ef8..39b34c9e72 100644 --- a/delivery_postlogistics/postlogistics/web_service.py +++ b/delivery_postlogistics/postlogistics/web_service.py @@ -19,8 +19,8 @@ _compile_itemid = re.compile(r"[^0-9A-Za-z+\-_]") _compile_itemnum = re.compile(r"[^0-9]") -AUTH_PATH = "/WEDECOAuth/token" -GENERATE_LABEL_PATH = "/api/barcode/v1/generateAddressLabel" +AUTH_PATH = "/OAuth/token" +GENERATE_LABEL_PATH = "/barcode/v1/generateAddressLabel" DISALLOWED_CHARS_MAPPING = { "|": "", @@ -38,9 +38,8 @@ class PostlogisticsWebService: Handbook available here: https://developer.post.ch/en/digital-commerce-api - https://wedec.post.ch/doc/swagger/index.html? - url=https://wedec.post.ch/doc/api/barcode/v1/swagger.yaml - #/Barcode/generateAddressLabel + https://developer.apis.post.ch/ui/apis/5cff6ab7-8325-4a05-bf6a-b783256a0552 + /pages/50fa2b65-2f67-4867-ba2b-652f6738676d Allows to generate labels @@ -415,7 +414,7 @@ def _prepare_data( @classmethod def _request_access_token(cls, delivery_carrier): - if not delivery_carrier.postlogistics_endpoint_url: + if not delivery_carrier.postlogistics_token_url: raise exceptions.UserError( _( "Missing Configuration\n\n" @@ -426,15 +425,16 @@ def _request_access_token(cls, delivery_carrier): client_id = delivery_carrier.postlogistics_client_id client_secret = delivery_carrier.postlogistics_client_secret + scope = delivery_carrier.postlogistics_scope authentication_url = urllib.parse.urljoin( - delivery_carrier.postlogistics_endpoint_url or "", AUTH_PATH + delivery_carrier.postlogistics_token_url or "", AUTH_PATH ) - if not (client_id and client_secret): + if not (client_id and client_secret and scope): raise exceptions.UserError( _( "Authorization Required\n\n" - "Please verify postlogistics client id and secret in:\n" + "Please verify postlogistics client id, secret and scope in:\n" "Delivery Carrier (PostLogistics)." ) ) @@ -446,7 +446,7 @@ def _request_access_token(cls, delivery_carrier): "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret, - "scope": "WEDEC_BARCODE_READ", + "scope": scope, }, timeout=60, ) diff --git a/delivery_postlogistics/tests/common.py b/delivery_postlogistics/tests/common.py index b37806c5bf..e846d391d2 100644 --- a/delivery_postlogistics/tests/common.py +++ b/delivery_postlogistics/tests/common.py @@ -1,11 +1,13 @@ # Copyright 2021 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) +import requests + from odoo.tests.common import TransactionCase from ..postlogistics.web_service import PostlogisticsWebService -ENDPOINT_URL = "https://wedecint.post.ch/" +ENDPOINT_URL = "https://api-int.post.ch/" CLIENT_ID = "XXX" CLIENT_SECRET = "XXX" LICENSE = "XXX" @@ -114,8 +116,14 @@ def create_picking(cls, partner=None, product_matrix=None): def setUpClassWebservice(cls): cls.service_class = PostlogisticsWebService(cls.env.user.company_id) + @classmethod + def _request_handler(cls, s, r, /, **kw): + """Don't block external requests.""" + return cls._super_send(s, r, **kw) + @classmethod def setUpClass(cls): + cls._super_send = requests.Session.send super().setUpClass() cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) cls.setUpClassLicense() diff --git a/delivery_postlogistics/tests/fixtures/cassettes/test_missing_language.yaml b/delivery_postlogistics/tests/fixtures/cassettes/test_missing_language.yaml index 3f4ac4fd64..3eb2c75807 100644 --- a/delivery_postlogistics/tests/fixtures/cassettes/test_missing_language.yaml +++ b/delivery_postlogistics/tests/fixtures/cassettes/test_missing_language.yaml @@ -1,6 +1,6 @@ interactions: - request: - body: scope=WEDEC_BARCODE_READ&client_secret=c70e3696ae5146e7fe317434e50a90cd&grant_type=client_credentials&client_id=865783331e39e91e633c3916fe892d92 + body: scope=DCAPI_BARCODE_READ&client_secret=c70e3696ae5146e7fe317434e50a90cd&grant_type=client_credentials&client_id=865783331e39e91e633c3916fe892d92 headers: Accept: - "*/*" @@ -15,7 +15,7 @@ interactions: content-type: - application/x-www-form-urlencoded method: POST - uri: https://wedecint.post.ch/WEDECOAuth/token + uri: https://api-int.post.ch/OAuth/token response: body: string: '{"access_token":"XXX","token_type":"Bearer","expires_in":60}' @@ -67,7 +67,7 @@ interactions: content-type: - application/json method: POST - uri: https://wedecint.post.ch/api/barcode/v1/generateAddressLabel + uri: https://dcapi.apis-int.post.ch/barcode/v1/generateAddressLabel response: body: string: "{\n \"labelDefinition\" : {\n \"labelLayout\" : \"A6\",\n \"\ diff --git a/delivery_postlogistics/tests/fixtures/cassettes/test_store_label.yaml b/delivery_postlogistics/tests/fixtures/cassettes/test_store_label.yaml index 28e207216e..684c442c83 100644 --- a/delivery_postlogistics/tests/fixtures/cassettes/test_store_label.yaml +++ b/delivery_postlogistics/tests/fixtures/cassettes/test_store_label.yaml @@ -1,6 +1,6 @@ interactions: - request: - body: scope=WEDEC_BARCODE_READ&client_secret=c70e3696ae5146e7fe317434e50a90cd&grant_type=client_credentials&client_id=865783331e39e91e633c3916fe892d92 + body: scope=DCAPI_BARCODE_READ&client_secret=c70e3696ae5146e7fe317434e50a90cd&grant_type=client_credentials&client_id=865783331e39e91e633c3916fe892d92 headers: Accept: - "*/*" @@ -15,7 +15,7 @@ interactions: content-type: - application/x-www-form-urlencoded method: POST - uri: https://wedecint.post.ch/WEDECOAuth/token + uri: https://api-int.post.ch/OAuth/token response: body: string: '{"access_token":"XXX","token_type":"Bearer","expires_in":60}' @@ -67,7 +67,7 @@ interactions: content-type: - application/json method: POST - uri: https://wedecint.post.ch/api/barcode/v1/generateAddressLabel + uri: https://dcapi.apis-int.post.ch/barcode/v1/generateAddressLabel response: body: string: "{\n \"labelDefinition\" : {\n \"labelLayout\" : \"A6\",\n \"\ diff --git a/delivery_postlogistics/views/delivery.xml b/delivery_postlogistics/views/delivery.xml index afc296a87d..169c5601a8 100644 --- a/delivery_postlogistics/views/delivery.xml +++ b/delivery_postlogistics/views/delivery.xml @@ -11,21 +11,29 @@ + +