Skip to content

Commit

Permalink
Merge pull request #7 from legalsylvain/16.0-mig-product_origin-improved
Browse files Browse the repository at this point in the history
[IMP] product_origin : improve migration
  • Loading branch information
acsonefho authored Jul 25, 2023
2 parents acd052b + e014701 commit 9b9e325
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 26 deletions.
12 changes: 5 additions & 7 deletions product_origin/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@

{
"name": "Product Origin",
"summary": """
Adds the origin of the product""",
"summary": """Adds the origin of the product""",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"development_status": "Beta",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"author": "ACSONE SA/NV,GRAP,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/product-attribute",
"maintainers": ["rousseldenis"],
"depends": [
"product",
],
"maintainers": ["rousseldenis", "legalsylvain"],
"depends": ["product"],
"data": [
"views/product_product.xml",
"views/product_template.xml",
],
}
1 change: 1 addition & 0 deletions product_origin/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import product_product
from . import product_template
38 changes: 38 additions & 0 deletions product_origin/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (C) 2023 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class ProductProduct(models.Model):
_inherit = "product.product"

country_id = fields.Many2one(
comodel_name="res.country",
string="Country of Origin",
ondelete="restrict",
)

state_id = fields.Many2one(
comodel_name="res.country.state",
string="Country State of Origin",
ondelete="restrict",
)

@api.constrains("country_id", "state_id")
def _chekc_country_id_state_id(self):
for product in self.filtered(lambda x: x.state_id and x.country_id):
if product.country_id != product.state_id.country_id:
raise ValidationError(
_(
f"The state '{product.state_id.name}' doesn't belong to"
f" the country '{product.country_id.name}'"
)
)

@api.onchange("country_id")
def onchange_country_id(self):
if self.state_id and self.state_id.country_id != self.country_id:
self.state_id = False
44 changes: 39 additions & 5 deletions product_origin/models/product_template.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright 2018 ACSONE SA/NV
# Copyright (C) 2023 - Today: GRAP (http://www.grap.coop)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models
from odoo import api, fields, models


class ProductTemplate(models.Model):
Expand All @@ -10,14 +11,47 @@ class ProductTemplate(models.Model):

country_id = fields.Many2one(
comodel_name="res.country",
compute="_compute_country_id",
inverse="_inverse_country_id",
string="Country of Origin",
ondelete="restrict",
index=True,
store=True,
)

state_id = fields.Many2one(
comodel_name="res.country.state",
compute="_compute_state_id",
inverse="_inverse_state_id",
string="Country State of Origin",
ondelete="restrict",
index=True,
store=True,
)

@api.depends("product_variant_ids", "product_variant_ids.country_id")
def _compute_country_id(self):
for template in self:
if template.product_variant_count == 1:
template.country_id = template.product_variant_ids.country_id
else:
template.country_id = False

def _inverse_country_id(self):
for template in self:
if len(template.product_variant_ids) == 1:
template.product_variant_ids.country_id = template.country_id

@api.depends("product_variant_ids", "product_variant_ids.state_id")
def _compute_state_id(self):
for template in self:
if template.product_variant_count == 1:
template.state_id = template.product_variant_ids.state_id
else:
template.state_id = False

def _inverse_state_id(self):
for template in self:
if len(template.product_variant_ids) == 1:
template.product_variant_ids.country_id = template.country_id

def _get_related_fields_variant_template(self):
res = super()._get_related_fields_variant_template()
res += ["country_id", "state_id"]
return res
1 change: 1 addition & 0 deletions product_origin/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Denis Roussel <[email protected]>
* Sylvain LE GAL (https://twitter.com/legalsylvain)
2 changes: 2 additions & 0 deletions product_origin/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Go to product form
* Fill in the country and/or state of origin of the product under the
'General Information' tab.

.. figure:: ../static/description/product_form.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions product_origin/views/product_product.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2023 - Today: GRAP (http://www.grap.coop)
@author: Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>

<record id="view_product_product_form" model="ir.ui.view">
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_variant_easy_edit_view" />
<field name="arch" type="xml">
<xpath expr="//group[@name='weight']" position="after">
<group string="Origin" name="group_origin">
<field name="country_id" />
<field
name="state_id"
attrs="{'invisible': [('country_id', '=', False)]}"
domain="[('country_id', '=', country_id)]"
/>
</group>
</xpath>
</field>
</record>

</odoo>
30 changes: 16 additions & 14 deletions product_origin/views/product_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,36 @@
<odoo>

<record model="ir.ui.view" id="product_template_form_view">
<field name="name">product.template.form (in product_origin)</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="arch" type="xml">
<group name="inventory" position="inside">
<group name="product_origin">
<group>
<field name="country_id" />
<field
name="state_id"
attrs="{'invisible': [('country_id', '=', False)]}"
domain="[('country_id', '=', country_id)]"
/>
</group>
<group />
<xpath expr="//group[@name='group_lots_and_weight']" position="after">
<group string="Origin" name="group_origin">
<field name="country_id" />
<field
name="state_id"
attrs="{'invisible': [('country_id', '=', False)]}"
domain="[('country_id', '=', country_id)]"
/>
</group>
</group>
</xpath>
</field>
</record>

<record model="ir.ui.view" id="product_template_search_view">
<field name="name">product.template.search (in product_origin)</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_search_view" />
<field name="arch" type="xml">
<filter name="favorites" position="before">
<field name="country_id" />
</filter>
<filter name="categ_id" position="after">
<filter
string="Country of Origin"
name="country_id"
context="{'group_by':'country_id'}"
/>
</filter>
</field>
</record>

Expand Down

0 comments on commit 9b9e325

Please sign in to comment.