0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Add collection field to multiple doc uploader

This commit is contained in:
Matt Westcott 2016-02-19 14:14:19 +00:00
parent 569178025c
commit f0677525d9
4 changed files with 105 additions and 7 deletions

View File

@ -36,7 +36,8 @@ def get_document_form(model):
def get_document_multi_form(model):
return modelform_factory(
model,
fields=['title', 'tags'],
form=BaseDocumentForm,
fields=['title', 'collection', 'tags'],
widgets={
'tags': widgets.AdminTagWidget,
'file': forms.FileInput()

View File

@ -22,6 +22,18 @@
<input id="fileupload" type="file" name="files[]" data-url="{% url 'wagtaildocs:add_multiple' %}" multiple>
</div>
{% csrf_token %}
{% if collections %}
<div class="field">
<label for="id_adddocument_collection">{% trans "Add to collection:" %}</label>
<div class="field-content">
<select id="id_adddocument_collection" name="collection">
{% for collection in collections %}
<option value="{{ collection.id }}">{{ collection.name }}</option>
{% endfor %}
</select>
</div>
</div>
{% endif %}
</form>
</div>

View File

@ -399,6 +399,24 @@ class TestMultipleDocumentUploader(TestCase, WagtailTestUtils):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtaildocs/multiple/add.html')
# no collection chooser when only one collection exists
self.assertNotContains(response, '<label for="id_adddocument_collection">')
def test_add_with_collections(self):
root_collection = Collection.get_first_root_node()
root_collection.add_child(name="Evil plans")
# Send request
response = self.client.get(reverse('wagtaildocs:add_multiple'))
# Check response
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtaildocs/multiple/add.html')
# collection chooser should exisst
self.assertContains(response, '<label for="id_adddocument_collection">')
self.assertContains(response, 'Evil plans')
def test_add_post(self):
"""
This tests that a POST request to the add view saves the document and returns an edit form
@ -417,6 +435,11 @@ class TestMultipleDocumentUploader(TestCase, WagtailTestUtils):
self.assertEqual(response.context['doc'].title, 'test.png')
self.assertTrue(response.context['doc'].file_size)
# check that it is in the root collection
doc = Document.objects.get(title='test.png')
root_collection = Collection.get_first_root_node()
self.assertEqual(doc.collection, root_collection)
# Check form
self.assertIn('form', response.context)
self.assertEqual(response.context['form'].initial['title'], 'test.png')
@ -429,6 +452,53 @@ class TestMultipleDocumentUploader(TestCase, WagtailTestUtils):
self.assertEqual(response_json['doc_id'], response.context['doc'].id)
self.assertTrue(response_json['success'])
# form should not contain a collection chooser
self.assertNotIn('Collection', response_json['form'])
def test_add_post_with_collections(self):
"""
This tests that a POST request to the add view saves the document
and returns an edit form, when collections are active
"""
root_collection = Collection.get_first_root_node()
evil_plans_collection = root_collection.add_child(name="Evil plans")
response = self.client.post(reverse('wagtaildocs:add_multiple'), {
'files[]': SimpleUploadedFile('test.png', b"Simple text document"),
'collection': evil_plans_collection.id
}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
# Check response
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'application/json')
self.assertTemplateUsed(response, 'wagtaildocs/multiple/edit_form.html')
# Check document
self.assertIn('doc', response.context)
self.assertEqual(response.context['doc'].title, 'test.png')
self.assertTrue(response.context['doc'].file_size)
# check that it is in the 'evil plans' collection
doc = Document.objects.get(title='test.png')
root_collection = Collection.get_first_root_node()
self.assertEqual(doc.collection, evil_plans_collection)
# Check form
self.assertIn('form', response.context)
self.assertEqual(response.context['form'].initial['title'], 'test.png')
# Check JSON
response_json = json.loads(response.content.decode())
self.assertIn('doc_id', response_json)
self.assertIn('form', response_json)
self.assertIn('success', response_json)
self.assertEqual(response_json['doc_id'], response.context['doc'].id)
self.assertTrue(response_json['success'])
# form should contain a collection chooser
self.assertIn('Collection', response_json['form'])
def test_add_post_noajax(self):
"""
This tests that only AJAX requests are allowed to POST to the add view

View File

@ -24,6 +24,13 @@ def add(request):
DocumentForm = get_document_form(Document)
DocumentMultiForm = get_document_multi_form(Document)
collections = permission_policy.collections_user_has_permission_for(request.user, 'add')
if len(collections) > 1:
collections_to_choose = collections
else:
# no need to show a collections chooser
collections_to_choose = None
if request.method == 'POST':
if not request.is_ajax():
return HttpResponseBadRequest("Cannot POST to this view without AJAX")
@ -32,9 +39,12 @@ def add(request):
return HttpResponseBadRequest("Must upload a file")
# Build a form for validation
form = DocumentForm(
{'title': request.FILES['files[]'].name},
{'file': request.FILES['files[]']})
form = DocumentForm({
'title': request.FILES['files[]'].name,
'collection': request.POST.get('collection'),
}, {
'file': request.FILES['files[]']
}, user=request.user)
if form.is_valid():
# Save it
@ -49,7 +59,9 @@ def add(request):
'doc_id': int(doc.id),
'form': render_to_string('wagtaildocs/multiple/edit_form.html', {
'doc': doc,
'form': DocumentMultiForm(instance=doc, prefix='doc-%d' % doc.id),
'form': DocumentMultiForm(
instance=doc, prefix='doc-%d' % doc.id, user=request.user
),
}, request=request),
})
else:
@ -61,10 +73,11 @@ def add(request):
'error_message': '\n'.join(['\n'.join([force_text(i) for i in v]) for k, v in form.errors.items()]),
})
else:
form = DocumentForm()
form = DocumentForm(user=request.user)
return render(request, 'wagtaildocs/multiple/add.html', {
'help_text': form.fields['file'].help_text,
'collections': collections_to_choose,
})
@ -81,7 +94,9 @@ def edit(request, doc_id, callback=None):
if not permission_policy.user_has_permission_for_instance(request.user, 'change', doc):
raise PermissionDenied
form = DocumentMultiForm(request.POST, request.FILES, instance=doc, prefix='doc-' + doc_id)
form = DocumentMultiForm(
request.POST, request.FILES, instance=doc, prefix='doc-' + doc_id, user=request.user
)
if form.is_valid():
form.save()