Skip to content

Commit

Permalink
[MIG] delivery_postlogistics: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BT-dmoreno committed Jul 16, 2024
1 parent aafff63 commit 7c56a32
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 48 deletions.
2 changes: 1 addition & 1 deletion delivery_postlogistics/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 17 additions & 6 deletions delivery_postlogistics/models/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"
Expand Down Expand Up @@ -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 (
Expand Down
6 changes: 3 additions & 3 deletions delivery_postlogistics/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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 "
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions delivery_postlogistics/models/stock_quant_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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 "
Expand Down
20 changes: 10 additions & 10 deletions delivery_postlogistics/postlogistics/web_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
"|": "",
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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)."
)
)
Expand All @@ -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,
)
Expand Down
10 changes: 9 additions & 1 deletion delivery_postlogistics/tests/common.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
- "*/*"
Expand All @@ -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}'
Expand Down Expand Up @@ -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 \"\
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
- "*/*"
Expand All @@ -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}'
Expand Down Expand Up @@ -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 \"\
Expand Down
30 changes: 19 additions & 11 deletions delivery_postlogistics/views/delivery.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@
<page
name="postlogistics"
string="PostLogistics"
attrs="{'invisible': [('delivery_type', '!=', 'postlogistics')]}"
invisible="delivery_type != 'postlogistics'"
>
<group>
<group string="Credentials">
<field
name="postlogistics_token_url"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_endpoint_url"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_client_id"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_client_secret"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_scope"
required="delivery_type == 'postlogistics'"
/>
<button
name="verify_credentials"
Expand All @@ -37,33 +45,33 @@
<group string="Template">
<field
name="postlogistics_label_layout"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_output_format"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_resolution"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
</group>
<group string="Misc.">
<field
name="postlogistics_license_id"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_default_package_type_id"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_tracking_format"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field
name="postlogistics_proclima_logo"
attrs="{'required': [('delivery_type', '=', 'postlogistics')]}"
required="delivery_type == 'postlogistics'"
/>
<field name="postlogistics_office" />
</group>
Expand Down
2 changes: 1 addition & 1 deletion delivery_postlogistics/views/postlogistics_license.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<tree editable="bottom">
<field name="name" />
<field name="number" />
<field name="sequence" invisible="True" />
<field name="sequence" column_invisible="1" />
<field name="company_id" groups="base.group_multi_company" />
</tree>
</field>
Expand Down
9 changes: 6 additions & 3 deletions delivery_postlogistics/views/stock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
<odoo>
<record id="view_picking_withcarrier_out_form" model="ir.ui.view">
<field name="model">stock.picking</field>
<field name="inherit_id" ref="delivery.view_picking_withcarrier_out_form" />
<field
name="inherit_id"
ref="stock_delivery.view_picking_withcarrier_out_form"
/>
<field name="arch" type="xml">
<xpath expr="//page//group[@name='carrier_data']/.." position="after">
<separator
string="Delivery instructions"
attrs="{'invisible': [('delivery_type', '!=', 'postlogistics')]}"
invisible="delivery_type != 'postlogistics'"
/>
<group>
<group
name="delivery_instructions"
attrs="{'invisible': [('delivery_type', '!=', 'postlogistics')]}"
invisible="delivery_type != 'postlogistics'"
>
<field name="delivery_fixed_date" />
<field name="delivery_place" />
Expand Down
11 changes: 7 additions & 4 deletions delivery_postlogistics/views/stock_package_type_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
<record id="stock_package_type_form_delivery" model="ir.ui.view">
<field name="name">stock.package.type.form.inherit</field>
<field name="model">stock.package.type</field>
<field name="inherit_id" ref="delivery.stock_package_type_form_delivery" />
<field
name="inherit_id"
ref="stock_delivery.stock_package_type_form_delivery"
/>
<field name="arch" type="xml">
<field name="shipper_package_code" position="attributes">
<attribute
name="attrs"
>{"required": [("package_carrier_type", "=", "postlogistics")]}</attribute>
name="required"
>package_carrier_type == 'postlogistics'</attribute>
</field>
<field name="shipper_package_code" position="after">
<span
colspan="2"
attrs="{'invisible': [('package_carrier_type', '!=', 'postlogistics')]}"
invisible="package_carrier_type != 'postlogistics'"
>See section 8.10 of <a
target="new"
href="https://developer.post.ch/en/digital-commerce-api"
Expand Down
2 changes: 2 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vcrpy
vcrpy-unittest

0 comments on commit 7c56a32

Please sign in to comment.