-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[18.0][POC] base_exception: Rollback transaction triggering exception rule #3476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
grindtildeath
wants to merge
3
commits into
OCA:18.0
Choose a base branch
from
camptocamp:18.0-fix-base_exception_rollback_transaction
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−4
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
cda3f33
[POC] base_exception: Rollback transaction triggering exception rule
grindtildeath 365e9cb
fixup! [POC] base_exception: Rollback transaction triggering exceptio…
grindtildeath 7aa6c4e
fixup! fixup! [POC] base_exception: Rollback transaction triggering e…
grindtildeath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Copyright 2025 Camptocamp SA | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) | ||
|
|
||
| from odoo.exceptions import ValidationError | ||
|
|
||
|
|
||
| class BaseExceptionError(ValidationError): | ||
| def __init__(self, msg, rules_to_add, rules_to_remove): | ||
| super().__init__(msg) | ||
| self.rules_to_add = rules_to_add | ||
| self.rules_to_remove = rules_to_remove |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from . import exception_rule | ||
| from . import base_exception_method | ||
| from . import base_exception | ||
| from . import ir_http |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Copyright 2025 Camptocamp SA | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) | ||
|
|
||
| from odoo import models | ||
| from odoo.api import Environment | ||
| from odoo.fields import Command | ||
| from odoo.http import request | ||
| from odoo.modules.registry import Registry | ||
|
|
||
| from ..exceptions import BaseExceptionError | ||
|
|
||
|
|
||
| class IrHttp(models.AbstractModel): | ||
| _inherit = "ir.http" | ||
|
|
||
| @classmethod | ||
| def _dispatch(cls, endpoint): | ||
| res = None | ||
| # FIXME: Find a way to condition the creation of new transaction | ||
| # only for requests that may trigger an exception rule | ||
| # ie exclude whatever goes to bus, websocket, getting views, etc | ||
| old_env = request.env | ||
| to_add = {} | ||
| to_remove = {} | ||
| with Registry(old_env.cr.dbname).cursor() as new_cr: | ||
| new_env = Environment(new_cr, old_env.uid, old_env.context) | ||
| request.env = new_env | ||
| try: | ||
| res = super()._dispatch(endpoint) | ||
| except BaseExceptionError as err: | ||
| to_add = err.rules_to_add | ||
| to_remove = err.rules_to_remove | ||
| new_env.cr.rollback() | ||
|
|
||
| for rule_id, (model, res_ids) in to_remove.items(): | ||
| old_env[model].browse(res_ids).write( | ||
| {"exception_ids": [Command.unlink(rule_id)]} | ||
| ) | ||
| for rule_id, (model, res_ids) in to_add.items(): | ||
| old_env[model].browse(res_ids).write( | ||
| {"exception_ids": [Command.link(rule_id)]} | ||
| ) | ||
|
|
||
| request.env = old_env | ||
| return res | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will work only over an HTTP request. What about crons or other types of operations? 🤔