Skip to content

Commit 1225dd7

Browse files
committed
fix: Redirect user to edit url on success
1 parent e0c7550 commit 1225dd7

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

cms/tests/test_wizards.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from cms.test_utils.project.backwards_wizards.wizards import wizard
2020
from cms.test_utils.project.sampleapp.cms_wizards import sample_wizard
2121
from cms.test_utils.testcases import CMSTestCase, TransactionCMSTestCase
22+
from cms.toolbar.utils import get_object_edit_url
2223
from cms.utils import get_current_site
2324
from cms.utils.conf import get_cms_setting
2425
from cms.utils.setup import setup_cms_apps
@@ -142,6 +143,25 @@ def test_get_success_url(self):
142143
url = page.get_absolute_url()
143144
self.assertEqual(self.page_wizard.get_success_url(page), url)
144145

146+
def test_get_edit_url(self):
147+
user = self.get_superuser()
148+
page = create_page(
149+
title="Sample Page",
150+
template=TEMPLATE_INHERITANCE_MAGIC,
151+
language="en",
152+
created_by=smart_str(user),
153+
parent=None,
154+
in_navigation=True,
155+
)
156+
157+
extension = apps.get_app_config('cms').cms_extension
158+
159+
with patch.object(extension, 'toolbar_enabled_models', {Page: page}):
160+
url = self.page_wizard.get_success_url(
161+
page, language="en")
162+
self.assertEqual(
163+
url, get_object_edit_url(page, language="en"))
164+
145165
def test_get_model(self):
146166
self.assertEqual(self.page_wizard.get_model(), Page)
147167
self.assertEqual(self.user_settings_wizard.get_model(), UserSettings)

cms/wizards/wizard_base.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
from django.utils.translation import gettext as _
99
from django.utils.translation import override as force_language
1010

11-
from cms.toolbar.utils import get_object_preview_url
11+
from cms.toolbar.utils import (
12+
get_object_edit_url,
13+
get_object_preview_url,
14+
)
1215

1316

1417
class WizardBase:
@@ -18,7 +21,7 @@ class WizardBase:
1821
template_name = None
1922

2023
def __init__(self, title, weight, form, model=None, template_name=None,
21-
description=None):
24+
description=None, edit_mode_on_success=True):
2225
"""
2326
:param title: The title of the wizard. It will appear in a large font size on the wizard “menu”
2427
:param weight: Used for determining the order of the wizards on the
@@ -32,14 +35,16 @@ def __init__(self, title, weight, form, model=None, template_name=None,
3235
:param description: This is used on the start form. The description is optional, but if it is
3336
not supplied, the CMS will create one from the pattern:
3437
"Create a new «model.verbose_name» instance."
38+
:param edit_mode_on_success: Whether the user will get redirected to object edit url after a
39+
successful creation or not. This only works if the object is registered
40+
for toolbar enabled models.
3541
"""
36-
# NOTE: If class attributes or properties are changed, consider updating
37-
# cms.templatetags.cms_wizard_tags.WizardProperty too.
3842
self.title = title
3943
self.weight = weight
4044
self.form = form
4145
self.model = model
4246
self.description = description
47+
self.edit_mode_on_success = edit_mode_on_success
4348
if template_name is not None:
4449
self.template_name = template_name
4550

@@ -129,9 +134,10 @@ def get_success_url(self, obj, **kwargs):
129134
"""
130135
Once the wizard has completed, the user will be redirected to the URL of the new
131136
object that was created. By default, this is done by return the result of
132-
calling the ``get_absolute_url`` method on the object. This may then be modified
133-
to force the user into edit mode if the wizard property ``edit_mode_on_success``
134-
is True.
137+
calling the ``get_absolute_url`` method on the object. If the object is registered
138+
for toolbar enabled models, the object edit url will be returned. This may be modified
139+
to return the preview url instead by setting the wizard property ``edit_mode_on_success``
140+
to False.
135141
136142
In some cases, the created content will not implement ``get_absolute_url`` or
137143
that redirecting the user is undesirable. In these cases, simply override this
@@ -144,6 +150,8 @@ def get_success_url(self, obj, **kwargs):
144150
extension = apps.get_app_config('cms').cms_extension
145151

146152
if obj.__class__ in extension.toolbar_enabled_models:
153+
if self.edit_mode_on_success:
154+
return get_object_edit_url(obj, language=kwargs.get("language", None))
147155
return get_object_preview_url(obj, language=kwargs.get("language", None))
148156
else:
149157
if "language" in kwargs:

docs/reference/wizards.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ When instantiating a Wizard object, use the keywords:
3838
:description: The description is optional, but if it is not supplied, the
3939
CMS will create one from the pattern:
4040
"Create a new «model.verbose_name» instance."
41+
:edit_mode_on_success: Whether the user will get redirected to object edit url after a
42+
successful creation or not. This only works if the object is registered
43+
for toolbar enabled models.
4144

4245

4346
.. important::

0 commit comments

Comments
 (0)