Skip to content

Commit

Permalink
[IMP] product_abc_classification_base: pre-commit stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rousseldenis committed Nov 10, 2022
1 parent 380c1cc commit ea0a87d
Show file tree
Hide file tree
Showing 41 changed files with 222 additions and 2,005 deletions.
1 change: 0 additions & 1 deletion product_abc_classification_base/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2020 ForgeFlow
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
Expand Down
21 changes: 5 additions & 16 deletions product_abc_classification_base/models/abc_classification_level.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2020 ForgeFlow
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
Expand All @@ -13,13 +12,9 @@ class AbcClassificationLevel(models.Model):
_order = "percentage desc, id desc"
_rec_name = "name"

percentage_products = fields.Float(
default=0.0, required=True, string="% Products"
)
percentage_products = fields.Float(default=0.0, required=True, string="% Products")
percentage = fields.Float(default=0.0, required=True, string="% Indicator")
profile_id = fields.Many2one(
"abc.classification.profile", ondelete="cascade"
)
profile_id = fields.Many2one("abc.classification.profile", ondelete="cascade")

name = fields.Char(help="Classification A, B or C", required=True)

Expand All @@ -35,13 +30,9 @@ class AbcClassificationLevel(models.Model):
def _check_percentage(self):
for level in self:
if level.percentage > 100.0:
raise ValidationError(
_("The percentage cannot be greater than 100.")
)
raise ValidationError(_("The percentage cannot be greater than 100."))
if level.percentage <= 0.0:
raise ValidationError(
_("The percentage should be a positive number.")
)
raise ValidationError(_("The percentage should be a positive number."))

@api.constrains("percentage_products")
def _check_percentage_products(self):
Expand All @@ -52,7 +43,5 @@ def _check_percentage_products(self):
)
if level.percentage_products <= 0.0:
raise ValidationError(
_(
"The percentage of products should be a positive number."
)
_("The percentage of products should be a positive number.")
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

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


Expand All @@ -18,7 +17,7 @@ class AbcClassificationProductLevel(models.Model):
"abc.classification.level",
string="Manual classification level",
track_visibility="onchange",
domain="[('profile_id', '=', profile_id)]"
domain="[('profile_id', '=', profile_id)]",
)
computed_level_id = fields.Many2one(
"abc.classification.level",
Expand All @@ -30,7 +29,7 @@ class AbcClassificationProductLevel(models.Model):
string="Classification level",
compute="_compute_level_id",
store=True,
domain="[('profile_id', '=', profile_id)]"
domain="[('profile_id', '=', profile_id)]",
)
flag = fields.Boolean(
default=False,
Expand Down Expand Up @@ -66,7 +65,7 @@ class AbcClassificationProductLevel(models.Model):
)
allowed_profile_ids = fields.Many2many(
comodel_name="abc.classification.profile",
related="product_id.abc_classification_profile_ids"
related="product_id.abc_classification_profile_ids",
)

_sql_constraints = [
Expand All @@ -92,10 +91,7 @@ def _check_level(self):
"profile as the one on the product level"
)
)
if (
rec.manual_level_id
and rec.manual_level_id.profile_id != rec.profile_id
):
if rec.manual_level_id and rec.manual_level_id.profile_id != rec.profile_id:
raise ValidationError(
_(
"Manual level must be in the same classifiation "
Expand All @@ -113,7 +109,7 @@ def _onchange_product_tmpl_id(self):
@api.depends("level_id", "profile_id")
def _compute_display_name(self):
for record in self:
record.display_name = u"{profile_name}: {level_name}".format(
record.display_name = "{profile_name}: {level_name}".format(
profile_name=record.profile_id.name,
level_name=record.level_id.name,
)
Expand All @@ -130,8 +126,7 @@ def _compute_level_id(self):
def _compute_flag(self):
for rec in self:
rec.flag = (
rec.computed_level_id
and rec.manual_level_id != rec.computed_level_id
rec.computed_level_id and rec.manual_level_id != rec.computed_level_id
)

@api.model
Expand All @@ -142,19 +137,20 @@ def create(self, vals):
vals["manual_level_id"] = vals["computed_level_id"]

if "profile_id" in vals:
profile = self.env["abc.classification.profile"].browse(vals['profile_id'])
profile = self.env["abc.classification.profile"].browse(vals["profile_id"])
if profile.auto_apply_computed_value and "computed_level_id" in vals:
vals["manual_level_id"] = vals["computed_level_id"]
return super(AbcClassificationProductLevel, self).create(vals)

def write(self, vals):
values = vals.copy()
if "profile_id" in values:
profile = self.env["abc.classification.profile"].browse(values['profile_id'])
profile = self.env["abc.classification.profile"].browse(
values["profile_id"]
)
else:
profile = self.mapped("profile_id")

if profile.auto_apply_computed_value and "computed_level_id" in values:
values["manual_level_id"] = values["computed_level_id"]
return super(AbcClassificationProductLevel, self).write(values)

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
# Copyright 2020 ForgeFlow
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
from psycopg2.extensions import AsIs

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


class AbcClassificationProfile(models.Model):

Expand All @@ -32,9 +32,7 @@ class AbcClassificationProfile(models.Model):

auto_apply_computed_value = fields.Boolean(default=False)

_sql_constraints = [
("name_uniq", "UNIQUE(name)", _("Profile name must be unique"))
]
_sql_constraints = [("name_uniq", "UNIQUE(name)", _("Profile name must be unique"))]

@api.constrains("level_ids")
def _check_levels(self):
Expand All @@ -43,20 +41,13 @@ def _check_levels(self):
total = sum(percentages)
if profile.level_ids and total != 100.0:
raise ValidationError(
_(
"The sum of the percentages of the levels should be "
"100."
)
_("The sum of the percentages of the levels should be " "100.")
)
if profile.level_ids and len({}.fromkeys(percentages)) != len(
percentages
):
if profile.level_ids and len({}.fromkeys(percentages)) != len(percentages):
raise ValidationError(
_("The percentages of the levels must be unique.")
)
percentage_productss = profile.level_ids.mapped(
"percentage_products"
)
percentage_productss = profile.level_ids.mapped("percentage_products")
total = sum(percentage_productss)
if profile.level_ids and total != 100.0:
raise ValidationError(
Expand All @@ -76,24 +67,30 @@ def _cron_compute_abc_classification(self):

def write(self, vals):
res = super(AbcClassificationProfile, self).write(vals)
if 'auto_apply_computed_value' in vals and vals['auto_apply_computed_value']:
if "auto_apply_computed_value" in vals and vals["auto_apply_computed_value"]:
self._auto_apply_computed_value_for_product_levels()
return res

def _auto_apply_computed_value_for_product_levels(self):
level_ids = []
for rec in self:
self.env.cr.execute("""
self.env.cr.execute(
"""
UPDATE %(table)s
SET manual_level_id = computed_level_id
WHERE profile_id = %(profile_id)s
RETURNING id
""", {"table": AsIs(self.env["abc.classification.product.level"]._table),
"profile_id": rec.id}
""",
{
"table": AsIs(self.env["abc.classification.product.level"]._table),
"profile_id": rec.id,
},
)
level_ids.extend(r[0] for r in self.env.cr.fetchall())
self.env["abc.classification.product.level"].invalidate_cache(["manual_level_id"], level_ids)
self.env["abc.classification.product.level"].invalidate_cache(
["manual_level_id"], level_ids
)
modified_levels = self.env["abc.classification.product.level"].browse(level_ids)
# mark field as modified and trigger recompute of dependent fields.
modified_levels.modified(["manual_level_id"])
Expand Down
1 change: 0 additions & 1 deletion product_abc_classification_base/models/product_product.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2020 ForgeFlow
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
Expand Down
10 changes: 6 additions & 4 deletions product_abc_classification_base/models/product_template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2020 ForgeFlow
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
Expand Down Expand Up @@ -48,21 +47,24 @@ def _compute_abc_classification_product_level_ids(self):
)
for template in unique_variants:
variants = template.product_variant_ids
template.abc_classification_product_level_ids = \
template.abc_classification_product_level_ids = (
variants.abc_classification_product_level_ids
)
for template in self - unique_variants:
template.abc_classification_product_level_ids = False

def _inverse_abc_classification_profile_ids(self):
for template in self:
if len(template.product_variant_ids) == 1:
variants = template.product_variant_ids
variants.abc_classification_profile_ids = \
variants.abc_classification_profile_ids = (
template.abc_classification_profile_ids
)

def _inverse_abc_classification_product_level_ids(self):
for template in self:
if len(template.product_variant_ids) == 1:
variants = template.product_variant_ids
variants.abc_classification_product_level_ids = \
variants.abc_classification_product_level_ids = (
template.abc_classification_product_level_ids
)
9 changes: 2 additions & 7 deletions product_abc_classification_base/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

Expand Down Expand Up @@ -78,13 +77,9 @@ def setUpClass(cls):
}
)
levels = cls.classification_profile_bis.level_ids
cls.classification_level_bis_a = levels.filtered(
lambda l: l.name == "a"
)
cls.classification_level_bis_a = levels.filtered(lambda l: l.name == "a")

cls.classification_level_bis_b = levels.filtered(
lambda l: l.name == "b"
)
cls.classification_level_bis_b = levels.filtered(lambda l: l.name == "b")
# create a template with one variant adn declare attributes to create
# an other variant on demand
cls.size_attr = cls.env["product.attribute"].create(
Expand Down
Loading

0 comments on commit ea0a87d

Please sign in to comment.