diff options
| -rw-r--r-- | addons/web/static/src/js/fields/basic_fields.js | 3 | ||||
| -rw-r--r-- | addons/web/static/src/js/fields/relational_fields.js | 1 | ||||
| -rw-r--r-- | addons/web/static/src/js/views/kanban/kanban_record.js | 2 | ||||
| -rw-r--r-- | addons/web/static/src/xml/base.xml | 15 | ||||
| -rw-r--r-- | addons/web/static/tests/fields/basic_fields_tests.js | 44 | ||||
| -rw-r--r-- | addons/web/static/tests/fields/relational_fields_tests.js | 7 | ||||
| -rw-r--r-- | doc/reference/javascript_reference.rst | 12 |
7 files changed, 69 insertions, 15 deletions
diff --git a/addons/web/static/src/js/fields/basic_fields.js b/addons/web/static/src/js/fields/basic_fields.js index 2c3e03681db..350dc5f5674 100644 --- a/addons/web/static/src/js/fields/basic_fields.js +++ b/addons/web/static/src/js/fields/basic_fields.js @@ -1634,6 +1634,7 @@ var AbstractFieldBinary = AbstractField.extend({ this.fields = record.fields; this.useFileAPI = !!window.FileReader; this.max_upload_size = 64 * 1024 * 1024; // 64Mo + this.accepted_file_extensions = (this.nodeOptions && this.nodeOptions.accepted_file_extensions) || this.accepted_file_extensions || '*'; if (!this.useFileAPI) { var self = this; this.fileupload_id = _.uniqueId('o_fileupload'); @@ -1765,6 +1766,7 @@ var FieldBinaryImage = AbstractFieldBinary.extend({ 'i': 'png', 'P': 'svg+xml', }, + accepted_file_extensions: 'image/*', /** * Returns the image URL from a model. * @@ -1976,6 +1978,7 @@ var FieldPdfViewer = FieldBinaryFile.extend({ description: _lt("PDF Viewer"), supportedFieldTypes: ['binary'], template: 'FieldPdfViewer', + accepted_file_extensions: 'application/pdf', /** * @override */ diff --git a/addons/web/static/src/js/fields/relational_fields.js b/addons/web/static/src/js/fields/relational_fields.js index 2b6a0d44854..4a68c140056 100644 --- a/addons/web/static/src/js/fields/relational_fields.js +++ b/addons/web/static/src/js/fields/relational_fields.js @@ -2007,6 +2007,7 @@ var FieldMany2ManyBinaryMultiFiles = AbstractField.extend({ this.uploadedFiles = {}; this.uploadingFiles = []; this.fileupload_id = _.uniqueId('oe_fileupload_temp'); + this.accepted_file_extensions = (this.nodeOptions && this.nodeOptions.accepted_file_extensions) || this.accepted_file_extensions || '*'; $(window).on(this.fileupload_id, this._onFileLoaded.bind(this)); this.metadata = {}; diff --git a/addons/web/static/src/js/views/kanban/kanban_record.js b/addons/web/static/src/js/views/kanban/kanban_record.js index 0c4213c60b7..ab19e73d2eb 100644 --- a/addons/web/static/src/js/views/kanban/kanban_record.js +++ b/addons/web/static/src/js/views/kanban/kanban_record.js @@ -395,7 +395,7 @@ var KanbanRecord = Widget.extend({ fields: ['id', 'name'], }).then(function (attachmentIds) { self.imageUploadID = _.uniqueId('o_cover_image_upload'); - self.image_only = true; // prevent uploading of other file types + self.accepted_file_extensions = 'image/*'; // prevent uploading of other file types var coverId = self.record[fieldName] && self.record[fieldName].raw_value; var $content = $(QWeb.render('KanbanView.SetCoverModal', { coverId: coverId, diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index b76e86c036d..7800bc1a218 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -872,7 +872,6 @@ <span class="o_form_binary_progress">Uploading...</span> <t t-call="HiddenInputFile"> - <t t-set="image_only" t-value="true"/> <t t-set="fileupload_id" t-value="widget.fileupload_id"/> </t> </div> @@ -924,8 +923,8 @@ method="post" enctype="multipart/form-data" t-att-action="fileupload_action || '/web/binary/upload'"> <input type="hidden" name="csrf_token" t-att-value="csrf_token"/> <input type="hidden" name="callback" t-att-value="fileupload_id"/> - <input t-if="widget.image_only" type="file" class="o_input_file" name="ufile" accept="image/*"/> - <input t-if="!widget.image_only" type="file" class="o_input_file" name="ufile" t-att="{'multiple': multi_upload ? 'multiple' : null}"/> + <input type="file" class="o_input_file" name="ufile" + t-att="{'multiple': multi_upload ? 'multiple' : null, 'accept': widget.accepted_file_extensions || '*'}"/> <t t-raw="0"/> </form> <iframe t-att-id="fileupload_id" t-att-name="fileupload_id" style="display: none"/> @@ -985,21 +984,13 @@ <button class="btn btn-secondary fa fa-trash-o o_clear_file_button" title="Clear" aria-label="Clear" type="button"></button> </div> <iframe class="o_pdfview_iframe o_field_pdfviewer"/> - <t t-call="HiddenInputPDFFile"> + <t t-call="HiddenInputFile"> <t t-set="fileupload_id" t-value="widget.fileupload_id"/> <t t-set="fileupload_style" t-translation="off">overflow-x: hidden</t> </t> </div> </t> -<t t-name="HiddenInputPDFFile" t-extend="HiddenInputFile"> - <!-- remove the inputs and put a new one for pdf only --> - <t t-jquery="input[type='file']" t-operation="replace"/> - <t t-jquery="form" t-operation="append"> - <input type="file" class="o_input_file" name="ufile" accept="application/pdf"/> - </t> -</t> - <div t-name="FieldBinaryFileUploader" t-attf-class="oe_fileupload #{widget.attrs.class ? widget.attrs.class :''}" aria-atomic="true"> <div class="oe_placeholder_files"/> <div t-if="widget.mode !== 'readonly'" class="oe_add"> diff --git a/addons/web/static/tests/fields/basic_fields_tests.js b/addons/web/static/tests/fields/basic_fields_tests.js index e864f6341f9..92f2e163b99 100644 --- a/addons/web/static/tests/fields/basic_fields_tests.js +++ b/addons/web/static/tests/fields/basic_fields_tests.js @@ -2095,6 +2095,22 @@ QUnit.module('basic_fields', { session.get_file = oldGetFile; }); + QUnit.test('binary fields: option accepted_file_extensions', async function (assert) { + assert.expect(1); + + var form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: `<form string="Partners"> + <field name="document" widget="binary" options="{'accepted_file_extensions': '.dat,.bin'}"/> + </form>` + }); + assert.strictEqual(form.$('input.o_input_file').attr('accept'), '.dat,.bin', + "the input should have the correct ``accept`` attribute"); + form.destroy(); + }); + QUnit.test('binary fields that are readonly in create mode do not download', async function (assert) { assert.expect(2); @@ -2414,6 +2430,34 @@ QUnit.module('basic_fields', { form.destroy(); }); + QUnit.test('image: option accepted_file_extensions', async function (assert) { + assert.expect(2); + + var form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: `<form string="Partners"> + <field name="document" widget="image" options="{'accepted_file_extensions': '.png,.jpeg'}"/> + </form>` + }); + assert.strictEqual(form.$('input.o_input_file').attr('accept'), '.png,.jpeg', + "the input should have the correct ``accept`` attribute"); + form.destroy(); + + var form = await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: `<form string="Partners"> + <field name="document" widget="image"/> + </form>` + }); + assert.strictEqual(form.$('input.o_input_file').attr('accept'), 'image/*', + 'the default value for the attribute "accept" on the "image" widget must be "image/*"'); + form.destroy(); + }); + QUnit.test('image fields in subviews are loaded correctly', async function (assert) { assert.expect(6); diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index d9e9b69e6c2..f8c466099e0 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -2317,7 +2317,7 @@ QUnit.module('relational_fields', { QUnit.module('FieldMany2ManyBinaryMultiFiles'); QUnit.test('widget many2many_binary', async function (assert) { - assert.expect(15); + assert.expect(16); this.data['ir.attachment'] = { fields: { name: {string:"Name", type: "char"}, @@ -2341,7 +2341,7 @@ QUnit.module('relational_fields', { model: 'turtle', data: this.data, arch:'<form string="Turtles">' + - '<group><field name="picture_ids" widget="many2many_binary"/></group>' + + '<group><field name="picture_ids" widget="many2many_binary" options="{\'accepted_file_extensions\': \'image/*\'}"/></group>' + '</form>', archs: { 'ir.attachment,false,list': '<tree string="Pictures"><field name="name"/></tree>', @@ -2374,6 +2374,9 @@ QUnit.module('relational_fields', { assert.containsOnce(form, 'div.o_field_widget.oe_fileupload .o_hidden_input_file form', "there should be a hidden form to upload attachments"); + assert.strictEqual(form.$('input.o_input_file').attr('accept'), 'image/*', + "there should be an attribute \"accept\" on the input") + // TODO: add an attachment // no idea how to test this diff --git a/doc/reference/javascript_reference.rst b/doc/reference/javascript_reference.rst index 49ab37cf2e0..bc8b949c80f 100644 --- a/doc/reference/javascript_reference.rst +++ b/doc/reference/javascript_reference.rst @@ -1777,6 +1777,9 @@ order. option is useful to inform the web client that the default field name is not the name of the current field, but the name of another field. + - accepted_file_extensions: the file extension the user can pick from the file input dialog box (default value is 'image/*') + (cf: ``accept`` attribute on <input type="file"/>) + .. code-block:: xml <field name="image" widget='image' options='{"preview_image":"image_128"}'/> @@ -1786,6 +1789,11 @@ order. - Supported field types: *binary* + Options: + + - accepted_file_extensions: the file extension the user can pick from the file input dialog box + (cf: ``accept`` attribute on <input type="file"/>) + Attribute: - filename: saving a binary file will lose its file name, since it only @@ -2117,6 +2125,10 @@ Relational fields - Supported field types: *many2many* + Options: + - accepted_file_extensions: the file extension the user can pick from the file input dialog box + (cf: ``accept`` attribute on <input type="file"/>) + - many2many_tags (FieldMany2ManyTags) Display many2many as a list of tags. |
