-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD][14.0] survey_question_type_binary
- Loading branch information
Showing
17 changed files
with
944 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
=========================== | ||
Survey binary question type | ||
=========================== | ||
|
||
.. | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! This file is generated by oca-gen-addon-readme !! | ||
!! changes will be overwritten. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! source digest: sha256:d28ebde5feac6fab0743a2a406ef45212e46d443aa73ea090def24b22d80f9f9 | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
.. |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/14.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-14-0/survey-14-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=14.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: | ||
|
||
Bug Tracker | ||
=========== | ||
|
||
Bugs are tracked on `GitHub Issues <https://github.com/OCA/survey/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 <https://github.com/OCA/survey/issues/new?body=module:%20survey_question_type_binary%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
||
Do not contact contributors directly about support or help with technical issues. | ||
|
||
Credits | ||
======= | ||
|
||
Authors | ||
~~~~~~~ | ||
|
||
* Aures TIC | ||
|
||
Contributors | ||
~~~~~~~~~~~~ | ||
|
||
* Jose Zambudio <[email protected]> | ||
|
||
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 <https://github.com/OCA/survey/tree/14.0/survey_question_type_binary>`_ project on GitHub. | ||
|
||
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2023 Jose Zambudio - Aures Tic <[email protected]> | ||
# 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": "14.0.1.0.0", | ||
"license": "AGPL-3", | ||
"author": "Aures TIC, Odoo Community Association (OCA)", | ||
"website": "https://github.com/OCA/survey", | ||
"depends": ["survey"], | ||
"data": [ | ||
# "views/survey_survey.xml", | ||
"views/survey_question.xml", | ||
"views/survey_user_input_line.xml", | ||
"templates/survey_template.xml", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from . import survey_question | ||
from . import survey_user_input | ||
from . import survey_user_input_line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Copyright 2023 Jose Zambudio - Aures Tic <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from odoo import _, fields, models, tools | ||
|
||
|
||
class SurveyQuestion(models.Model): | ||
_inherit = "survey.question" | ||
|
||
question_type = fields.Selection( | ||
selection_add=[ | ||
("binary", "Binary"), | ||
] | ||
) | ||
allowed_filetypes = fields.Char( | ||
default="jpg,jpeg,png,gif,pdf,doc,docx,xls,xlsx,ppt,pptx,odt,ods,odp", | ||
help="File extensions separated by commas", | ||
) | ||
max_filesize = fields.Integer( | ||
default="50000", | ||
help="Indicate maximum file size in bytes.", | ||
) | ||
|
||
def validate_binary(self, post, answer_tag): | ||
self.ensure_one() | ||
errors = {} | ||
answer = post[answer_tag].strip() | ||
# Empty answer to mandatory question | ||
if self.constr_mandatory and not answer: | ||
errors.update({answer_tag: self.constr_error_msg}) | ||
# Checks if user input is a number | ||
if answer: | ||
try: | ||
data = answer.get("data") | ||
filetype = answer.get("size") | ||
filesize = answer.get("type") | ||
except ValueError: | ||
errors.update({answer_tag: "This is not a file"}) | ||
return errors | ||
# Answer is not in the right range | ||
with tools.ignore(Exception): | ||
# 0 answer to mandatory question | ||
if self.constr_mandatory and not data: | ||
errors.update({answer_tag: self.constr_error_msg}) | ||
else: | ||
if filetype not in self.allowed_filetypes: | ||
errors.update( | ||
{ | ||
answer_tag: _( | ||
"Only files with {} extension are allowed." | ||
).format(self.allowed_filetypes) | ||
} | ||
) | ||
if filesize > self.max_filesize: | ||
errors.update( | ||
{ | ||
answer_tag: _( | ||
"The file cannot exceed {} MB in size." | ||
).format(self.max_filesize / 1024) | ||
} | ||
) | ||
return errors | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2023 Jose Zambudio - Aures Tic <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from odoo import fields, models | ||
|
||
|
||
class SurveyUserInput(models.Model): | ||
_inherit = "survey.user_input" | ||
|
||
def save_lines(self, question, answer, comment=None): | ||
old_answers = self.env["survey.user_input.line"].search( | ||
[ | ||
("user_input_id", "=", self.id), | ||
("question_id", "=", question.id), | ||
] | ||
) | ||
|
||
if question.question_type == "binary": | ||
self._save_line_simple_answer(question, old_answers, answer) | ||
else: | ||
super(SurveyUserInput, self).save_lines(question, answer, comment=comment) | ||
|
||
def _get_line_answer_values(self, question, answer, answer_type): | ||
vals = super(SurveyUserInput, self)._get_line_answer_values( | ||
question, answer, answer_type | ||
) | ||
if answer_type == "binary" and answer: | ||
data = answer.get("data") | ||
filetype = answer.get("type") | ||
filesize = answer.get("size") | ||
vals.update( | ||
{ | ||
'value_binary': data, | ||
'value_binary_type': filetype, | ||
'value_binary_size': filesize, | ||
} | ||
) | ||
return vals |
64 changes: 64 additions & 0 deletions
64
survey_question_type_binary/models/survey_user_input_line.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright 2023 Jose Zambudio - Aures Tic <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
from odoo import _, api, fields, models | ||
from odoo.exceptions import ValidationError | ||
|
||
VALID_EXTENSIONS = ["jpg", "jpeg", "png", "gif", "tiff", "bmp", "ico"] | ||
|
||
|
||
class SurveyUserInputLine(models.Model): | ||
_inherit = "survey.user_input.line" | ||
|
||
answer_type = fields.Selection( | ||
selection_add=[ | ||
("binary", "Binary"), | ||
] | ||
) | ||
value_binary = fields.Binary() | ||
value_binary_type = fields.Char() | ||
value_binary_size = fields.Integer() | ||
is_binary_image = fields.Boolean( | ||
compute="_compute_binary_image", | ||
store=True, | ||
) | ||
|
||
@api.depends("value_binary_type") | ||
def _compute_binary_image(self): | ||
for input_line in self: | ||
input_line.is_binary_image = ( | ||
input_line.value_binary_type in VALID_EXTENSIONS | ||
) | ||
|
||
@api.constrains( | ||
"question_id", | ||
"answer_type", | ||
"value_binary", | ||
"value_binary_type", | ||
"value_binary_size", | ||
) | ||
def _check_binary_answer(self): | ||
for rec in self: | ||
if ( | ||
rec.question_id.question_type != "binary" | ||
or not rec.answer_type | ||
or rec.answer_type != "binary" | ||
): | ||
continue | ||
if ( | ||
rec.question_id.max_filesize | ||
and rec.question_id.max_filesize < rec.value_binary_size | ||
): | ||
raise ValidationError( | ||
_("The file cannot exceed {} MB in size.").format( | ||
rec.question_id.max_filesize / 1024 | ||
) | ||
) | ||
if ( | ||
rec.question_id.allowed_filetypes | ||
and rec.value_binary_type not in rec.question_id.allowed_filetypes | ||
): | ||
raise ValidationError( | ||
_("Only files with {} extension are allowed.").format( | ||
rec.question_id.allowed_filetypes | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Jose Zambudio <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This module add binary field question type for attach on survey page |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.