Skip to content

Commit

Permalink
[MIG] delivery_postlogistics_dangerous_goods: Migration to 14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mmequignon authored and simahawk committed Feb 24, 2023
1 parent eb6aa0a commit 32a8681
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 230 deletions.
4 changes: 2 additions & 2 deletions delivery_postlogistics_dangerous_goods/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Postlogistics Shipping Dangerous Goods",
"summary": "Declare dangerous goods when generating postlogistics labels",
"version": "13.0.1.0.0",
"version": "14.0.1.0.0",
"author": "Camptocamp,Odoo Community Association (OCA)",
"maintainer": "Camptocamp",
"license": "AGPL-3",
Expand All @@ -13,7 +13,7 @@
# OCA/delivery-carrier
"delivery_postlogistics",
# OCA/community-data-files
"l10n_eu_product_adr",
"l10n_eu_product_adr_dangerous_goods",
],
"website": "https://github.com/OCA/delivery-carrier",
"installable": True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class StockPicking(models.Model):
def _generate_postlogistics_label(
self, webservice_class=None, package_ids=None, skip_attach_file=False
):
""" Generate post logistic label using specific from this module."""
"""Generate post logistic label using specific from this module."""
if webservice_class is None:
webservice_class = PostlogisticsWebServiceDangerousGoods
return super()._generate_postlogistics_label(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import re

from odoo import _, exceptions

from odoo.addons.delivery_postlogistics.postlogistics import web_service

UNNUMBER_REGEX = re.compile("^[0-9]{1,4}")
Expand All @@ -20,22 +18,16 @@ def _get_unnumbers(self, picking, pack=None):
and pack.mapped("quant_ids.product_id")
or picking.mapped("move_lines.product_id")
)
limited_amount_lq = picking.env.ref("l10n_eu_product_adr.limited_amount_1")
limited_amount_lq = picking.env.ref(
"l10n_eu_product_adr_dangerous_goods.limited_amount_1"
)
limited_quantity_products = products.filtered(
lambda p: p.is_dangerous and p.limited_amount_id == limited_amount_lq
)
unnumbers = []
for product in limited_quantity_products:
unnumber = product.un_ref.name
match = UNNUMBER_REGEX.match(unnumber)
if not match:
raise exceptions.UserError(
_("UNNumber {} is invalid for product {}.").format(
unnumber, product.name
)
)
unnumbers.append(int(match[0]))
return unnumbers
# Since 14.0, un numbers checks are done directly in l10n_eu_product_adr
return [
int(product.adr_goods_id.un_number) for product in limited_quantity_products
]

def _prepare_attributes(
self, picking, pack=None, pack_num=None, pack_total=None, pack_weight=None
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,63 +1,28 @@
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from os.path import dirname, join

from vcr import VCR

from odoo import exceptions

from odoo.addons.delivery_postlogistics.tests.common import TestPostlogisticsCommon

from ..postlogistics.web_service import PostlogisticsWebServiceDangerousGoods

recorder = VCR(
record_mode="once",
cassette_library_dir=join(dirname(__file__), "fixtures/cassettes"),
path_transformer=VCR.ensure_suffix(".yaml"),
filter_headers=["Authorization", "Date"],
filter_post_data_parameters=["client_id", "client_secret"],
# ignore scheme, host, port
match_on=("method", "path", "query"),
# allow to read and edit content in cassettes
decode_compressed_response=True,
)


class TestPostlogisticsDangerousGoods(TestPostlogisticsCommon):
@classmethod
def setUpClassProduct(cls):
# Create UNNumbers
un_reference_model = cls.env["un.reference"]
cls.unnumber_valid = un_reference_model.create(
{"name": "1234", "description": "Valid UNNumber"}
)
unnumber_non_valid = un_reference_model.create(
{"name": "B1234", "description": "Non-valid UNNumber"}
limited_amount_lq = cls.env.ref(
"l10n_eu_product_adr_dangerous_goods.limited_amount_1"
)

limited_amount_lq = cls.env.ref("l10n_eu_product_adr.limited_amount_1")

weapon_good = cls.env.ref("l10n_eu_product_adr.adr_goods_0007")
# Create products
cls.product_lq = cls.env["product.product"].create(
{
"name": "Product LQ",
"un_ref": cls.unnumber_valid.id,
"limited_amount_id": limited_amount_lq.id,
"is_dangerous": True,
"is_dangerous_good": True,
}
)
cls.product_lq_wrong_number = cls.env["product.product"].create(
cls.dangerous_weapon = cls.env["product.product"].create(
{
"name": "Product LQ wrong UNNumber",
"un_ref": unnumber_non_valid.id,
"name": "Knife-Wrench",
"limited_amount_id": limited_amount_lq.id,
"adr_goods_id": weapon_good.id,
"is_dangerous": True,
"is_dangerous_good": True,
}
)
cls.product_no_lq = cls.env["product.product"].create({"name": "Product no LQ"})
cls.product_no_lq = cls.env["product.product"].create({"name": "Wrench"})

@classmethod
def setUpClassWebservice(cls):
Expand All @@ -71,20 +36,6 @@ def setUpClass(cls):
super().setUpClass()
cls.setUpClassProduct()

@recorder.use_cassette
def test_validate_wrong_unnumber(self):
# Should raise an exception if unnumber is not a 4 digits long string
products = [(self.product_lq_wrong_number, 10.0)]
picking = self.create_picking(product_matrix=products)
with self.assertRaises(exceptions.UserError):
picking._generate_postlogistics_label()

@recorder.use_cassette
def test_confirm_right_unnumber(self):
products = [(self.product_lq, 10.0)]
picking = self.create_picking(product_matrix=products)
picking._generate_postlogistics_label()

def test_json_no_dangerous_goods(self):
# When there's no dangerous goods in the package,
# no unnumber should be sent through the api
Expand All @@ -102,46 +53,16 @@ def test_json_no_dangerous_goods(self):
def test_json_dangerous_goods(self):
# When there's dangerous goods in the package,
# we should have the list of unnumbers
products = [(self.product_lq, 10.0)]
products = [(self.dangerous_weapon, 10.0)]
picking = self.create_picking(product_matrix=products)
package_ids = picking._get_packages_from_picking()
recipient = self.service_class._prepare_recipient(picking)
item_list = self.service_class._prepare_item_list(
picking, recipient, package_ids
)
expected_unnumbers = [
1234,
7,
]
attributes = item_list[0]["attributes"]
self.assertEqual(attributes["unnumbers"], expected_unnumbers)
self.assertIn("LQ", attributes["przl"])

def test_get_unnumbers(self):
products = [(self.product_lq, 10.0)]
picking = self.create_picking(product_matrix=products)
# More than 4 digits
self.unnumber_valid.name = "12345"
expected_unnumbers = [
1234,
]
unnumbers = self.service_class._get_unnumbers(picking)
self.assertEqual(unnumbers, expected_unnumbers)
# Less than 4 digits
self.unnumber_valid.name = "123"
expected_unnumbers = [
123,
]
unnumbers = self.service_class._get_unnumbers(picking)
self.assertEqual(unnumbers, expected_unnumbers)
# Digits and chars
self.unnumber_valid.name = "12A3"
expected_unnumbers = [
12,
]
unnumbers = self.service_class._get_unnumbers(picking)
self.assertEqual(unnumbers, expected_unnumbers)
# First char is digit
self.unnumber_valid.name = "A123"
expected_unnumbers = []
with self.assertRaises(exceptions.UserError):
unnumbers = self.service_class._get_unnumbers(picking)

0 comments on commit 32a8681

Please sign in to comment.