From 83446d5b66a8d6493ea7e6fda88e88b0519df2b9 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Thu, 4 Jul 2024 15:57:36 +0200 Subject: [PATCH] [MIG] product_route_profile: Migration to 17.0 --- product_route_profile/__manifest__.py | 2 +- product_route_profile/hooks.py | 11 +++---- .../models/product_template.py | 29 ++++++++++++++----- product_route_profile/models/route_profile.py | 3 +- .../tests/test_product_route_profile.py | 2 +- .../views/product_template.xml | 6 ++-- 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/product_route_profile/__manifest__.py b/product_route_profile/__manifest__.py index 7519aed60e56..e3da6ad1bf1d 100644 --- a/product_route_profile/__manifest__.py +++ b/product_route_profile/__manifest__.py @@ -5,7 +5,7 @@ { "name": "Product Route Profile", "summary": "Add Route profile concept on product", - "version": "15.0.1.0.0", + "version": "17.0.1.0.0", "category": "Warehouse", "website": "https://github.com/OCA/stock-logistics-warehouse", "author": "Akretion, Odoo Community Association (OCA)", diff --git a/product_route_profile/hooks.py b/product_route_profile/hooks.py index c0ba61394c31..880da17f6046 100644 --- a/product_route_profile/hooks.py +++ b/product_route_profile/hooks.py @@ -4,17 +4,15 @@ from collections import defaultdict -from odoo import SUPERUSER_ID, api - -def post_init_hook(cr, registry): +def post_init_hook(env): def get_profile(route_ids): route_ids = tuple(set(route_ids)) profile = route2profile.get(route_ids) if not profile: profile_name = "" route_names = [ - rec.name for rec in env["stock.location.route"].browse(route_ids) + rec.name for rec in env["stock.route"].browse(route_ids) ] profile_name = " / ".join(route_names) profile = env["route.profile"].create( @@ -26,13 +24,12 @@ def get_profile(route_ids): route2profile[route_ids] = profile return profile - env = api.Environment(cr, SUPERUSER_ID, {}) query = """ SELECT product_id, array_agg(route_id) FROM stock_route_product group by product_id; """ - cr.execute(query) - results = cr.fetchall() + env.cr.execute(query) + results = env.cr.fetchall() route2profile = {} profile2product = defaultdict(lambda: env["product.template"]) for row in results: diff --git a/product_route_profile/models/product_template.py b/product_route_profile/models/product_template.py index a6b158975f68..d4923d1c49c8 100644 --- a/product_route_profile/models/product_template.py +++ b/product_route_profile/models/product_template.py @@ -64,11 +64,24 @@ def _prepare_profile(self): "route_ids": [(6, 0, self.route_ids.ids)], } - @api.model - def create(self, vals): - route_profile_id = vals.get("route_profile_id", False) - if route_profile_id: - route_profile = self.env["route.profile"].browse(route_profile_id) - vals["route_ids"] = [(6, 0, route_profile.route_ids.ids)] - self = self.with_context(skip_inverse_route_ids=True) - return super(ProductTemplate, self).create(vals) + @api.model_create_multi + def create(self, vals_list): + vals_with_profile = [] + vals_without_profile = [] + for vals in vals_list: + route_profile_id = vals.get("route_profile_id") + if route_profile_id: + vals = vals.copy() + route_profile = self.env["route.profile"].browse(route_profile_id) + vals["route_ids"] = [(6, 0, route_profile.route_ids.ids)] + vals_with_profile.append(vals) + else: + vals_without_profile.append(vals) + res = self.env["product.template"] + if vals_without_profile: + res += super().create(vals_without_profile) + if vals_with_profile: + res += super( + ProductTemplate, self.with_context(skip_inverse_route_ids=True) + ).create(vals_with_profile) + return res diff --git a/product_route_profile/models/route_profile.py b/product_route_profile/models/route_profile.py index 12df849b49b4..e71b52067a5f 100644 --- a/product_route_profile/models/route_profile.py +++ b/product_route_profile/models/route_profile.py @@ -14,10 +14,9 @@ class RouteProfile(models.Model): comodel_name="res.company", default=lambda self: self.env.company.id, required=False, - string="Company", ) route_ids = fields.Many2many( - "stock.location.route", + comodel_name="stock.route", string="Routes", domain=[("product_selectable", "=", True)], ) diff --git a/product_route_profile/tests/test_product_route_profile.py b/product_route_profile/tests/test_product_route_profile.py index 2ff6636e419f..9e41e07a8948 100644 --- a/product_route_profile/tests/test_product_route_profile.py +++ b/product_route_profile/tests/test_product_route_profile.py @@ -8,7 +8,7 @@ class TestProductRouteProfile(TransactionCase): @classmethod def setUpClass(cls): - super(TestProductRouteProfile, cls).setUpClass() + super().setUpClass() cls.company_bis = cls.env["res.company"].create( { diff --git a/product_route_profile/views/product_template.xml b/product_route_profile/views/product_template.xml index 33c49e431c0c..93f073c3c7e0 100644 --- a/product_route_profile/views/product_template.xml +++ b/product_route_profile/views/product_template.xml @@ -9,16 +9,16 @@ - {'invisible': True} + True