kopia lustrzana https://github.com/wagtail/wagtail
Output form media on add/edit/chooser document forms with custom models
Similar to eaad013081
, but for Document
pull/5594/head^2
rodzic
4d2956f7e7
commit
b9c470df37
|
@ -33,6 +33,7 @@ Changelog
|
|||
* Fix: Specific page permission checks are now enforced when viewing a page revision (Andy Chosak)
|
||||
* Fix: `pageurl` and `slugurl` tags no longer fail when `request.site` is `None` (Samir Shah)
|
||||
* Fix: Output form media on add/edit image forms with custom models (Matt Westcott)
|
||||
* Fix: Output form media on add/edit document forms with custom models (Sergey Fedoseev)
|
||||
* Fix: Layout for the clear checkbox in default FileField widget (Mikalai Radchuk)
|
||||
* Fix: Remove ASCII conversion from Postgres search backend, to support stemming in non-Latin alphabets (Pavel Denisov)
|
||||
* Fix: Prevent tab labels on page edit view from being cut off on very narrow screens (Kevin Howbrook)
|
||||
|
|
|
@ -57,6 +57,7 @@ Bug fixes
|
|||
* Specific page permission checks are now enforced when viewing a page revision (Andy Chosak)
|
||||
* ``pageurl`` and ``slugurl`` tags no longer fail when ``request.site`` is ``None`` (Samir Shah)
|
||||
* Output form media on add/edit image forms with custom models (Matt Westcott)
|
||||
* Output form media on add/edit document forms with custom models (Sergey Fedoseev)
|
||||
* Fixes layout for the clear checkbox in default FileField widget (Mikalai Radchuk)
|
||||
* Remove ASCII conversion from Postgres search backend, to support stemming in non-Latin alphabets (Pavel Denisov)
|
||||
* Prevent tab labels on page edit view from being cut off on very narrow screens (Kevin Howbrook)
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
{% trans "Choose a document" as choose_str %}
|
||||
{% include "wagtailadmin/shared/header.html" with title=choose_str tabbed=1 merged=1 icon="doc-full-inverse" %}
|
||||
|
||||
{{ uploadform.media.js }}
|
||||
{{ uploadform.media.css }}
|
||||
|
||||
{% if uploadform %}
|
||||
<ul class="tab-nav merged">
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
{% block extra_js %}
|
||||
{{ block.super }}
|
||||
|
||||
{{ form.media.js }}
|
||||
|
||||
{% url 'wagtailadmin_tag_autocomplete' as autocomplete_url %}
|
||||
<script>
|
||||
$(function() {
|
||||
|
@ -16,6 +18,11 @@
|
|||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_css %}
|
||||
{{ block.super }}
|
||||
{{ form.media.css }}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% trans "Add document" as add_str %}
|
||||
{% include "wagtailadmin/shared/header.html" with title=add_str icon="doc-full-inverse" %}
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
{% block extra_js %}
|
||||
{{ block.super }}
|
||||
|
||||
{{ form.media.js }}
|
||||
|
||||
{% url 'wagtailadmin_tag_autocomplete' as autocomplete_url %}
|
||||
<script>
|
||||
$(function() {
|
||||
|
@ -16,6 +18,11 @@
|
|||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block extra_css %}
|
||||
{{ block.super }}
|
||||
{{ form.media.css }}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% trans "Editing" as editing_str %}
|
||||
{% include "wagtailadmin/shared/header.html" with title=editing_str subtitle=document.title icon="doc-full-inverse" %}
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
{% block extra_css %}
|
||||
{{ block.super }}
|
||||
|
||||
{{ form_media.css }}
|
||||
|
||||
<link rel="stylesheet" href="{% static 'wagtaildocs/css/add-multiple.css' %}" type="text/css" />
|
||||
{% endblock %}
|
||||
|
||||
|
@ -67,6 +69,8 @@
|
|||
{% block extra_js %}
|
||||
{{ block.super }}
|
||||
|
||||
{{ form_media.js }}
|
||||
|
||||
<!-- this exact order of plugins is vital -->
|
||||
<script src="{% static 'wagtailadmin/js/vendor/jquery.iframe-transport.js' %}"></script>
|
||||
<script src="{% static 'wagtailadmin/js/vendor/jquery.fileupload.js' %}"></script>
|
||||
|
|
|
@ -11,7 +11,7 @@ from django.urls import reverse
|
|||
from wagtail.core.models import Collection, GroupCollectionPermission, Page
|
||||
from wagtail.documents import models
|
||||
from wagtail.documents.tests.utils import get_test_document_file
|
||||
from wagtail.tests.testapp.models import EventPage, EventPageRelatedLink
|
||||
from wagtail.tests.testapp.models import CustomDocument, EventPage, EventPageRelatedLink
|
||||
from wagtail.tests.utils import WagtailTestUtils
|
||||
|
||||
|
||||
|
@ -115,6 +115,9 @@ class TestDocumentAddView(TestCase, WagtailTestUtils):
|
|||
# Ensure the form supports file uploads
|
||||
self.assertContains(response, 'enctype="multipart/form-data"')
|
||||
|
||||
# draftail should NOT be a standard JS include on this page
|
||||
self.assertNotContains(response, 'wagtailadmin/js/draftail.js')
|
||||
|
||||
def test_get_with_collections(self):
|
||||
root_collection = Collection.get_first_root_node()
|
||||
root_collection.add_child(name="Evil plans")
|
||||
|
@ -126,6 +129,21 @@ class TestDocumentAddView(TestCase, WagtailTestUtils):
|
|||
self.assertContains(response, '<label for="id_collection">')
|
||||
self.assertContains(response, "Evil plans")
|
||||
|
||||
@override_settings(WAGTAILDOCS_DOCUMENT_MODEL='tests.CustomDocument')
|
||||
def test_get_with_custom_document_model(self):
|
||||
response = self.client.get(reverse('wagtaildocs:add'))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, 'wagtaildocs/documents/add.html')
|
||||
|
||||
# Ensure the form supports file uploads
|
||||
self.assertContains(response, 'enctype="multipart/form-data"')
|
||||
|
||||
# custom fields should be included
|
||||
self.assertContains(response, 'name="fancy_description"')
|
||||
|
||||
# form media should be imported
|
||||
self.assertContains(response, 'wagtailadmin/js/draftail.js')
|
||||
|
||||
def test_post(self):
|
||||
# Build a fake file
|
||||
fake_file = get_test_document_file()
|
||||
|
@ -259,6 +277,11 @@ class TestDocumentEditView(TestCase, WagtailTestUtils):
|
|||
# Ensure the form supports file uploads
|
||||
self.assertContains(response, 'enctype="multipart/form-data"')
|
||||
|
||||
# draftail should NOT be a standard JS include on this page
|
||||
# (see TestDocumentEditViewWithCustomDocumentModel - this confirms that form media
|
||||
# definitions are being respected)
|
||||
self.assertNotContains(response, 'wagtailadmin/js/draftail.js')
|
||||
|
||||
def test_post(self):
|
||||
# Build a fake file
|
||||
fake_file = get_test_document_file()
|
||||
|
@ -339,6 +362,34 @@ class TestDocumentEditView(TestCase, WagtailTestUtils):
|
|||
b'An updated test content.')
|
||||
|
||||
|
||||
@override_settings(WAGTAILDOCS_DOCUMENT_MODEL='tests.CustomDocument')
|
||||
class TestDocumentEditViewWithCustomDocumentModel(TestCase, WagtailTestUtils):
|
||||
def setUp(self):
|
||||
self.login()
|
||||
|
||||
# Create a document to edit
|
||||
self.document = CustomDocument.objects.create(
|
||||
title="Test document",
|
||||
file=get_test_document_file(),
|
||||
)
|
||||
|
||||
self.storage = self.document.file.storage
|
||||
|
||||
def get(self, params={}):
|
||||
return self.client.get(reverse('wagtaildocs:edit', args=(self.document.id,)), params)
|
||||
|
||||
def test_get_with_custom_document_model(self):
|
||||
response = self.get()
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, 'wagtaildocs/documents/edit.html')
|
||||
|
||||
# Ensure the form supports file uploads
|
||||
self.assertContains(response, 'enctype="multipart/form-data"')
|
||||
|
||||
# form media should be imported
|
||||
self.assertContains(response, 'wagtailadmin/js/draftail.js')
|
||||
|
||||
|
||||
class TestDocumentDeleteView(TestCase, WagtailTestUtils):
|
||||
def setUp(self):
|
||||
self.login()
|
||||
|
@ -393,6 +444,10 @@ class TestMultipleDocumentUploader(TestCase, WagtailTestUtils):
|
|||
self.assertEqual(self.doc.title, "New title!")
|
||||
self.assertFalse(self.doc.tags.all())
|
||||
|
||||
def check_form_media_in_response(self, response):
|
||||
# draftail should NOT be a standard JS include on this page
|
||||
self.assertNotContains(response, 'wagtailadmin/js/draftail.js')
|
||||
|
||||
def test_add(self):
|
||||
"""
|
||||
This tests that the add view responds correctly on a GET request
|
||||
|
@ -407,6 +462,8 @@ class TestMultipleDocumentUploader(TestCase, WagtailTestUtils):
|
|||
# no collection chooser when only one collection exists
|
||||
self.assertNotContains(response, '<label for="id_adddocument_collection">')
|
||||
|
||||
self.check_form_media_in_response(response)
|
||||
|
||||
def test_add_with_collections(self):
|
||||
root_collection = Collection.get_first_root_node()
|
||||
root_collection.add_child(name="Evil plans")
|
||||
|
@ -657,6 +714,10 @@ class TestMultipleCustomDocumentUploader(TestMultipleDocumentUploader):
|
|||
super().check_doc_after_edit()
|
||||
self.assertEqual(self.doc.description, "New description.")
|
||||
|
||||
def check_form_media_in_response(self, response):
|
||||
# form media should be imported
|
||||
self.assertContains(response, 'wagtailadmin/js/draftail.js')
|
||||
|
||||
|
||||
class TestMultipleCustomDocumentUploaderNoCollection(TestMultipleCustomDocumentUploader):
|
||||
@classmethod
|
||||
|
@ -684,6 +745,23 @@ class TestDocumentChooserView(TestCase, WagtailTestUtils):
|
|||
response_json = json.loads(response.content.decode())
|
||||
self.assertEqual(response_json['step'], 'chooser')
|
||||
|
||||
# draftail should NOT be a standard JS include on this page
|
||||
self.assertNotIn('wagtailadmin/js/draftail.js', response_json['html'])
|
||||
|
||||
@override_settings(WAGTAILDOCS_DOCUMENT_MODEL='tests.CustomDocument')
|
||||
def test_with_custom_document_model(self):
|
||||
response = self.client.get(reverse('wagtaildocs:chooser'))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
response_json = json.loads(response.content.decode())
|
||||
self.assertEqual(response_json['step'], 'chooser')
|
||||
self.assertTemplateUsed(response, 'wagtaildocs/chooser/chooser.html')
|
||||
|
||||
# custom form fields should be present
|
||||
self.assertIn('name="document-chooser-upload-fancy_description"', response_json['html'])
|
||||
|
||||
# form media imports should appear on the page
|
||||
self.assertIn('wagtailadmin/js/draftail.js', response_json['html'])
|
||||
|
||||
def test_search(self):
|
||||
response = self.client.get(reverse('wagtaildocs:chooser'), {'q': "Hello"})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
|
|
@ -79,12 +79,15 @@ def add(request):
|
|||
'error_message': '\n'.join(['\n'.join([force_str(i) for i in v]) for k, v in form.errors.items()]),
|
||||
})
|
||||
else:
|
||||
# Instantiate a dummy copy of the form that we can retrieve validation messages and media from;
|
||||
# actual rendering of forms will happen on AJAX POST rather than here
|
||||
form = DocumentForm(user=request.user)
|
||||
|
||||
return render(request, 'wagtaildocs/multiple/add.html', {
|
||||
'help_text': form.fields['file'].help_text,
|
||||
'collections': collections_to_choose,
|
||||
})
|
||||
return render(request, 'wagtaildocs/multiple/add.html', {
|
||||
'help_text': form.fields['file'].help_text,
|
||||
'collections': collections_to_choose,
|
||||
'form_media': form.media,
|
||||
})
|
||||
|
||||
|
||||
@require_POST
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.2.5 on 2019-09-27 14:45
|
||||
|
||||
from django.db import migrations
|
||||
import wagtail.core.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('tests', '0042_simplechildpage_simpleparentpage'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='customdocument',
|
||||
name='fancy_description',
|
||||
field=wagtail.core.fields.RichTextField(blank=True),
|
||||
),
|
||||
]
|
|
@ -920,8 +920,10 @@ class CustomRendition(AbstractRendition):
|
|||
|
||||
class CustomDocument(AbstractDocument):
|
||||
description = models.TextField(blank=True)
|
||||
fancy_description = RichTextField(blank=True)
|
||||
admin_form_fields = Document.admin_form_fields + (
|
||||
'description',
|
||||
'fancy_description'
|
||||
)
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue