Skip to content

Commit

Permalink
[IMP] Configure default stock_level_config on se.backend
Browse files Browse the repository at this point in the history
  • Loading branch information
qgroulard committed Oct 26, 2023
1 parent 06721e7 commit 22b15ec
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions shopinvader_search_engine_product_stock/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions shopinvader_search_engine_product_stock/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import product_product
from . import se_backend
from . import se_index
from . import stock_move
36 changes: 36 additions & 0 deletions shopinvader_search_engine_product_stock/models/se_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2018 Akretion (http://www.akretion.com)
# Copyright 2018 ACSONE SA/NV
# Sébastien BEAU <[email protected]>
# Copyright 2020 Camptocamp SA (http://www.camptocamp.com)
# Simone Orsi <[email protected]>
# 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
)
12 changes: 6 additions & 6 deletions shopinvader_search_engine_product_stock/models/se_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions shopinvader_search_engine_product_stock/views/se_backend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 ACSONE SA/NV
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="se_backend_form_view">
<field
name="name"
>se.backend.form (in shopinvader_search_engine_product_stock)</field>
<field name="model">se.backend</field>
<field name="inherit_id" ref="connector_search_engine.se_backend_form_view" />
<field name="arch" type="xml">
<xpath expr="//group[@name='se']/group[@name='se-main']" postion="inside">
<field name="show_stock_level_config" invisible="1" />
<field
name="stock_level_config"
attrs="{'invisible': [('show_stock_level_config', '=', False)]}"
/>
</xpath>
</field>
</record>

</odoo>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() + [
Expand Down

0 comments on commit 22b15ec

Please sign in to comment.