diff --git a/shopinvader_search_engine_product_stock/__manifest__.py b/shopinvader_search_engine_product_stock/__manifest__.py index e8c0d3c9f9..cba47e8b5e 100644 --- a/shopinvader_search_engine_product_stock/__manifest__.py +++ b/shopinvader_search_engine_product_stock/__manifest__.py @@ -14,6 +14,7 @@ "installable": True, "depends": ["stock", "shopinvader_search_engine"], "data": [ + "views/se_backend.xml", "views/se_index.xml", "data/queue_job_channel_data.xml", "data/queue_job_function_data.xml", diff --git a/shopinvader_search_engine_product_stock/models/__init__.py b/shopinvader_search_engine_product_stock/models/__init__.py index db97b5c38c..e25561b60e 100644 --- a/shopinvader_search_engine_product_stock/models/__init__.py +++ b/shopinvader_search_engine_product_stock/models/__init__.py @@ -1,3 +1,4 @@ from . import product_product +from . import se_backend from . import se_index from . import stock_move diff --git a/shopinvader_search_engine_product_stock/models/se_backend.py b/shopinvader_search_engine_product_stock/models/se_backend.py new file mode 100644 index 0000000000..6634917cb6 --- /dev/null +++ b/shopinvader_search_engine_product_stock/models/se_backend.py @@ -0,0 +1,36 @@ +# Copyright 2018 Akretion (http://www.akretion.com) +# Copyright 2018 ACSONE SA/NV +# Sébastien BEAU +# Copyright 2020 Camptocamp SA (http://www.camptocamp.com) +# Simone Orsi +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, fields, models + + +class SeBackend(models.Model): + _inherit = "se.backend" + + # NOTE: this field right now has no effect on qty sync. + # It's here to allow extending modules to define their own policies. + # See example in `shopinvader_product_stock_state`. + stock_level_config = fields.Selection( + selection="_selection_stock_level_config", + default="only_qty", + required=True, + help="Define stock level export policy", + ) + show_stock_level_config = fields.Boolean(compute="_compute_show_stock_level_config") + + def _selection_stock_level_config(self): + return [("only_qty", "Only Quantity")] + + @api.depends("index_ids", "index_ids.model_id") + def _compute_show_stock_level_config(self): + product_model_id = ( + self.env["ir.model"].search([("model", "=", "product.product")]).id + ) + for backend in self: + backend.show_stock_level_config = ( + product_model_id in backend.index_ids.mapped("model_id").ids + ) diff --git a/shopinvader_search_engine_product_stock/models/se_index.py b/shopinvader_search_engine_product_stock/models/se_index.py index 62aece7f5c..179313eb5b 100644 --- a/shopinvader_search_engine_product_stock/models/se_index.py +++ b/shopinvader_search_engine_product_stock/models/se_index.py @@ -48,10 +48,9 @@ class SeIndex(models.Model): # It's here to allow extending modules to define their own policies. # See example in `shopinvader_product_stock_state`. stock_level_config = fields.Selection( - selection="_selection_stock_level_config", - default="only_qty", - required=True, - help="Define stock level export policy", + selection=lambda self: self.backend_id._selection_stock_level_config(), + help="Define stock level export policy, " + "keep empty to take configuration from the backend", ) def _default_stock_field_id(self): @@ -60,8 +59,9 @@ def _default_stock_field_id(self): def _default_warehouse_ids(self): return self.env["stock.warehouse"].search([], limit=1) - def _selection_stock_level_config(self): - return [("only_qty", "Only Quantity")] + def _get_stock_level_config(self): + self.ensure_one() + return self.stock_level_config or self.backend_id.stock_level_config def _get_warehouse_list_for_export(self): """Get list of warehouse to be used for exporting stock level. diff --git a/shopinvader_search_engine_product_stock/views/se_backend.xml b/shopinvader_search_engine_product_stock/views/se_backend.xml new file mode 100644 index 0000000000..41245361e3 --- /dev/null +++ b/shopinvader_search_engine_product_stock/views/se_backend.xml @@ -0,0 +1,23 @@ + + + + + + se.backend.form (in shopinvader_search_engine_product_stock) + se.backend + + + + + + + + + + diff --git a/shopinvader_search_engine_product_stock_state/models/__init__.py b/shopinvader_search_engine_product_stock_state/models/__init__.py index 0f6112139f..ea848cb553 100644 --- a/shopinvader_search_engine_product_stock_state/models/__init__.py +++ b/shopinvader_search_engine_product_stock_state/models/__init__.py @@ -3,4 +3,4 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import product_product -from . import se_index +from . import se_backend diff --git a/shopinvader_search_engine_product_stock_state/models/product_product.py b/shopinvader_search_engine_product_stock_state/models/product_product.py index 90898cb910..f60884e3f2 100644 --- a/shopinvader_search_engine_product_stock_state/models/product_product.py +++ b/shopinvader_search_engine_product_stock_state/models/product_product.py @@ -16,7 +16,7 @@ def _prepare_stock_data(self): index_id = self.env.context.get("index_id", False) if index_id: index = self.env["se.index"].browse(index_id) - if "state" in index.stock_level_config: + if "state" in index._get_stock_level_config(): res["state"] = self.stock_state if self._skip_stock_qty_update(index): res.pop("qty", None) @@ -25,7 +25,7 @@ def _prepare_stock_data(self): def _skip_stock_qty_update(self, index=None): self.ensure_one() if index: - config = index.stock_level_config + config = index._get_stock_level_config() return config == "only_state" or ( config == "state_and_low_qty" and self.stock_state != "in_limited_stock" ) diff --git a/shopinvader_search_engine_product_stock_state/models/se_index.py b/shopinvader_search_engine_product_stock_state/models/se_backend.py similarity index 90% rename from shopinvader_search_engine_product_stock_state/models/se_index.py rename to shopinvader_search_engine_product_stock_state/models/se_backend.py index 380a426302..c9d162ed05 100644 --- a/shopinvader_search_engine_product_stock_state/models/se_index.py +++ b/shopinvader_search_engine_product_stock_state/models/se_backend.py @@ -6,8 +6,8 @@ from odoo import models -class SeIndex(models.Model): - _inherit = "se.index" +class SeBackend(models.Model): + _inherit = "se.backend" def _selection_stock_level_config(self): return super()._selection_stock_level_config() + [