Skip to content

[19.0][MIG] stock_exception: migration to 19.0 and adaptation to the new base_exception flow - #2585

Open
lef-adhoc wants to merge 28 commits into
OCA:19.0from
adhoc-dev:19.0-t-75458-lef
Open

lef-adhoc wants to merge 28 commits into
OCA:19.0from
adhoc-dev:19.0-t-75458-lef

Conversation

@lef-adhoc

@lef-adhoc lef-adhoc commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Migration of stock_exception to 19.0, adapted to the current base_exception.

Why this instead of #2185

#2185 by @JordiMForgeFlow did the migration, and the stale bot closed it on 2026-09-27 without anyone reviewing it. This PR carries its commits unchanged, rebased on the current 19.0, and adds on top the adaptation the module needs: as it stood, stock_exception still drives the pre-refactor mechanism and silently stops working once the base_exception changes of #3590 and the ones that followed are in (forward ported in #3879).

@JordiMForgeFlow, happy to close this one if you would rather reopen yours and take the last commit.

The adaptation

detect_exceptions() no longer returns the exceptions: it stores them and raises BaseExceptionError, and the popup is opened by the client error handler through action_popup_exceptions().

  • action_confirm() and button_validate() just run the detection and let the error stop the flow, instead of reading its return and opening the popup themselves, which with the new mechanism never happened.
  • _must_popup_exception() returns True. Without it the default False turns the error into a soft reload: the transfer stays in draft and the user is never told why.
  • The detection runs in the current transaction where the caller has no handler for the error: the state constraint (the transfer may not be committed yet, for example one created by a sale order confirmation, and the second cursor would fail with MissingError) and the cron, which otherwise stopped on the first transfer with an exception.
  • The rules on the stock moves are checked even when a rule on the transfer already applies: super().detect_exceptions() now raises as soon as it finds one, so they were skipped and a blocking move exception could be ignored together with the others.
  • A move exception points the popup at the transfer, which is the record on screen and where the exception is stored, as sale_exception does with the order lines. Otherwise the client falls back to a soft reload and the user is not told why.
  • The tests follow the new flow: the confirmation raises, and the stored exceptions are read through a detection with base_exception_no_rollback, since the normal flow keeps them in a transaction of its own and rolls this one back.

On top of the migration the module also gets demo data (a customer on credit hold with a warning rule on the transfer and a blocking one on its moves, so runboat has something to show: every shipped rule is archived), the CONFIGURE and USAGE fragments, the new OCA icon and a .po cleanup.

Thanks @bosd for the review, the fixes and the documentation: adhoc-dev#2, merged here commit by commit.

Test plan

Run on an Odoo 19 database with base_exception at the state of #3879:

odoo -d <db> -i stock_exception --test-enable --test-tags /stock_exception

CI runs them with base_exception from #3879, see Depends on: 10 tests, no failures, with Odoo and with OCB.

Depends on

_must_popup_exception(), base_exception_no_rollback and BaseExceptionError come from #3879, the forward port of the base_exception commits that are in 18.0 and missing in 19.0. Until that is merged, CI and runboat take it from the PR through the first commit of this one, [DO NOT MERGE] test-requirements.txt, which has to be dropped before merging. That is what makes the test jobs and runboat meaningful here, at the price of Detect unreleased dependencies, which is red by design while that commit is in.

Known limitation

After Ignore Exceptions in the popup, the wizard always calls action_confirm(). When the popup came from Validate, the transfer is not validated and the user has to click Validate again. This is the behaviour of the module as it was, kept as is here and described in USAGE.

newtratip and others added 20 commits November 24, 2026 14:52
Adding some more info to the description for when exceptions are checked.
Currently translated at 100.0% (28 of 28 strings)

Translation: stock-logistics-warehouse-17.0/stock-logistics-warehouse-17.0-stock_exception
Translate-URL: https://translation.odoo-community.org/projects/stock-logistics-warehouse-17-0/stock-logistics-warehouse-17-0-stock_exception/it/
Adding some more info to the description for when exceptions are checked.
detect_exceptions() no longer returns the exceptions: it stores them and
raises BaseExceptionError, and the popup is opened by the client error
handler through action_popup_exceptions().

