Skip to content

Commit

Permalink
Fixing image preview on form validation errors #22
Browse files Browse the repository at this point in the history
Includes:
* Adding new bound_data method to VersatileImagePPOIClickField
* Updating test models & fixture
* Adding test coverage
  • Loading branch information
respondcreate committed Sep 25, 2015
1 parent 01b0203 commit c54e56e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ docs/_build/
# Coverage Reports #
##################
.coverage
htmlcov
htmlcov/*

# Generated Image #
#################
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/versatileimagefield.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@
"optional_image": "exif-orientation-examples/Landscape_8.jpg",
"optional_image_with_ppoi": "exif-orientation-examples/Landscape_6.jpg",
"ppoi": "0.5x0.5",
"optional_image_with_ppoi_ppoi": "1.0x1.0"
"optional_image_with_ppoi_ppoi": "1.0x1.0",
"required_text_field": "boo urns"
},
"model": "tests.versatileimagewidgettestmodel",
"pk": 1
Expand Down
3 changes: 3 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@ class VersatileImageWidgetTestModel(models.Model):
blank=True,
ppoi_field='optional_image_with_ppoi_ppoi'
)
required_text_field = models.CharField(
max_length=20,
)
ppoi = PPOIField()
optional_image_with_ppoi_ppoi = PPOIField()
24 changes: 24 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,3 +940,27 @@ def test_field_serialization(self):
}
]
)

def test_bound_form_data(self):
"Ensure fields return the correct data after form validation errors."
response = self.client.post(
'/admin/tests/versatileimagewidgettestmodel/1/',
{
'required_text_field': ''
},
follow=True
)
self.assertEqual(response.status_code, 200)
if six.PY2:
response_content = str(response.content)
else:
response_content = str(response.content, encoding='utf-8')
self.assertInHTML(
(
'<img src="/media/__sized__/python-logo-thumbnail-300x300.'
'png" id="image_0_imagepreview" data-hidden_field_id='
'"id_image_1" data-point_stage_id="image_0_point-stage" '
'data-ppoi_id="image_0_ppoi" class="sizedimage-preview"/>'
),
response_content
)
6 changes: 6 additions & 0 deletions versatileimagefield/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def __init__(self, *args, **kwargs):
tuple(fields), *args, **kwargs
)

def bound_data(self, data, initial):
to_return = data
if data[0] is None:
to_return = initial
return to_return


class SizedImageCenterpointClickDjangoAdminField(
VersatileImagePPOIClickField):
Expand Down

0 comments on commit c54e56e

Please sign in to comment.