Pass the correct for_update value for get_form_class in SnippetViewSet's edit views (#10445)

Fixes #10438
pull/10454/head
Sage Abdullah 2023-05-16 12:49:24 +01:00 zatwierdzone przez Matt Westcott
rodzic cc30fd3a13
commit 188bbbb0c5
5 zmienionych plików z 29 dodań i 3 usunięć

Wyświetl plik

@ -22,6 +22,7 @@ Changelog
* Fix: Ensure that copying page correctly picks up the latest revision (Matt Westcott)
* Fix: Ensure comment buttons always respect `WAGTAILADMIN_COMMENTS_ENABLED` (Thibaud Colas)
* Fix: Fix error when deleting a single snippet through the bulk actions interface (Sage Abdullah)
* Fix: Pass the correct `for_update` value for `get_form_class` in `SnippetViewSet` edit views (Sage Abdullah)
* Docs: Update documentation for `log_action` parameter on `RevisionMixin.save_revision` (Christer Jensen)

Wyświetl plik

@ -17,6 +17,7 @@ depth: 1
* Ensure that copying page correctly picks up the latest revision (Matt Westcott)
* Ensure comment buttons always respect `WAGTAILADMIN_COMMENTS_ENABLED` (Thibaud Colas)
* Fix error when deleting a single snippet through the bulk actions interface (Sage Abdullah)
* Pass the correct `for_update` value for `get_form_class` in `SnippetViewSet` edit views (Sage Abdullah)
### Documentation

Wyświetl plik

@ -959,3 +959,20 @@ class TestMenuItemRegistration(BaseSnippetViewSetTests):
menu_items = admin_menu.render_component(self.request)
snippets = [item for item in menu_items if item.name == "snippets"]
self.assertEqual(len(snippets), 0)
class TestCustomFormClass(BaseSnippetViewSetTests):
model = DraftStateModel
def test_get_form_class(self):
add_view = self.client.get(self.get_url("add"))
self.assertNotContains(add_view, '<input type="text" name="text"')
self.assertContains(add_view, '<textarea name="text"')
obj = self.model.objects.create(text="Hello World")
# The get_form_class has been overridden to replace the widget for the
# text field with a TextInput, but only for the edit view
edit_view = self.client.get(self.get_url("edit", args=(quote(obj.pk),)))
self.assertContains(edit_view, '<input type="text" name="text"')
self.assertNotContains(edit_view, '<textarea name="text"')

Wyświetl plik

@ -901,7 +901,7 @@ class SnippetViewSet(ModelViewSet):
header_icon=self.icon,
permission_policy=self.permission_policy,
panel=self._edit_handler,
form_class=self.get_form_class(),
form_class=self.get_form_class(for_update=True),
index_url_name=self.get_url_name("list"),
edit_url_name=self.get_url_name("edit"),
delete_url_name=self.get_url_name("delete"),
@ -971,7 +971,7 @@ class SnippetViewSet(ModelViewSet):
header_icon=self.icon,
permission_policy=self.permission_policy,
panel=self._edit_handler,
form_class=self.get_form_class(),
form_class=self.get_form_class(for_update=True),
index_url_name=self.get_url_name("list"),
edit_url_name=self.get_url_name("edit"),
delete_url_name=self.get_url_name("delete"),
@ -1044,7 +1044,7 @@ class SnippetViewSet(ModelViewSet):
def preview_on_edit_view(self):
return self.preview_on_edit_view_class.as_view(
model=self.model,
form_class=self.get_form_class(),
form_class=self.get_form_class(for_update=True),
)
@property

Wyświetl plik

@ -1,3 +1,4 @@
from django import forms
from django.http import HttpResponse
from django.utils.safestring import mark_safe
@ -315,6 +316,12 @@ class DraftStateModelViewSet(SnippetViewSet):
PublishingPanel(),
]
def get_form_class(self, for_update=False):
form_class = super().get_form_class(for_update)
if for_update:
form_class.base_fields["text"].widget = forms.TextInput()
return form_class
class ModeratedModelViewSet(SnippetViewSet):
model = ModeratedModel