diff --git a/survey_question_type_binary/README.rst b/survey_question_type_binary/README.rst new file mode 100644 index 00000000..a3f6ebc1 --- /dev/null +++ b/survey_question_type_binary/README.rst @@ -0,0 +1,87 @@ +=========================== +Survey binary question type +=========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:8ae0b5054b6a94d46e0bd97aa8c46bb0baa2d35408dae4626171e93df92e57bd + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsurvey-lightgray.png?logo=github + :target: https://github.com/OCA/survey/tree/17.0/survey_question_type_binary + :alt: OCA/survey +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/survey-17-0/survey-17-0-survey_question_type_binary + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/survey&target_branch=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module add binary field question type for attach on survey page. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Once installed from the configuration of the questions, in the options, +you can indicate the allowed mime types (Allowed Filemimetypes) and the +maximum file size to upload (Max Filesize). + +In addition to the binary option, a Multi: Binary option is supported +which has the same functionality as the single option but allows the +user to add more than one file. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Aures TIC + +Contributors +------------ + +- Jose Zambudio + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/survey `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/survey_question_type_binary/__init__.py b/survey_question_type_binary/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/survey_question_type_binary/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/survey_question_type_binary/__manifest__.py b/survey_question_type_binary/__manifest__.py new file mode 100644 index 00000000..478cc17c --- /dev/null +++ b/survey_question_type_binary/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2023 Jose Zambudio - Aures Tic +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Survey binary question type", + "summary": """ + This module add binary field as question type for survey page""", + "version": "17.0.1.0.0", + "license": "AGPL-3", + "author": "Aures TIC, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/survey", + "depends": ["survey"], + "data": [ + "security/ir.model.access.csv", + "views/survey_question.xml", + "views/survey_user_input_line.xml", + "templates/survey_template.xml", + ], + "assets": { + "survey.survey_assets": [ + "survey_question_type_binary/static/src/js/survey_form.js", + ], + }, +} diff --git a/survey_question_type_binary/i18n/it.po b/survey_question_type_binary/i18n/it.po new file mode 100644 index 00000000..7391f54c --- /dev/null +++ b/survey_question_type_binary/i18n/it.po @@ -0,0 +1,226 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * survey_question_type_binary +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-05-08 09:38+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: survey_question_type_binary +#. odoo-python +#: code:addons/survey_question_type_binary/models/survey_user_input_line.py:0 +#, python-format +msgid "%s File(s)" +msgstr "%s file" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_question_form_inherit_question_type_binary +msgid " Upload binary" +msgstr " Carica binario" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "Filename: " +msgstr "Nome file: " + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "MIME Type: " +msgstr "Tipo MIME: " + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "Size: " +msgstr "Dimensione: " + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_question__allowed_filemimetypes +msgid "Allowed Filemimetypes" +msgstr "Consenti tipi file MIME" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line__answer_binary_ids +msgid "Answer Binary" +msgstr "Risposta binaria" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line__answer_type +msgid "Answer Type" +msgstr "Tipo risposta" + +#. module: survey_question_type_binary +#: model:ir.model.fields.selection,name:survey_question_type_binary.selection__survey_question__question_type__binary +#: model:ir.model.fields.selection,name:survey_question_type_binary.selection__survey_user_input_line__answer_type__binary +msgid "Binary" +msgstr "Binaria" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "Contact image" +msgstr "Immagine contatto" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__create_uid +msgid "Created by" +msgstr "Creato da" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__create_date +msgid "Created on" +msgstr "Creato il" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__display_name +msgid "Display Name" +msgstr "Nome visualizzato" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "Document" +msgstr "Documento" + +#. module: survey_question_type_binary +#: model:ir.model.fields,help:survey_question_type_binary.field_survey_question__allowed_filemimetypes +msgid "" +"File mime types separated by commas (E.g: image/png,image/jpeg). Leave empty " +"to allow any value." +msgstr "" +"Tipi MIME file separati da virgole (Es. image/png,image/jpeg). Lasciare " +"vuoto per consentire qualsiasi valore." + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__filename +msgid "Filename" +msgstr "Nome file" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__id +msgid "ID" +msgstr "ID" + +#. module: survey_question_type_binary +#: model:ir.model.fields,help:survey_question_type_binary.field_survey_question__max_filesize +msgid "" +"Indicate maximum file size in bytes (Default 50MB). Leave empty to allow any " +"value." +msgstr "" +"Indica la dimensione file massima in byte (predefinito 50MB). Lasciare vuoto " +"per consentire qualsiasi valore." + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__input_line_id +msgid "Input Line" +msgstr "Riga inserita" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__is_binary_image +msgid "Is Binary Image" +msgstr "È immagine binaria" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary____last_update +msgid "Last Modified on" +msgstr "Ultima modifica il" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__write_uid +msgid "Last Updated by" +msgstr "Ultimo aggiornamento di" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__write_date +msgid "Last Updated on" +msgstr "Ultimo aggiornamento il" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_question__max_filesize +msgid "Max Filesize" +msgstr "Massimo dimensione file" + +#. module: survey_question_type_binary +#: model:ir.model.fields.selection,name:survey_question_type_binary.selection__survey_user_input_line__answer_type__multi_binary +msgid "Multi: Binary" +msgstr "Multi: binario" + +#. module: survey_question_type_binary +#: model:ir.model.fields.selection,name:survey_question_type_binary.selection__survey_question__question_type__multi_binary +msgid "Multiple: Binary" +msgstr "Multiplo: binario" + +#. module: survey_question_type_binary +#. odoo-python +#: code:addons/survey_question_type_binary/models/survey_question.py:0 +#: code:addons/survey_question_type_binary/models/survey_user_input_line.py:0 +#, python-format +msgid "Only files with {} mime types are allowed." +msgstr "Sono consentiti solo file di tipo MIME {}." + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_question__question_type +msgid "Question Type" +msgstr "Tipo di domanda" + +#. module: survey_question_type_binary +#: model:ir.model,name:survey_question_type_binary.model_survey_question +msgid "Survey Question" +msgstr "Domanda sondaggio" + +#. module: survey_question_type_binary +#: model:ir.model,name:survey_question_type_binary.model_survey_user_input +msgid "Survey User Input" +msgstr "Risposta utente al sondaggio" + +#. module: survey_question_type_binary +#: model:ir.model,name:survey_question_type_binary.model_survey_user_input_line +msgid "Survey User Input Line" +msgstr "Riga risposta utente al sondaggio" + +#. module: survey_question_type_binary +#: model:ir.model,name:survey_question_type_binary.model_survey_user_input_line_binary +msgid "Survey User Input Line Binary" +msgstr "Riga risposta binaria utente al sondaggio" + +#. module: survey_question_type_binary +#. odoo-python +#: code:addons/survey_question_type_binary/models/survey_user_input_line.py:0 +#, python-format +msgid "The answer must be in the right type" +msgstr "La risposta deve essere del tipo corretto" + +#. module: survey_question_type_binary +#. odoo-python +#: code:addons/survey_question_type_binary/models/survey_question.py:0 +#: code:addons/survey_question_type_binary/models/survey_user_input_line.py:0 +#, python-format +msgid "The file cannot exceed {}MB in size." +msgstr "Il file non può superare i {}MB." + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__value_binary +msgid "Value Binary" +msgstr "Valore binario" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__value_binary_size +msgid "Value Binary Size" +msgstr "Dimensione valore binario" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__value_binary_type +msgid "Value Binary Type" +msgstr "Tipo valore binario" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "bytes" +msgstr "byte" diff --git a/survey_question_type_binary/i18n/survey_question_type_binary.pot b/survey_question_type_binary/i18n/survey_question_type_binary.pot new file mode 100644 index 00000000..12d97555 --- /dev/null +++ b/survey_question_type_binary/i18n/survey_question_type_binary.pot @@ -0,0 +1,219 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * survey_question_type_binary +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: survey_question_type_binary +#. odoo-python +#: code:addons/survey_question_type_binary/models/survey_user_input_line.py:0 +#, python-format +msgid "%s File(s)" +msgstr "" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_question_form_inherit_question_type_binary +msgid " Upload binary" +msgstr "" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "Filename: " +msgstr "" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "MIME Type: " +msgstr "" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "Size: " +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_question__allowed_filemimetypes +msgid "Allowed Filemimetypes" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line__answer_binary_ids +msgid "Answer Binary" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line__answer_type +msgid "Answer Type" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields.selection,name:survey_question_type_binary.selection__survey_question__question_type__binary +#: model:ir.model.fields.selection,name:survey_question_type_binary.selection__survey_user_input_line__answer_type__binary +msgid "Binary" +msgstr "" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "Contact image" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__create_uid +msgid "Created by" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__create_date +msgid "Created on" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__display_name +msgid "Display Name" +msgstr "" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "Document" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,help:survey_question_type_binary.field_survey_question__allowed_filemimetypes +msgid "" +"File mime types separated by commas (E.g: image/png,image/jpeg). Leave empty" +" to allow any value." +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__filename +msgid "Filename" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__id +msgid "ID" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,help:survey_question_type_binary.field_survey_question__max_filesize +msgid "" +"Indicate maximum file size in bytes (Default 50MB). Leave empty to allow any" +" value." +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__input_line_id +msgid "Input Line" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__is_binary_image +msgid "Is Binary Image" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary____last_update +msgid "Last Modified on" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__write_uid +msgid "Last Updated by" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__write_date +msgid "Last Updated on" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_question__max_filesize +msgid "Max Filesize" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields.selection,name:survey_question_type_binary.selection__survey_user_input_line__answer_type__multi_binary +msgid "Multi: Binary" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields.selection,name:survey_question_type_binary.selection__survey_question__question_type__multi_binary +msgid "Multiple: Binary" +msgstr "" + +#. module: survey_question_type_binary +#. odoo-python +#: code:addons/survey_question_type_binary/models/survey_question.py:0 +#: code:addons/survey_question_type_binary/models/survey_user_input_line.py:0 +#, python-format +msgid "Only files with {} mime types are allowed." +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_question__question_type +msgid "Question Type" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model,name:survey_question_type_binary.model_survey_question +msgid "Survey Question" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model,name:survey_question_type_binary.model_survey_user_input +msgid "Survey User Input" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model,name:survey_question_type_binary.model_survey_user_input_line +msgid "Survey User Input Line" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model,name:survey_question_type_binary.model_survey_user_input_line_binary +msgid "Survey User Input Line Binary" +msgstr "" + +#. module: survey_question_type_binary +#. odoo-python +#: code:addons/survey_question_type_binary/models/survey_user_input_line.py:0 +#, python-format +msgid "The answer must be in the right type" +msgstr "" + +#. module: survey_question_type_binary +#. odoo-python +#: code:addons/survey_question_type_binary/models/survey_question.py:0 +#: code:addons/survey_question_type_binary/models/survey_user_input_line.py:0 +#, python-format +msgid "The file cannot exceed {}MB in size." +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__value_binary +msgid "Value Binary" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__value_binary_size +msgid "Value Binary Size" +msgstr "" + +#. module: survey_question_type_binary +#: model:ir.model.fields,field_description:survey_question_type_binary.field_survey_user_input_line_binary__value_binary_type +msgid "Value Binary Type" +msgstr "" + +#. module: survey_question_type_binary +#: model_terms:ir.ui.view,arch_db:survey_question_type_binary.survey_user_input_line_view_form_inherit_question_type_binary +msgid "bytes" +msgstr "" diff --git a/survey_question_type_binary/models/__init__.py b/survey_question_type_binary/models/__init__.py new file mode 100644 index 00000000..84c8fd30 --- /dev/null +++ b/survey_question_type_binary/models/__init__.py @@ -0,0 +1,4 @@ +from . import survey_question +from . import survey_user_input +from . import survey_user_input_line +from . import survey_user_input_line_binary diff --git a/survey_question_type_binary/models/survey_question.py b/survey_question_type_binary/models/survey_question.py new file mode 100644 index 00000000..212ce0d1 --- /dev/null +++ b/survey_question_type_binary/models/survey_question.py @@ -0,0 +1,85 @@ +# Copyright 2023 Jose Zambudio - Aures Tic +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import base64 +import contextlib + +from odoo import _, fields, models +from odoo.tools.mimetypes import guess_mimetype + + +class SurveyQuestion(models.Model): + _inherit = "survey.question" + + question_type = fields.Selection( + selection_add=[ + ("binary", "Binary"), + ("multi_binary", "Multiple: Binary"), + ] + ) + allowed_filemimetypes = fields.Char( + help="File mime types separated by commas (E.g: image/png,image/jpeg). " + "Leave empty to allow any value.", + ) + max_filesize = fields.Integer( + default="52428800", + help="Indicate maximum file size in bytes (Default 50MB). Leave empty " + "to allow any value.", + ) + + def validate_question(self, answer, comment=None): + if self.question_type in ("binary", "multi_binary"): + return self.validate_binary(answer) + return super().validate_question(answer, comment=comment) + + def validate_binary(self, answers): + self.ensure_one() + errors = {} + # Empty answer to mandatory question + if self.constr_mandatory and not answers: + errors.update({self.id: self.constr_error_msg}) + if answers: + if not isinstance(answers, (list | tuple)): + answers = [answers] + for answer in answers: + try: + data = answer.get("data") + filesize = ( + data and (len(data) * 3) / 4 - str(data).count("=", -2) or 0 + ) + filemimetype = ( + data and guess_mimetype(base64.b64decode(data)) or False + ) + answer.update( + { + "size": filesize, + "type": filemimetype, + } + ) + except ValueError: + errors.update({self.id: "This is not a file"}) + return errors + with contextlib.suppress(Exception): + # 0 answer to mandatory question + if self.constr_mandatory and not data: + errors.update({self.id: self.constr_error_msg}) + else: + if ( + self.allowed_filemimetypes + and filemimetype not in self.allowed_filemimetypes + ): + errors.update( + { + self.id: _( + "Only files with {} mime types are allowed." + ).format(self.allowed_filemimetypes) + } + ) + if self.max_filesize and filesize > self.max_filesize: + errors.update( + { + self.id: _( + "The file cannot exceed {}MB in size." + ).format(self.max_filesize / 1024 / 1024) + } + ) + return errors diff --git a/survey_question_type_binary/models/survey_user_input.py b/survey_question_type_binary/models/survey_user_input.py new file mode 100644 index 00000000..6b990c90 --- /dev/null +++ b/survey_question_type_binary/models/survey_user_input.py @@ -0,0 +1,56 @@ +# Copyright 2023 Jose Zambudio - Aures Tic +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import models + + +class SurveyUserInput(models.Model): + _inherit = "survey.user_input" + + def _save_lines(self, question, answer, comment=None, overwrite_existing=True): + old_answers = self.env["survey.user_input.line"].search( + [ + ("user_input_id", "=", self.id), + ("question_id", "=", question.id), + ] + ) + + if question.question_type in ("binary", "multi_binary"): + if not isinstance(answer, (list | tuple)): + answer = [answer] + if not answer: + answer = [False] + for answer_binary in answer: + old_answers = self._save_line_simple_answer( + question, old_answers, answer_binary + ) + else: + super()._save_lines( + question, answer, comment=comment, overwrite_existing=overwrite_existing + ) + return True + + def _get_line_answer_values(self, question, answer, answer_type): + vals = super()._get_line_answer_values(question, answer, answer_type) + if answer_type in ("binary", "multi_binary") and answer: + if answer_type == "binary": + del vals["value_binary"] + else: + del vals["value_multi_binary"] + if not isinstance(answer, (list | tuple)): + answer = [answer] + answer_binary_datas = [] + for answer_binary in answer: + data = answer_binary.get("data") + filename = answer_binary.get("filename") + answer_binary_datas += [ + ( + 0, + 0, + { + "value_binary": data, + "filename": filename, + }, + ) + ] + vals.update({"answer_binary_ids": answer_binary_datas}) + return vals diff --git a/survey_question_type_binary/models/survey_user_input_line.py b/survey_question_type_binary/models/survey_user_input_line.py new file mode 100644 index 00000000..883368ba --- /dev/null +++ b/survey_question_type_binary/models/survey_user_input_line.py @@ -0,0 +1,75 @@ +# Copyright 2023 Jose Zambudio - Aures Tic +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + + +class SurveyUserInputLine(models.Model): + _inherit = "survey.user_input.line" + + answer_type = fields.Selection( + selection_add=[ + ("binary", "Binary"), + ("multi_binary", "Multi: Binary"), + ] + ) + answer_binary_ids = fields.One2many( + comodel_name="survey.user_input.line_binary", + inverse_name="input_line_id", + readonly=True, + ) + + @api.constrains("skipped", "answer_type") + def _check_answer_type_skipped(self): + super_check = self + for line in self: + if line.answer_type not in ("binary", "multi_binary"): + continue + super_check -= line + field_name = "answer_binary_ids" + if field_name and not line[field_name]: + raise ValidationError(_("The answer must be in the right type")) + return super(SurveyUserInputLine, super_check)._check_answer_type_skipped() + + @api.constrains( + "question_id", + "answer_type", + "answer_binary_ids", + ) + def _check_binary_answer(self): + for rec in self: + if ( + rec.question_id.question_type not in ("binary", "multi_binary") + or not rec.answer_type + or rec.answer_type not in ("binary", "multi_binary") + ): + continue + for answer_binary in rec.answer_binary_ids: + if ( + rec.question_id.max_filesize + and rec.question_id.max_filesize < answer_binary.value_binary_size + ): + raise ValidationError( + _("The file cannot exceed {}MB in size.").format( + rec.question_id.max_filesize / 1024 / 1024 + ) + ) + if ( + rec.question_id.allowed_filemimetypes + and answer_binary.value_binary_type + not in rec.question_id.allowed_filemimetypes + ): + raise ValidationError( + _("Only files with {} mime types are allowed.").format( + rec.question_id.allowed_filemimetypes + ) + ) + + def _compute_display_name(self): + super()._compute_display_name() + for line in self: + if line.answer_type == "binary" and line.answer_binary_ids: + line.display_name = line.answer_binary_ids.filename + if line.answer_type == "multi_binary" and line.answer_binary_ids: + line.display_name = _("%s File(s)") % len(line.answer_binary_ids) + return True diff --git a/survey_question_type_binary/models/survey_user_input_line_binary.py b/survey_question_type_binary/models/survey_user_input_line_binary.py new file mode 100644 index 00000000..5b89f7a5 --- /dev/null +++ b/survey_question_type_binary/models/survey_user_input_line_binary.py @@ -0,0 +1,61 @@ +# Copyright 2023 Jose Zambudio - Aures Tic +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import base64 + +from odoo import api, fields, models +from odoo.tools.mimetypes import guess_mimetype + +VALID_MIMETYPES = [ + "image/jpeg", + "image/png", + "image/gif", + "image/bmp", + "image/svg+xml", + "image/x-icon", +] + + +class SurveyUserInputLineBinary(models.Model): + _name = "survey.user_input.line_binary" + _description = "Survey User Input Line Binary" + + input_line_id = fields.Many2one( + comodel_name="survey.user_input.line", + required=True, + readonly=True, + ondelete="cascade", + ) + value_binary = fields.Binary( + required=True, + readonly=True, + ) + filename = fields.Char( + required=True, + readonly=True, + ) + value_binary_type = fields.Char( + compute="_compute_binary_data", + store=True, + readonly=True, + ) + value_binary_size = fields.Integer( + compute="_compute_binary_data", + store=True, + readonly=True, + ) + is_binary_image = fields.Boolean( + compute="_compute_binary_data", + store=True, + readonly=True, + ) + + @api.depends("value_binary") + def _compute_binary_data(self): + for input_line in self: + input_line.value_binary_type = guess_mimetype( + base64.b64decode(input_line.value_binary) + ) + input_line.value_binary_size = (len(input_line.value_binary) * 3) / 4 - str( + input_line.value_binary + ).count("=", -2) + input_line.is_binary_image = input_line.value_binary_type in VALID_MIMETYPES diff --git a/survey_question_type_binary/pyproject.toml b/survey_question_type_binary/pyproject.toml new file mode 100644 index 00000000..4231d0cc --- /dev/null +++ b/survey_question_type_binary/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/survey_question_type_binary/readme/CONFIGURE.md b/survey_question_type_binary/readme/CONFIGURE.md new file mode 100644 index 00000000..28b5d98b --- /dev/null +++ b/survey_question_type_binary/readme/CONFIGURE.md @@ -0,0 +1,7 @@ +Once installed from the configuration of the questions, in the options, +you can indicate the allowed mime types (Allowed Filemimetypes) and the +maximum file size to upload (Max Filesize). + +In addition to the binary option, a Multi: Binary option is supported +which has the same functionality as the single option but allows the +user to add more than one file. diff --git a/survey_question_type_binary/readme/CONTRIBUTORS.md b/survey_question_type_binary/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..b9d53235 --- /dev/null +++ b/survey_question_type_binary/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Jose Zambudio \<\> diff --git a/survey_question_type_binary/readme/DESCRIPTION.md b/survey_question_type_binary/readme/DESCRIPTION.md new file mode 100644 index 00000000..6fb13022 --- /dev/null +++ b/survey_question_type_binary/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module add binary field question type for attach on survey page. diff --git a/survey_question_type_binary/security/ir.model.access.csv b/survey_question_type_binary/security/ir.model.access.csv new file mode 100644 index 00000000..92278b75 --- /dev/null +++ b/survey_question_type_binary/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_survey_user_input_line_binary,access_survey_user_input_line_binary,model_survey_user_input_line_binary,base.group_user,1,1,1,0 diff --git a/survey_question_type_binary/static/description/icon.png b/survey_question_type_binary/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/survey_question_type_binary/static/description/icon.png differ diff --git a/survey_question_type_binary/static/description/index.html b/survey_question_type_binary/static/description/index.html new file mode 100644 index 00000000..d24a4fb3 --- /dev/null +++ b/survey_question_type_binary/static/description/index.html @@ -0,0 +1,430 @@ + + + + + +Survey binary question type + + + +
+

