diff --git a/wagtail/documents/templates/wagtaildocs/chooser/chooser.js b/wagtail/documents/templates/wagtaildocs/chooser/chooser.js
index 026f54da97..c84626d5aa 100644
--- a/wagtail/documents/templates/wagtaildocs/chooser/chooser.js
+++ b/wagtail/documents/templates/wagtaildocs/chooser/chooser.js
@@ -1,5 +1,4 @@
-{% load i18n %}
-function(modal) {
+function(modal, jsonData) {
function ajaxifyLinks (context) {
$('a.document-choice', context).on('click', function() {
modal.loadUrl(this.href);
@@ -63,12 +62,10 @@ function(modal) {
modal.loadResponseText(response);
},
error: function(response, textStatus, errorThrown) {
- {% trans "Server Error" as error_label %}
- {% trans "Report this error to your webmaster with the following information:" as error_message %}
- message = '{{ error_message|escapejs }}
' + errorThrown + ' - ' + response.status;
+ message = jsonData['error_message'] + '
' + errorThrown + ' - ' + response.status;
$('#upload').append(
'
' +
- '{{ error_label|escapejs }}: ' + message + '
');
+ '' + jsonData['error_label'] + ': ' + message + '');
}
});
@@ -85,8 +82,7 @@ function(modal) {
$('#collection_chooser_collection_id').on('change', search);
- {% url 'wagtailadmin_tag_autocomplete' as autocomplete_url %}
$('#id_tags', modal.body).tagit({
- autocomplete: {source: "{{ autocomplete_url|addslashes }}"}
+ autocomplete: {source: jsonData['tag_autocomplete_url']}
});
}
diff --git a/wagtail/documents/templates/wagtaildocs/chooser/document_chosen.js b/wagtail/documents/templates/wagtaildocs/chooser/document_chosen.js
index 45dc7ebd20..a86daf60f8 100644
--- a/wagtail/documents/templates/wagtaildocs/chooser/document_chosen.js
+++ b/wagtail/documents/templates/wagtaildocs/chooser/document_chosen.js
@@ -1,4 +1,4 @@
-function(modal) {
- modal.respond('documentChosen', {{ document_json|safe }});
+function(modal, jsonData) {
+ modal.respond('documentChosen', jsonData['result']);
modal.close();
}
diff --git a/wagtail/documents/views/chooser.py b/wagtail/documents/views/chooser.py
index 26d0fa2537..6699efbddd 100644
--- a/wagtail/documents/views/chooser.py
+++ b/wagtail/documents/views/chooser.py
@@ -1,7 +1,6 @@
-import json
-
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
+from django.utils.translation import ugettext as _
from wagtail.admin.forms import SearchForm
from wagtail.admin.modal_workflow import render_modal_workflow
@@ -17,19 +16,28 @@ from wagtail.utils.pagination import paginate
permission_checker = PermissionPolicyChecker(permission_policy)
-def get_document_json(document):
+def get_chooser_context():
+ """construct context variables needed by the chooser JS"""
+ return {
+ 'error_label': _("Server Error"),
+ 'error_message': _("Report this error to your webmaster with the following information:"),
+ 'tag_autocomplete_url': reverse('wagtailadmin_tag_autocomplete'),
+ }
+
+
+def get_document_result_data(document):
"""
- helper function: given a document, return the json to pass back to the
+ helper function: given a document, return the json data to pass back to the
chooser panel
"""
- return json.dumps({
+ return {
'id': document.id,
'title': document.title,
'url': document.url,
'filename': document.filename,
'edit_link': reverse('wagtaildocs:edit', args=(document.id,)),
- })
+ }
def chooser(request):
@@ -88,7 +96,7 @@ def chooser(request):
'searchform': searchform,
'collections': collections,
'is_searching': False,
- })
+ }, json_data=get_chooser_context())
def document_chosen(request, document_id):
@@ -96,7 +104,7 @@ def document_chosen(request, document_id):
return render_modal_workflow(
request, None, 'wagtaildocs/chooser/document_chosen.js',
- {'document_json': get_document_json(document)}
+ None, json_data={'result': get_document_result_data(document)}
)
@@ -119,7 +127,7 @@ def chooser_upload(request):
return render_modal_workflow(
request, None, 'wagtaildocs/chooser/document_chosen.js',
- {'document_json': get_document_json(document)}
+ None, json_data={'result': get_document_result_data(document)}
)
else:
form = DocumentForm(user=request.user)
@@ -128,5 +136,6 @@ def chooser_upload(request):
return render_modal_workflow(
request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js',
- {'documents': documents, 'uploadform': form}
+ {'documents': documents, 'uploadform': form},
+ json_data=get_chooser_context()
)