Skip to content

Commit

Permalink
[MIG] l10n_eu_product_adr: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chaule97 committed Nov 19, 2024
1 parent a20203a commit 86b91a3
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 143 deletions.
4 changes: 4 additions & 0 deletions l10n_eu_product_adr/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Contributors
- Stefan Rijnhart <[email protected]>
- Vyshnevska Iryna <[email protected]>
- Isaac Gallart <[email protected]>
- Chau Le <[email protected]>

Other credits
-------------
Expand All @@ -143,6 +144,9 @@ Dangerous goods data
Dangerous goods data extracted from spreadsheet retrieved from
https://www.cepa.be/wp-content/uploads/ADR_2019_BijlageA_deel3_Tabel_A_EXCEL_FORMAAT.xlsx

The migration of this module from 16.0 to 18.0 was financially supported
by Camptocamp.

Maintainers
-----------

Expand Down
2 changes: 1 addition & 1 deletion l10n_eu_product_adr/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "ADR Dangerous Goods",
"summary": "Allows to set appropriate danger class and components",
"version": "16.0.1.0.0",
"version": "18.0.1.0.0",
"category": "Inventory/Delivery",
"website": "https://github.com/OCA/community-data-files",
"author": "Opener B.V., Camptocamp, Odoo Community Association (OCA)",
Expand Down
2 changes: 1 addition & 1 deletion l10n_eu_product_adr/data/adr_class.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<odoo noupdate="1">
<record id="adr_class_1" model="adr.class">
<field name="name">Explosives</field>
<field name="code">1</field>
Expand Down
2 changes: 1 addition & 1 deletion l10n_eu_product_adr/data/adr_label.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<odoo noupdate="1">
<record id="adr_label_1" model="adr.label">
<field
name="name"
Expand Down
2 changes: 1 addition & 1 deletion l10n_eu_product_adr/data/adr_packing_instruction.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<odoo noupdate="1">
<record id="adr_packing_instruction_IBC01" model="adr.packing.instruction">
<field name="code">IBC01</field>
</record>
Expand Down
2 changes: 1 addition & 1 deletion l10n_eu_product_adr/data/uom_uom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<odoo noupdate="1">
<record id="product_uom_mililiter" model="uom.uom">
<field name="name">ml</field>
<field name="category_id" ref="uom.product_uom_categ_vol" />
Expand Down
11 changes: 6 additions & 5 deletions l10n_eu_product_adr/models/adr_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ def name_search(self, name="", args=None, operator="ilike", limit=100):
"""Allow to search for full codes"""
args = list(args or [])
if name and operator in ("ilike", "="):
res = self.search(AND([args, [("code", "=", name)]]), limit=limit)
res = self.search(AND([args, [("code", operator, name)]]), limit=limit)
if res:
return res.name_get()
return [(rec.id, rec.display_name) for rec in res]
return super().name_search(name=name, args=args, operator=operator, limit=limit)

def name_get(self):
"""Prepend the code to the class name"""
return [(rec.id, f"{rec.code} {rec.name}") for rec in self]
@api.depends("code", "name")
def _compute_display_name(self):
for record in self:
record.display_name = f"{record.code} {record.name}"

_sql_constraints = [
(
Expand Down
37 changes: 23 additions & 14 deletions l10n_eu_product_adr/models/adr_goods.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2021 Stefan Rijnhart <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import _, api, fields, models
from odoo import api, fields, models
from odoo.exceptions import ValidationError
from odoo.osv.expression import AND

Expand Down Expand Up @@ -70,39 +70,48 @@ def _check_un_number(self):
for goods in self:
if len(goods.un_number) != 4:
raise ValidationError(
_(
self.env._(
"UN Number %s is invalid because it does not have "
"a length of 4."
"a length of 4.",
goods.un_number,
)
% goods.un_number
)

@api.model
def name_search(self, name="", args=None, operator="ilike", limit=100):
"""Allow to search for UN Number"""
args = list(args or [])
if name and operator in ("ilike", "="):
res = self.search(AND([args, [("un_number", operator, name)]]), limit=limit)
if res:
return res.name_get()
record = self.search(
AND([args, [("un_number", operator, name)]]), limit=limit
)
if record:
return [(rec.id, rec.display_name) for rec in record]
return super().name_search(name=name, args=args, operator=operator, limit=limit)

def name_get(self):
@api.depends(
"un_number",
"name",
"transport_category",
"limited_quantity",
"limited_quantity_uom_id",
)
def _compute_display_name(self):
"""Format the class name"""
res = []
for rec in self:
name = f"{rec.un_number} {rec.name}"
affixes = []
if rec.transport_category != "-":
affixes.append(_("cat:%s", rec.transport_category))
affixes.append(self.env._("cat:%s", rec.transport_category))
if rec.limited_quantity:
affixes.append(
_("qty:{limited_quantity} {limited_quantity_uom_id}").format(
self.env._(
"qty:%(limited_quantity)s %(limited_quantity_uom_id)s",
limited_quantity=rec.limited_quantity,
limited_quantity_uom_id=rec.limited_quantity_uom_id.name,
)
)
if affixes:
name += " (%s)" % (", ".join(affixes))
res.append((rec.id, name))
return res
name += f" ({', '.join(affixes)})"

rec.display_name = name
9 changes: 4 additions & 5 deletions l10n_eu_product_adr/models/adr_label.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2019 Iryna Vyshnevska (Camptocamp)
# Copyright 2021 Stefan Rijnhart <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import _, fields, models
from odoo import fields, models
from odoo.exceptions import ValidationError


Expand All @@ -26,10 +26,9 @@ def unlink(self):
for label in self:
if label.goods_ids:
raise ValidationError(
_(
"Dangerous Goods Label {label} cannot be deleted because it "
"is in use on one or more dangerous goods: {labels}"
).format(
self.env._(
"Dangerous Goods Label %(label)s cannot be deleted because it "
"is in use on one or more dangerous goods: %(labels)s",
label=label.name,
labels=", ".join(label.goods_ids.mapped("un_number")),
)
Expand Down
4 changes: 2 additions & 2 deletions l10n_eu_product_adr/models/product_template.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2019 Iryna Vyshnevska (Camptocamp)
# Copyright 2021 Opener B.V. <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import _, api, fields, models
from odoo import api, fields, models
from odoo.exceptions import UserError


Expand Down Expand Up @@ -69,7 +69,7 @@ def write(self, values):
if variant_vals:
if any(template.adr_goods_on_variants for template in self):
raise UserError(
_(
self.env._(
"There are different dangerous goods configured on "
"this product's variant, so you cannot update the "
"dangerous goods from here. Please reconfigure each "
Expand Down
1 change: 1 addition & 0 deletions l10n_eu_product_adr/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Stefan Rijnhart \<<[email protected]>\>
- Vyshnevska Iryna \<<[email protected]>\>
- Isaac Gallart \<<[email protected]>\>
- Chau Le \<<[email protected]>\>
2 changes: 2 additions & 0 deletions l10n_eu_product_adr/readme/CREDITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ SVG image files for ADR labels were retrieved from

Dangerous goods data extracted from spreadsheet retrieved from
<https://www.cepa.be/wp-content/uploads/ADR_2019_BijlageA_deel3_Tabel_A_EXCEL_FORMAAT.xlsx>

The migration of this module from 16.0 to 18.0 was financially supported by Camptocamp.
28 changes: 13 additions & 15 deletions l10n_eu_product_adr/scripts/import_adr_multilang_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,10 @@ def parse_limited_quantity(row, split=True):
if "BP 251" in value: # known case
return False, False
else:
raise ValueError("Cannot parse limited quantity: %s (%s)" % (value, row))
raise ValueError(f"Cannot parse limited quantity: {value} ({row})")
quantity, uom_name = match.groups()
if uom_name.lower() not in uom_map:
raise ValueError(
"Unknown uom %s in limited quantity %s (%s)" % (uom_name, value, row)
)
raise ValueError(f"Unknown uom {uom_name} in limited quantity {value} ({row})")
return quantity, uom_map[uom_name.lower()]


Expand Down Expand Up @@ -214,7 +212,7 @@ def get_xml_id(row):
apply_description_quirk(row),
]
res = "_".join(part for part in parts if part).replace(".", "dot")
return "adr_goods_%s" % res
return f"adr_goods_{res}"


def parse_un_number(row):
Expand All @@ -239,8 +237,8 @@ def un_number(record, value, row):
def packing_instruction_ids(record, value, row):
refs = []
for instruction in parse_packing_instructions(row):
refs.append("ref('adr_packing_instruction_%s')" % instruction)
expression = "[(6, 0, [%s])]" % ", ".join(refs)
refs.append(f"ref('adr_packing_instruction_{instruction}')")
expression = f"[(6, 0, [{', '.join(refs)}])]"
etree.SubElement(
record,
"field",
Expand Down Expand Up @@ -281,7 +279,7 @@ def class_id(record, value, row):
"field",
attrib={
"name": "class_id",
"ref": "adr_class_%s" % value.replace(".", "_"),
"ref": f"adr_class_{value.replace('.', '_')}",
},
)

Expand Down Expand Up @@ -321,7 +319,7 @@ def parse_transport_category(row):
if not match:
raise ValueError(
"Unknown value for transport code/tunnel restriction code: "
"%s (%s)" % (value, row)
"{value} ({row})"
)
category = match.groups()[0].strip()
tunnel_restriction_code = match.groups()[1].strip()
Expand All @@ -335,8 +333,8 @@ def parse_transport_category(row):
raise ValueError(f"Invalid transport category {category} in cell value {value}")
if tunnel_restriction_code not in valid_tunnel_codes:
raise ValueError(
"Invalid tunnel restriction code %s in cell value %s"
% (tunnel_restriction_code, value)
f"Invalid tunnel restriction code {tunnel_restriction_code} "
"in cell value {value}"
)
return category, tunnel_restriction_code

Expand Down Expand Up @@ -396,8 +394,8 @@ def label_ids(record, value, row):
labels += ["7A", "7B", "7C", "7E"]
label_refs = []
for label in labels:
label_refs.append("ref('adr_label_%s')" % label.replace(".", "_"))
expression = "[(6, 0, [%s])]" % ", ".join(label_refs)
label_refs.append(f"ref('adr_label_{label.replace('.', '_')}')")
expression = f"[(6, 0, [{', '.join(label_refs)}])]"
etree.SubElement(
record,
"field",
Expand All @@ -414,7 +412,7 @@ def transform_row(root, row):
if field not in transformers:
continue
value = row[index]
if isinstance(value, (int, float)):
if isinstance(value, int | float):
value = str(value)
try:
transformers[field](record, value, row)
Expand Down Expand Up @@ -471,7 +469,7 @@ def import_adr_multilang_xlsx(argv):
else:
seen.add(xmlid)
if duplicates:
raise ValueError("Duplicate xml ids:\n%s" % "\n".join(duplicates))
raise ValueError("Duplicate xml ids:\n" + "\n".join(duplicates))
print( # pylint: disable=W8116
etree.tostring(
root, pretty_print=True, xml_declaration=True, encoding="utf-8"
Expand Down
21 changes: 9 additions & 12 deletions l10n_eu_product_adr/scripts/import_adr_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@
# export ADR_FILE=/tmp/ADR_2019_BijlageA_deel3_Tabel_A_EXCEL_FORMAAT.xlsx
# cat import_adr_translations.py | odoo.py shell -d <DATABASE>

import imp
from os import environ, path
import importlib.util
from os import environ

from openpyxl import load_workbook # pylint: disable=W7936

from odoo.modules import get_module_resource
from odoo.tools import file_open
from odoo.tools.misc import file_path

pyfile = get_module_resource(
"l10n_eu_product_adr", "scripts", "import_adr_multilang_xlsx.py"
)
name, ext = path.splitext(path.basename(pyfile))
fp, pathname = file_open(pyfile, pathinfo=True)
mod = imp.load_module(name, fp, pathname, (".py", "r", imp.PY_SOURCE))
pyfile = file_path("l10n_eu_product_adr/scripts/import_adr_multilang_xlsx.py")
spec = importlib.util.spec_from_file_location("import_adr_multilang_xlsx", pyfile)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
get_xml_id = mod.get_xml_id

env = self.env # noqa
Expand All @@ -42,7 +39,7 @@ def activate_languages():
.search([("code", "=", code)])
)
if not lang:
raise ValueError("Language with code %s not found in Odoo" % code)
raise ValueError(f"Language with code {code} not found in Odoo")
if not lang.active:
lang.active = True
installed.append(code)
Expand All @@ -66,7 +63,7 @@ def import_adr_translations():
if row[0] is None: # Emtpy rows
continue
xml_id = get_xml_id(row)
record = env.ref("l10n_eu_product_adr.%s" % xml_id)
record = env.ref(f"l10n_eu_product_adr.{xml_id}")
for index, lang in columns.items():
if row[index]:
translation = row[index].strip().replace("\n", "")
Expand Down
3 changes: 3 additions & 0 deletions l10n_eu_product_adr/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ <h2><a class="toc-backref" href="#toc-entry-8">Contributors</a></h2>
<li>Stefan Rijnhart &lt;<a class="reference external" href="mailto:stefan&#64;opener.amsterdam">stefan&#64;opener.amsterdam</a>&gt;</li>
<li>Vyshnevska Iryna &lt;<a class="reference external" href="mailto:i.vyshnevska&#64;mobilunity.com">i.vyshnevska&#64;mobilunity.com</a>&gt;</li>
<li>Isaac Gallart &lt;<a class="reference external" href="mailto:igallart&#64;puntsistemes.es">igallart&#64;puntsistemes.es</a>&gt;</li>
<li>Chau Le &lt;<a class="reference external" href="mailto:chaulb&#64;trobz.com">chaulb&#64;trobz.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="other-credits">
Expand All @@ -491,6 +492,8 @@ <h3><a class="toc-backref" href="#toc-entry-10">Label images</a></h3>
<h3><a class="toc-backref" href="#toc-entry-11">Dangerous goods data</a></h3>
<p>Dangerous goods data extracted from spreadsheet retrieved from
<a class="reference external" href="https://www.cepa.be/wp-content/uploads/ADR_2019_BijlageA_deel3_Tabel_A_EXCEL_FORMAAT.xlsx">https://www.cepa.be/wp-content/uploads/ADR_2019_BijlageA_deel3_Tabel_A_EXCEL_FORMAAT.xlsx</a></p>
<p>The migration of this module from 16.0 to 18.0 was financially supported
by Camptocamp.</p>
</div>
</div>
<div class="section" id="maintainers">
Expand Down
Loading

0 comments on commit 86b91a3

Please sign in to comment.