2
0
Эх сурвалжийг харах

Eliminate template tags from document choooser JS

Matt Westcott 6 жил өмнө
parent
commit
b54846089b

+ 4 - 8
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 }}<br />' + errorThrown + ' - ' + response.status;
+                message = jsonData['error_message'] + '<br />' + errorThrown + ' - ' + response.status;
                 $('#upload').append(
                     '<div class="help-block help-critical">' +
-                    '<strong>{{ error_label|escapejs }}: </strong>' + message + '</div>');
+                    '<strong>' + jsonData['error_label'] + ': </strong>' + message + '</div>');
             }
         });
 
@@ -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']}
     });
 }

+ 2 - 2
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();
 }

+ 19 - 10
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()
     )