- action_confirm() and button_validate() just run the detection and let the
  error stop the flow, instead of reading its return and opening the popup
  themselves, which with the new mechanism never happened.
- _must_popup_exception() returns True. Without it the default False turns
  the error into a soft reload: the transfer stays in draft and the user is
  never told why.
- The tests follow the new flow: the confirmation raises and the popup is
  asked for afterwards, as the client does.
@oca-cla-bot

oca-cla-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bosd bosd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up. I ran the tests locally with base_exception from OCA/server-tools#3879: the 4 existing tests pass, so the red CI only comes from the missing dependency.

CI fix: until #3879 is merged, add a test-requirements.txt at the repository root (remove it again before merging):

odoo-addon-base_exception @ git+https://github.com/OCA/server-tools.git@refs/pull/3879/head#subdirectory=base_exception

Two more things the new flow breaks, with inline suggestions and tests below. I checked each one locally both ways: the new tests fail without the fix and pass with it (6/6).

  1. stock_check_exception runs the detection from the state constraint. A picking confirmed in the same transaction that created it (sale or purchase confirmation, procurement) is not committed yet. When a rule matches, the detection writes the exceptions through a second cursor and fails with MissingError: Record does not exist or has been deleted, so the sale order cannot be confirmed. With base_exception_no_rollback the detection stays in the current transaction, and you get the same ValidationError as on 17.0.
  2. test_all_draft_pickings (the cron) now raises BaseExceptionError as soon as one draft picking matches a rule.

Comment thread stock_exception/models/stock.py Outdated
@api.model
def test_all_draft_pickings(self):
picking_set = self.search([("state", "=", "draft")])
picking_set.detect_exceptions()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cron would now raise BaseExceptionError on the first picking with an exception. Store the exceptions without rolling back:

Suggested change
picking_set.detect_exceptions()
picking_set.with_context(
base_exception_no_rollback={self._name: picking_set.ids}
).detect_exceptions()

Comment thread stock_exception/models/stock.py Outdated
lambda s: s.state in ["waiting", "confirmed", "assigned"]
)
if pickings:
pickings._check_exception()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is reached from the state constraint, also for pickings that were created earlier in the same transaction and are not committed yet (for example when a sale order is confirmed). The detection then writes the exceptions through a new cursor that cannot see the picking, and fails with MissingError. Keep it in the current transaction:

Suggested change
pickings._check_exception()
# The pickings may not be committed yet (e.g. created by a sale
# order confirmation): detect in the current transaction.
pickings.with_context(
base_exception_no_rollback={self._name: pickings.ids}
)._check_exception()

@@ -0,0 +1,129 @@
# Copyright 2024 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
from odoo.tests import Form

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for the new tests below:

Suggested change
from odoo.tests import Form
from odoo.exceptions import ValidationError
from odoo.tests import Form

def test_confirm_picking(self):
self.assertEqual(self.picking.state, "draft")
self.picking.action_confirm()
self.assertEqual(self.picking.state, "assigned")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression tests for the two fixes in stock.py. Both raise BaseExceptionError without them:

Suggested change
self.assertEqual(self.picking.state, "assigned")
self.assertEqual(self.picking.state, "assigned")
def test04_confirm_moves_of_new_picking(self):
"""A picking confirmed through its moves, in the transaction that
created it (as a sale order confirmation does), reports the exception
instead of failing on the uncommitted picking."""
self.exception_rule.create(
{
"name": "No Partner",
"model": "stock.picking",
"exception_type": "by_py_code",
"code": "if not self.partner_id: failed=True",
}
)
picking = self.picking.copy()
self.assertTrue(picking.move_ids)
with self.assertRaisesRegex(ValidationError, "No Partner"):
picking.move_ids._action_confirm()
self.env.flush_all()
def test05_cron_stores_exceptions(self):
self.exception_rule.create(
{
"name": "No Partner",
"model": "stock.picking",
"exception_type": "by_py_code",
"code": "if not self.partner_id: failed=True",
}
)
self.env["stock.picking"].test_all_draft_pickings()
self.assertTrue(self.picking.exception_ids)

bosd added 5 commits November 24, 2026 21:24
The state constraint runs the detection for pickings that may not be
committed yet, e.g. created by a sale order confirmation. The detection
then writes the exceptions through a second cursor that cannot see them,
and fails with MissingError. The cron raised BaseExceptionError on the
first picking with an exception. Both now store the exceptions without
rolling back.
Found by odoo-lint (PO101): these msgids are no longer in the .pot, so
Odoo ignores them.
The detection on the pickings now raises as soon as one of their rules
applies, so the rules on the stock moves were never checked in that case:
only the exceptions of the picking were shown, and a blocking rule on a
move could be ignored together with them. Check the moves before raising.
Add demo rules that only apply to a demo customer on credit hold, one that
warns on the picking and one that blocks on its moves, and a draft delivery
to try them.

Describe when the rules are checked, how to configure them, and where and
with which text the exceptions are shown: the name and description of the
rule, in the popup, the banner on the transfer and the list of transfers.
@bosd

bosd commented Nov 24, 2026

Copy link
Copy Markdown

I put my review suggestions, plus a few more improvements, in adhoc-dev#2 against this branch:

  • the test-requirements.txt for #3879, so CI and runboat work (runboat currently shows no exceptions: it gets the released base_exception, and all shipped rules are archived);
  • a fix for the move rules being skipped whenever a picking rule applies;
  • the new OCA icon and a .po cleanup;
  • demo data and CONFIGURE/USAGE docs.

Each commit stands on its own.

The exceptions detected on the moves are stored on their picking, but the
error carried the move as target model, so the client did a soft reload
instead of opening the popup: the transfer stayed in draft and the user was
not told why. Point the target model to the picking, as sale_exception does
with the order lines.
@lef-adhoc

lef-adhoc commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @bosd, all six commits are in, each one as it was.

That includes the [DO NOT MERGE] one: it is what makes the test jobs and runboat meaningful here. One nuance about "so CI goes green": test-requirements.txt with a URL in it turns Detect unreleased dependencies red by design, as the job rejects any non-comment line with a /. It goes green again when the commit is dropped, once #3879 is in.

On top of yours I added one fix. A move exception carried stock.move as target model, and the client error handler compares that with the record on screen: it did not match the transfer, so it fell back to a soft reload and nothing was shown. The blocking exception of your demo move rule shows up because the rule on the picking applies at the same time and raises first, with the right target model; alone, it was silent. sale_exception solves it by overriding _detect_exception_get_exc_class_values on the order line, and the move does the same now, with a test.

That also covers the one doubt I had about check the moves when the picking has an exception: if the move detection raises inside the except, it replaces the error being re-raised. With the target model fixed, both errors open the popup on the transfer and the stored exceptions of the picking and of its moves are shown together, so there is nothing to guard against there.

Agreed on Ignore Exceptions after Validate. I left the behaviour as it was and noted it in the PR body as a known limitation, so it is not lost.

@bosd bosd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review and functional test LGTM. The target_model fix for move exceptions matches what sale_exception does for order lines.

Before merging: drop the [DO NOT MERGE] commit once OCA/server-tools#3879 is merged and released. The CLA failure is for authors of commits carried over from 14.0.

A picking confirmed in the transaction that created it (a sale order
confirmation, a procurement) is not committed yet, so the cursor that
base_exception opens to store the exceptions cannot see it: the write
fails and no sale order with a delivery can be confirmed.

Check those pickings in the current transaction and raise here, so the
exception is still reported. The committed ones keep storing their
exceptions through the other cursor, which is what the popup reads
after the rollback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants