1
0
Fork 0

Ensure transient cover_url field value gets persisted across form renders

Given this field doesn't map to an `Edition` model field it lost its values when re-rendering the form.
It worked only when the form was valid and rendered as part of the confirmation screen, which is due to
the context data value being set in `add_authors` which was only getting called after the form validation.

I've opted to pull it out into a separate new function that gets called before form validation.
This commit is contained in:
Christof Dorner 2023-01-04 17:45:01 +01:00
parent 2bd94b1332
commit 1c6548a0ad
2 changed files with 18 additions and 1 deletions

View file

@ -82,6 +82,7 @@ class EditBookViews(TestCase):
form = forms.EditionForm(instance=self.book)
form.data["title"] = ""
form.data["last_edited_by"] = self.local_user.id
form.data["cover-url"] = "http://local.host/cover.jpg"
request = self.factory.post("", form.data)
request.user = self.local_user
@ -91,6 +92,10 @@ class EditBookViews(TestCase):
# Title is unchanged
self.book.refresh_from_db()
self.assertEqual(self.book.title, "Example Edition")
# transient field values are set correctly
self.assertEqual(
result.context_data["cover_url"], "http://local.host/cover.jpg"
)
def test_edit_book_add_author(self):
"""lets a user edit a book with new authors"""
@ -280,9 +285,14 @@ class EditBookViews(TestCase):
form = forms.EditionForm(instance=self.book)
form.data["title"] = ""
form.data["last_edited_by"] = self.local_user.id
form.data["cover-url"] = "http://local.host/cover.jpg"
request = self.factory.post("", form.data)
request.user = self.local_user
result = view(request)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
# transient field values are set correctly
self.assertEqual(
result.context_data["cover_url"], "http://local.host/cover.jpg"
)