Skip to content

Commit

Permalink
[IMP] stock_storage_type: condition context
Browse files Browse the repository at this point in the history
Give in evaluation context what is being moved
  • Loading branch information
jbaudoux committed Nov 29, 2024
1 parent 57d60ea commit a582d80
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
8 changes: 5 additions & 3 deletions stock_storage_type/models/stock_location.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2019-2021 Camptocamp SA
# Copyright 2019-2021 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# Copyright 2019 Camptocamp SA
# Copyright 2019 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
import logging

Expand Down Expand Up @@ -425,7 +425,9 @@ def _get_package_type_putaway_strategy(
return dest_location

for package_sequence in package_locations:
if not package_sequence.can_be_applied(putaway_location, quants, product):
if not package_sequence.can_be_applied(
putaway_location, quants, package, product, quantity
):
continue
pref_loc = package_sequence.location_id
storage_locations = pref_loc.get_storage_locations(products=product)
Expand Down
7 changes: 5 additions & 2 deletions stock_storage_type/models/stock_storage_location_sequence.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2019 Camptocamp SA
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import _, fields, models

Expand Down Expand Up @@ -82,10 +83,12 @@ def button_show_locations(self):
]
return action

def can_be_applied(self, putaway_location, quant, product):
def can_be_applied(self, putaway_location, quant, package, product, quantity):
"""Check if conditions are met."""
self.ensure_one()
for cond in self.location_sequence_cond_ids:
if not cond.evaluate(self, putaway_location, quant, product):
if not cond.evaluate(
self, putaway_location, quant, package, product, quantity
):
return False
return True
51 changes: 46 additions & 5 deletions stock_storage_type/models/stock_storage_location_sequence_cond.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2022 ACSONE SA/NV
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
import textwrap
Expand Down Expand Up @@ -58,7 +59,9 @@ def _default_code_snippet_docs(self):
* condition
* putaway_location
* quant (recordset)
* package
* product
* quantity
* env
* datetime
* dateutil
Expand All @@ -71,7 +74,13 @@ def _default_code_snippet_docs(self):
"""

def _get_code_snippet_eval_context(
self, storage_location_sequence, putaway_location, quant, product
self,
storage_location_sequence,
putaway_location,
quant,
package,
product,
quantity,
):
"""Prepare the context used when evaluating python code
:returns: dict -- evaluation context given to safe_eval
Expand All @@ -83,7 +92,9 @@ def _get_code_snippet_eval_context(
"condition": self,
"putaway_location": putaway_location,
"quant": quant,
"package": package,
"product": product,
"quantity": quantity,
"datetime": safe_eval.datetime,
"dateutil": safe_eval.dateutil,
"time": safe_eval.time,
Expand All @@ -93,12 +104,25 @@ def _get_code_snippet_eval_context(
),
}

def _exec_code(self, storage_location_sequence, putaway_location, quant, product):
def _exec_code(
self,
storage_location_sequence,
putaway_location,
quant,
package,
product,
quantity,
):
self.ensure_one()
if not self._code_snippet_valued():
return False
eval_ctx = self._get_code_snippet_eval_context(
storage_location_sequence, putaway_location, quant, product
storage_location_sequence,
putaway_location,
quant,
package,
product,
quantity,
)
snippet = self.code_snippet
safe_eval.safe_eval(snippet, eval_ctx, mode="exec", nocopy=True)
Expand All @@ -113,13 +137,17 @@ def _exec_code(self, storage_location_sequence, putaway_location, quant, product
"* putaway sequence: %s\n"
"* putaway location: %s\n"
"* quants: %s\n"
"* package: %s\n"
"* product: %s\n"
"* quantity: %s\n"
% (
self.name,
storage_location_sequence.id,
putaway_location.name,
quant.ids,
package.display_name,
product.display_name,
quantity,
)
)
return result
Expand All @@ -135,11 +163,24 @@ def _code_snippet_valued(self):
]
)

def evaluate(self, storage_location_sequence, putaway_location, quant, product):
def evaluate(
self,
storage_location_sequence,
putaway_location,
quant,
package,
product,
quantity,
):
self.ensure_one()
if self.condition_type == "code":
return self._exec_code(
storage_location_sequence, putaway_location, quant, product
storage_location_sequence,
putaway_location,
quant,
package,
product,
quantity,
)
condition_type = self.condition_type
raise exceptions.UserError(
Expand Down

0 comments on commit a582d80

Please sign in to comment.