Survey binary question type

+ + +

Beta License: AGPL-3 OCA/survey Translate me on Weblate Try me on Runboat

+

This module add binary field question type for attach on survey page.

+

Table of contents

+ +
+

Configuration

+

Once installed from the configuration of the questions, in the options, +you can indicate the allowed mime types (Allowed Filemimetypes) and the +maximum file size to upload (Max Filesize).

+

In addition to the binary option, a Multi: Binary option is supported +which has the same functionality as the single option but allows the +user to add more than one file.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Aures TIC
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/survey project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/survey_question_type_binary/static/src/js/survey_form.js b/survey_question_type_binary/static/src/js/survey_form.js new file mode 100644 index 00000000..dd3f2091 --- /dev/null +++ b/survey_question_type_binary/static/src/js/survey_form.js @@ -0,0 +1,80 @@ +odoo.define("survey_question_type_binary", [], function () { + "use strict"; + + const survey_form = odoo.loader.modules.get("@survey/js/survey_form")[ + Symbol.for("default") + ]; + + survey_form.include({ + _getSubmitBinariesValues: function (params) { + const self = this; + let promises = []; + + this.$("[data-question-type]").each(function () { + switch ($(this).data("questionType")) { + case "binary": + case "multi_binary": + promises = promises.concat( + self._getSubmitAnswersBinary(params, $(this)) + ); + break; + } + }); + return promises; + }, + _getSubmitAnswersBinary: function (params, $input) { + const question_id = $input.attr("name"); + return Array.prototype.map.call($input[0].files, (file) => { + return this._readFileAsDataURL(file).then(function (sDataURL) { + if (!params[question_id]) { + params[question_id] = []; + } + params[question_id].push({ + data: sDataURL.split(",")[1], + filename: file.name, + size: file.size, + type: file.type, + }); + }); + }); + }, + _readFileAsDataURL: function (file) { + return $.Deferred(function (deferred) { + $.extend(new FileReader(), { + onload: function (e) { + var sDataURL = e.target.result; + deferred.resolve(sDataURL); + }, + onerror: function () { + deferred.reject(this); + }, + }).readAsDataURL(file); + }).promise(); + }, + _submitForm: function (options) { + const self = this; + const params = {}; + const binaryPrimises = self._getSubmitBinariesValues(params); + + if (binaryPrimises.length > 0 && !self.options.isStartScreen) { + const $form = self.$("form"); + const formData = new FormData($form[0]); + + if (options.previousPageId) { + params.previous_page_id = options.previousPageId; + } + self._prepareSubmitValues(formData, params); + var route = "/survey/submit"; + Promise.all(binaryPrimises).then(function () { + const submitPromise = self.rpc( + `${route}/${self.options.surveyToken}/${self.options.answerToken}`, + params + ); + self._nextScreen(submitPromise, options); + }); + } else { + return self._super(options); + } + }, + }); +}); diff --git a/survey_question_type_binary/templates/survey_template.xml b/survey_question_type_binary/templates/survey_template.xml new file mode 100644 index 00000000..cfd75c99 --- /dev/null +++ b/survey_question_type_binary/templates/survey_template.xml @@ -0,0 +1,57 @@ + + + + + + + + + diff --git a/survey_question_type_binary/tests/__init__.py b/survey_question_type_binary/tests/__init__.py new file mode 100644 index 00000000..df7702e6 --- /dev/null +++ b/survey_question_type_binary/tests/__init__.py @@ -0,0 +1 @@ +from . import test_survey diff --git a/survey_question_type_binary/tests/test_survey.py b/survey_question_type_binary/tests/test_survey.py new file mode 100644 index 00000000..9013a052 --- /dev/null +++ b/survey_question_type_binary/tests/test_survey.py @@ -0,0 +1,179 @@ +# Copyright 2023 Jose Zambudio - Aures Tic +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +# from odoo.exceptions import ValidationError +import base64 + +from odoo.modules.module import get_module_resource + +from odoo.addons.survey.tests import common + + +class TestSurvey(common.SurveyCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + User = cls.env["res.users"].with_context(no_reset_password=True) + (group_survey_user, group_employee) = ( + cls.env.ref("survey.group_survey_user").id, + cls.env.ref("base.group_user").id, + ) + cls.survey_manager = User.create( + { + "name": "Maria Riera", + "login": "Riera", + "email": "maria.riera@example.com", + "groups_id": [ + ( + 6, + 0, + [ + cls.env.ref("survey.group_survey_manager").id, + group_survey_user, + group_employee, + ], + ) + ], + } + ) + cls.survey1 = ( + cls.env["survey.survey"] + .with_user(cls.survey_manager) + .create({"title": "S0", "page_ids": [(0, 0, {"title": "P0"})]}) + ) + cls.page1 = ( + cls.env["survey.question"] + .with_user(cls.survey_manager) + .create( + { + "title": "First page", + "survey_id": cls.survey1.id, + "sequence": 1, + "is_page": True, + } + ) + ) + cls.user_input1 = ( + cls.env["survey.user_input"] + .with_user(cls.survey_manager) + .create( + { + "survey_id": cls.survey1.id, + "partner_id": cls.survey_manager.partner_id.id, + } + ) + ) + cls.question_binary = ( + cls.env["survey.question"] + .with_user(cls.survey_manager) + .create( + { + "title": "Test Binary", + "page_id": cls.page1.id, + "question_type": "binary", + "allowed_filemimetypes": "application/pdf", + "max_filesize": 1024, + "constr_mandatory": True, + "validation_required": True, + } + ) + ) + cls.question_multi_binary = ( + cls.env["survey.question"] + .with_user(cls.survey_manager) + .create( + { + "title": "Test Binary", + "page_id": cls.page1.id, + "question_type": "multi_binary", + "allowed_filemimetypes": "image/png", + "max_filesize": 2097152, + "validation_required": True, + } + ) + ) + module_icon = get_module_resource( + "survey_question_type_binary", "static", "description", "icon.png" + ) + with open(module_icon, "rb") as img: + cls.image_base64 = base64.b64encode(img.read()) + module_html = get_module_resource( + "survey_question_type_binary", "static", "description", "index.html" + ) + with open(module_html, "rb") as file: + cls.html = base64.b64encode(file.read()) + + def test_01_question_binary_with_error_values(self): + self.assertEqual( + self.question_binary.validate_question({}), + {self.question_binary.id: self.question_binary.constr_error_msg}, + ) + self.assertEqual( + self.question_binary.validate_question({"data": b""}), + {self.question_binary.id: self.question_binary.constr_error_msg}, + ) + self.assertEqual( + self.question_binary.validate_question({"data": "This is not a file"}), + {self.question_binary.id: "This is not a file"}, + ) + self.assertEqual( + self.question_binary.validate_question({"data": self.image_base64}), + {self.question_binary.id: f"The file cannot exceed {1 / 1024}MB in size."}, + ) + self.question_binary.max_filesize = 2097152 # Increse to 2.0MB + message = "Only files with {} mime types are allowed." + self.assertEqual( + self.question_binary.validate_question({"data": self.image_base64}), + {self.question_binary.id: message.format("application/pdf")}, + ) + + def test_02_question_binary_with_valid_values(self): + self.question_binary.max_filesize = 2097152 # Increse to 2.0MB + self.question_binary.allowed_filemimetypes = "image/png" + self.assertEqual( + self.question_binary.validate_question({"data": self.image_base64}), + {}, + ) + self.user_input1._save_lines( + question=self.question_binary, + answer={ + "data": self.image_base64, + "filename": "test image.png", + }, + ) + self.assertTrue( + self.user_input1.user_input_line_ids.filtered( + lambda r: r.question_id == self.question_binary + ).answer_binary_ids, + ) + + def test_03_question_multi_binary_with_valid_values(self): + self.assertEqual( + self.question_multi_binary.validate_question( + [ + {"data": self.image_base64}, + {"data": self.image_base64}, + {"data": self.image_base64}, + {"data": self.image_base64}, + ] + ), + {}, + ) + + def test_04_question_binary_data(self): + self.user_input1._save_lines( + question=self.question_multi_binary, + answer=[ + { + "data": self.image_base64, + "filename": "test image.png", + } + ], + ) + answer = self.user_input1.user_input_line_ids.filtered( + lambda r: r.question_id == self.question_multi_binary + ).answer_binary_ids + self.assertTrue( + answer.is_binary_image, + ) + self.assertEqual(answer.value_binary_type, "image/png") + self.assertEqual(answer.value_binary_size, 9455) diff --git a/survey_question_type_binary/views/survey_question.xml b/survey_question_type_binary/views/survey_question.xml new file mode 100644 index 00000000..6a8d438c --- /dev/null +++ b/survey_question_type_binary/views/survey_question.xml @@ -0,0 +1,36 @@ + + + + + survey.question.view.form.inherit + survey.question + + + +
+ Upload binary +
+
+ + question_type not in ['char_box', 'numerical_box', 'date', 'datetime', "binary", "multi_binary"] + + + + + + +
+
+
diff --git a/survey_question_type_binary/views/survey_user_input_line.xml b/survey_question_type_binary/views/survey_user_input_line.xml new file mode 100644 index 00000000..a8ec441f --- /dev/null +++ b/survey_question_type_binary/views/survey_user_input_line.xml @@ -0,0 +1,106 @@ + + + + + survey.user_input.line.view.form.inherit + survey.user_input.line + + + + + + + + + + +
+
+ Contact image + + + + + + + + + +
+
+
Filename:
+
MIME Type:
+
Size: bytes
+
+
+
+
+
+
+
+
+
+ +