Skip to content

Commit

Permalink
cms_form: add FormRedirect exception
Browse files Browse the repository at this point in the history
Handy to force redirect down the stack.
  • Loading branch information
simahawk committed Jan 25, 2024
1 parent 631964e commit 27074a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cms_form/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from odoo import _, http
from odoo.http import request

from ..exceptions import FormRedirect


class FormControllerMixin(object):

Expand Down Expand Up @@ -96,7 +98,10 @@ def make_response(self, model, model_id=None, **kw):
form = self.get_form(model, model_id=model_id, **kw)
form.form_check_permission()
# pass only specific extra args, to not pollute form render values
form.form_process(extra_args={"page": kw.get("page")})
try:
form.form_process(extra_args={"page": kw.get("page")})
except FormRedirect as exc:
return werkzeug.utils.redirect(exc.next_url, code=303)
# search forms do not need these attrs
if getattr(form, "form_success", None) and getattr(form, "form_redirect", None):
# anything went fine, redirect to next url
Expand Down
10 changes: 10 additions & 0 deletions cms_form/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2024 Simone Orsi
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).


class FormRedirect(Exception):
"""Triggers a redirect to given URL."""

def __init__(self, message, next_url):
super().__init__(message)
self.next_url = next_url

0 comments on commit 27074a3

Please sign in to comment.