Skip to content

Commit

Permalink
[MIG] product_route_profile: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRijnhart committed Jul 4, 2024
1 parent 56855b5 commit 83446d5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion product_route_profile/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
11 changes: 4 additions & 7 deletions product_route_profile/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand Down
29 changes: 21 additions & 8 deletions product_route_profile/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions product_route_profile/models/route_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
)
2 changes: 1 addition & 1 deletion product_route_profile/tests/test_product_route_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
6 changes: 3 additions & 3 deletions product_route_profile/views/product_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="arch" type="xml">
<xpath expr="//field[@name='route_ids']/parent::div" position="attributes">
<attribute name="attrs">{'invisible': True}</attribute>
<attribute name="invisible">True</attribute>
</xpath>
<xpath expr="//group[@name='operations']" position="inside">
<field
name="route_profile_id"
attrs="{'invisible': [('type', 'in', ['service', 'digital'])]}"
invisible="type not in ['product', 'consu']"
/>
<field
name="force_route_profile_id"
attrs="{'invisible': [('type', 'in', ['service', 'digital'])]}"
invisible="type not in ['product', 'consu']"
/>
</xpath>
</field>
Expand Down

0 comments on commit 83446d5

Please sign in to comment.