Repository: wagtail/wagtail

Issue:
Document Chooser with Populate Title
Hi, I found that Document Chooser cannot populate the updated file title but Image Chooser does.
I think its good to add this feature and its not hard to implement it because the function already exists in image-chooser-modal.js


Discussion:
The relevant bits in the image chooser code are:

https://github.com/wagtail/wagtail/blob/main/wagtail/images/static_src/wagtailimages/js/image-chooser-modal.js#L116-L130
Is this already done?
@akshay-kapase no. Feel free to pick this up.
Hi, Can I work on this issue?
FYI, #7534 enhances the image title population with an override function, it might be good to copy this approach for the document files.

See also #4945 for context

Resolving patch:
diff --git a/wagtail/documents/views/multiple.py b/wagtail/documents/views/multiple.py
--- a/wagtail/documents/views/multiple.py
+++ b/wagtail/documents/views/multiple.py
@@ -54,6 +54,15 @@ def save_object(self, form):
 
         return doc
 
+    def get_context_data(self, **kwargs):
+        context = super().get_context_data(**kwargs)
+
+        context.update({
+            'max_title_length': self.form.fields['title'].max_length,
+        })
+
+        return context
+
 
 class EditView(BaseEditView):
     permission_policy = permission_policy


Test patch:
diff --git a/wagtail/documents/tests/test_admin_views.py b/wagtail/documents/tests/test_admin_views.py
--- a/wagtail/documents/tests/test_admin_views.py
+++ b/wagtail/documents/tests/test_admin_views.py
@@ -687,6 +687,53 @@ def test_add_post(self):
         # form should not contain a collection chooser
         self.assertNotIn('Collection', response_json['form'])
 
+    def test_add_post_with_title(self):
+        """
+        This tests that a POST request to the add view saves the document with a suplied title and returns an edit form
+        """
+        response = self.client.post(reverse('wagtaildocs:add_multiple'), {
+            'title': '(TXT) test title',
+            'files[]': SimpleUploadedFile('test.txt', b"Simple text document"),
+        })
+
+        # Check response
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response['Content-Type'], 'application/json')
+        self.assertTemplateUsed(response, 'wagtailadmin/generic/multiple_upload/edit_form.html')
+
+        # Check document
+        self.assertIn('doc', response.context)
+        self.assertEqual(response.context['doc'].title, '(TXT) test title')
+        self.assertIn('.txt', response.context['doc'].filename)
+        self.assertTrue(response.context['doc'].file_size)
+        self.assertTrue(response.context['doc'].file_hash)
+        self.assertEqual(response.context['edit_action'], '/admin/documents/multiple/%d/' % response.context['doc'].id)
+        self.assertEqual(response.context['delete_action'], '/admin/documents/multiple/%d/delete/' % response.context['doc'].id)
+
+        # check that it is in the root collection
+        doc = get_document_model().objects.get(title='(TXT) test title')
+        root_collection = Collection.get_first_root_node()
+        self.assertEqual(doc.collection, root_collection)
+
+        # Check form
+        self.assertIn('form', response.context)
+        self.assertEqual(
+            set(response.context['form'].fields),
+            set(get_document_model().admin_form_fields) - {'file', 'collection'},
+        )
+        self.assertEqual(response.context['form'].initial['title'], '(TXT) test title')
+
+        # 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 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
@@ -924,6 +971,31 @@ def test_add_post(self):
         # form should not contain a collection chooser
         self.assertNotIn('Collection', response_json['form'])
 
+    def test_add_post_with_title(self):
+        """
+        This tests that a POST request to the add view saves the document with a suplied title and returns an edit form
+        """
+        response = self.client.post(reverse('wagtaildocs:add_multiple'), {
+            'title': '(TXT) test title',
+            'files[]': SimpleUploadedFile('test.txt', b"Simple text document"),
+        })
+
+        # Check response
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response['Content-Type'], 'application/json')
+        self.assertTemplateUsed(response, 'wagtailadmin/generic/multiple_upload/edit_form.html')
+
+        # Check document
+        self.assertIn('uploaded_document', response.context)
+        self.assertIn('.txt', response.context['uploaded_document'].file.name)
+
+        # Check JSON
+        response_json = json.loads(response.content.decode())
+        self.assertIn('uploaded_document_id', response_json)
+        self.assertIn('form', response_json)
+        self.assertEqual(response_json['uploaded_document_id'], response.context['uploaded_document'].id)
+        self.assertTrue(response_json['success'])
+
     def test_add_post_with_collections(self):
         """
         This tests that a POST request to the add view saves the document