mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-21 05:21:54 +00:00
Pass all necessary template context to chooser view after upload validation error
Fixes #4548
This commit is contained in:
parent
c4c6ec1983
commit
d3970f06cd
4 changed files with 52 additions and 18 deletions
|
|
@ -5,6 +5,7 @@ Changelog
|
|||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
* Fix: Site.get_site_root_paths() preferring other sites over the default when some sites share the same root_page (Andy Babic)
|
||||
* Fix: Rich text image chooser no longer skips format selection after a validation error (Matt Westcott)
|
||||
|
||||
|
||||
2.1 (22.05.2018)
|
||||
|
|
|
|||
|
|
@ -14,3 +14,4 @@ Bug fixes
|
|||
~~~~~~~~~
|
||||
|
||||
* Fix ``Site.get_site_root_paths()`` preferring other sites over the default when some sites share the same root_page (Andy Babic)
|
||||
* Rich text image chooser no longer skips format selection after a validation error (Matt Westcott)
|
||||
|
|
|
|||
|
|
@ -716,6 +716,22 @@ class TestImageChooserUploadView(TestCase, WagtailTestUtils):
|
|||
self.assertContains(response, "Page 1 of ")
|
||||
self.assertEqual(12, len(response.context['images']))
|
||||
|
||||
def test_select_format_flag_after_upload_form_error(self):
|
||||
submit_url = reverse('wagtailimages:chooser_upload') + '?select_format=true'
|
||||
response = self.client.post(submit_url, {
|
||||
'title': "Test image",
|
||||
'file': SimpleUploadedFile('not_an_image.txt', b'this is not an image'),
|
||||
})
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, 'wagtailimages/chooser/chooser.html')
|
||||
self.assertFormError(response, 'uploadform', 'file', "Not a supported image format. Supported formats: GIF, JPEG, PNG.")
|
||||
|
||||
# the action URL of the re-rendered form should include the select_format=true parameter
|
||||
# (NB the HTML in the response is embedded in a JS string, so need to escape accordingly)
|
||||
expected_action_attr = 'action=\\"%s\\"' % submit_url
|
||||
self.assertContains(response, expected_action_attr)
|
||||
|
||||
@override_settings(DEFAULT_FILE_STORAGE='wagtail.tests.dummy_external_storage.DummyExternalStorage')
|
||||
def test_upload_with_external_storage(self):
|
||||
response = self.client.post(reverse('wagtailimages:chooser_upload'), {
|
||||
|
|
|
|||
|
|
@ -37,6 +37,23 @@ def get_image_json(image):
|
|||
})
|
||||
|
||||
|
||||
def get_chooser_context(request):
|
||||
"""Helper function to return common template context variables for the main chooser view"""
|
||||
|
||||
collections = Collection.objects.all()
|
||||
if len(collections) < 2:
|
||||
collections = None
|
||||
|
||||
return {
|
||||
'searchform': SearchForm(),
|
||||
'is_searching': False,
|
||||
'query_string': None,
|
||||
'will_select_format': request.GET.get('select_format'),
|
||||
'popular_tags': popular_tags_for_model(get_image_model()),
|
||||
'collections': collections,
|
||||
}
|
||||
|
||||
|
||||
def chooser(request):
|
||||
Image = get_image_model()
|
||||
|
||||
|
|
@ -52,7 +69,6 @@ def chooser(request):
|
|||
for hook in hooks.get_hooks('construct_image_chooser_queryset'):
|
||||
images = hook(images, request)
|
||||
|
||||
q = None
|
||||
if (
|
||||
'q' in request.GET or 'p' in request.GET or 'tag' in request.GET or
|
||||
'collection_id' in request.GET
|
||||
|
|
@ -71,6 +87,7 @@ def chooser(request):
|
|||
is_searching = True
|
||||
else:
|
||||
is_searching = False
|
||||
q = None
|
||||
|
||||
tag_name = request.GET.get('tag')
|
||||
if tag_name:
|
||||
|
|
@ -86,24 +103,16 @@ def chooser(request):
|
|||
'will_select_format': request.GET.get('select_format')
|
||||
})
|
||||
else:
|
||||
searchform = SearchForm()
|
||||
|
||||
collections = Collection.objects.all()
|
||||
if len(collections) < 2:
|
||||
collections = None
|
||||
|
||||
paginator, images = paginate(request, images, per_page=12)
|
||||
|
||||
return render_modal_workflow(request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', {
|
||||
context = get_chooser_context(request)
|
||||
context.update({
|
||||
'images': images,
|
||||
'uploadform': uploadform,
|
||||
'searchform': searchform,
|
||||
'is_searching': False,
|
||||
'query_string': q,
|
||||
'will_select_format': request.GET.get('select_format'),
|
||||
'popular_tags': popular_tags_for_model(Image),
|
||||
'collections': collections,
|
||||
})
|
||||
return render_modal_workflow(
|
||||
request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', context
|
||||
)
|
||||
|
||||
|
||||
def image_chosen(request, image_id):
|
||||
|
|
@ -120,8 +129,6 @@ def chooser_upload(request):
|
|||
Image = get_image_model()
|
||||
ImageForm = get_image_form(Image)
|
||||
|
||||
searchform = SearchForm()
|
||||
|
||||
if request.method == 'POST':
|
||||
image = Image(uploaded_by_user=request.user)
|
||||
form = ImageForm(request.POST, request.FILES, instance=image, user=request.user)
|
||||
|
|
@ -148,11 +155,20 @@ def chooser_upload(request):
|
|||
form = ImageForm(user=request.user)
|
||||
|
||||
images = Image.objects.order_by('-created_at')
|
||||
|
||||
# allow hooks to modify the queryset
|
||||
for hook in hooks.get_hooks('construct_image_chooser_queryset'):
|
||||
images = hook(images, request)
|
||||
|
||||
paginator, images = paginate(request, images, per_page=12)
|
||||
|
||||
context = get_chooser_context(request)
|
||||
context.update({
|
||||
'images': images,
|
||||
'uploadform': form,
|
||||
})
|
||||
return render_modal_workflow(
|
||||
request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js',
|
||||
{'images': images, 'uploadform': form, 'searchform': searchform}
|
||||
request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', context
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue