Skip to content

Commit

Permalink
Fix thumbnail selection in post form (#478)
Browse files Browse the repository at this point in the history
check that initial thumbnail fields are empty before overriding their value
  • Loading branch information
yakky committed Mar 5, 2019
1 parent 4cc7e93 commit d692da3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
10 changes: 6 additions & 4 deletions djangocms_blog/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ def __init__(self, *args, **kwargs):
self.fields['app_config'].widget.can_add_related = False

if self.app_config:
self.initial['main_image_full'] = \
self.app_config.app_data['config'].get('default_image_full')
self.initial['main_image_thumbnail'] = \
self.app_config.app_data['config'].get('default_image_thumbnail')
if not self.initial.get('main_image_full', ''):
self.initial['main_image_full'] = \
self.app_config.app_data['config'].get('default_image_full')
if not self.initial.get('main_image_thumbnail', ''):
self.initial['main_image_thumbnail'] = \
self.app_config.app_data['config'].get('default_image_thumbnail')
43 changes: 43 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,49 @@ def setUp(self):
upscale=True,
)

def test_admin_thumbnails(self):
self.get_pages()

custom_thumbnail = ThumbnailOption.objects.create(
name='Custom thumbnail', width=120, height=120, crop=True, upscale=True,
)
custom_full = ThumbnailOption.objects.create(
name='Custom image', width=800, height=200, crop=True, upscale=True,
)

post_admin = admin.site._registry[Post]
request = self.get_page_request('/', self.user, r'/en/blog/', edit=False)

post = self._get_post(self._post_data[0]['en'])
post = self._get_post(self._post_data[0]['it'], post, 'it')

response = post_admin.change_view(request, str(post.pk))
response.render()
self.assertRegexpMatches(force_text(response.content), r'[^>]*>Custom image')
self.assertRegexpMatches(force_text(response.content), r'[^>]*>Custom thumbnail')
self.assertRegexpMatches(force_text(response.content), r'[^>]*>Blog image')
self.assertRegexpMatches(force_text(response.content), r'[^>]*>Blog thumbnail')

post.main_image_full = custom_full
post.main_image_thumbnail = custom_thumbnail
post.save()
response = post_admin.change_view(request, str(post.pk))
response.render()
self.assertRegexpMatches(force_text(response.content), r'selected[^>]*>Custom image')
self.assertRegexpMatches(force_text(response.content), r'selected[^>]*>Custom thumbnail')

self.app_config_1.app_data.config.default_image_full = self.default_full
self.app_config_1.app_data.config.default_image_thumbnail = self.default_thumbnail
self.app_config_1.save()
post.main_image_full = None
post.main_image_thumbnail = None
post.save()

response = post_admin.change_view(request, str(post.pk))
response.render()
self.assertRegexpMatches(force_text(response.content), r'selected[^>]*>Blog image')
self.assertRegexpMatches(force_text(response.content), r'selected[^>]*>Blog thumbnail')

def test_admin_post_views(self):
self.get_pages()

Expand Down

0 comments on commit d692da3

Please sign in to comment.