Skip to content

Commit

Permalink
[ADD][14.0] survey_question_type_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
zamberjo committed Jul 14, 2023
1 parent 9113e2a commit 5b8e4ee
Show file tree
Hide file tree
Showing 17 changed files with 944 additions and 0 deletions.
76 changes: 76 additions & 0 deletions survey_question_type_binary/README.rst
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.
1 change: 1 addition & 0 deletions survey_question_type_binary/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions survey_question_type_binary/__manifest__.py
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",
],
}
3 changes: 3 additions & 0 deletions survey_question_type_binary/models/__init__.py
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
61 changes: 61 additions & 0 deletions survey_question_type_binary/models/survey_question.py
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()

Check warning on line 26 in survey_question_type_binary/models/survey_question.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_question.py#L24-L26

Added lines #L24 - L26 were not covered by tests
# Empty answer to mandatory question
if self.constr_mandatory and not answer:
errors.update({answer_tag: self.constr_error_msg})

Check warning on line 29 in survey_question_type_binary/models/survey_question.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_question.py#L29

Added line #L29 was not covered by tests
# 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

Check warning on line 38 in survey_question_type_binary/models/survey_question.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_question.py#L32-L38

Added lines #L32 - L38 were not covered by tests
# Answer is not in the right range
with tools.ignore(Exception):

Check warning on line 40 in survey_question_type_binary/models/survey_question.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_question.py#L40

Added line #L40 was not covered by tests
# 0 answer to mandatory question
if self.constr_mandatory and not data:
errors.update({answer_tag: self.constr_error_msg})

Check warning on line 43 in survey_question_type_binary/models/survey_question.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_question.py#L43

Added line #L43 was not covered by tests
else:
if filetype not in self.allowed_filetypes:
errors.update(

Check warning on line 46 in survey_question_type_binary/models/survey_question.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_question.py#L46

Added line #L46 was not covered by tests
{
answer_tag: _(
"Only files with {} extension are allowed."
).format(self.allowed_filetypes)
}
)
if filesize > self.max_filesize:
errors.update(

Check warning on line 54 in survey_question_type_binary/models/survey_question.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_question.py#L54

Added line #L54 was not covered by tests
{
answer_tag: _(
"The file cannot exceed {} MB in size."
).format(self.max_filesize / 1024)
}
)
return errors

Check warning on line 61 in survey_question_type_binary/models/survey_question.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_question.py#L61

Added line #L61 was not covered by tests
37 changes: 37 additions & 0 deletions survey_question_type_binary/models/survey_user_input.py
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(

Check warning on line 10 in survey_question_type_binary/models/survey_user_input.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_user_input.py#L10

Added line #L10 was not covered by tests
[
("user_input_id", "=", self.id),
("question_id", "=", question.id),
]
)

if question.question_type == "binary":
self._save_line_simple_answer(question, old_answers, answer)

Check warning on line 18 in survey_question_type_binary/models/survey_user_input.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_user_input.py#L18

Added line #L18 was not covered by tests
else:
super(SurveyUserInput, self).save_lines(question, answer, comment=comment)

Check warning on line 20 in survey_question_type_binary/models/survey_user_input.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_user_input.py#L20

Added line #L20 was not covered by tests

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(

Check warning on line 30 in survey_question_type_binary/models/survey_user_input.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_user_input.py#L27-L30

Added lines #L27 - L30 were not covered by tests
{
'value_binary': data,
'value_binary_type': filetype,
'value_binary_size': filesize,
}
)
return vals
64 changes: 64 additions & 0 deletions survey_question_type_binary/models/survey_user_input_line.py
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

Check warning on line 46 in survey_question_type_binary/models/survey_user_input_line.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_user_input_line.py#L46

Added line #L46 was not covered by tests
if (
rec.question_id.max_filesize
and rec.question_id.max_filesize < rec.value_binary_size
):
raise ValidationError(

Check warning on line 51 in survey_question_type_binary/models/survey_user_input_line.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_user_input_line.py#L51

Added line #L51 was not covered by tests
_("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(

Check warning on line 60 in survey_question_type_binary/models/survey_user_input_line.py

View check run for this annotation

Codecov / codecov/patch

survey_question_type_binary/models/survey_user_input_line.py#L60

Added line #L60 was not covered by tests
_("Only files with {} extension are allowed.").format(
rec.question_id.allowed_filetypes
)
)
1 change: 1 addition & 0 deletions survey_question_type_binary/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Jose Zambudio <[email protected]>
1 change: 1 addition & 0 deletions survey_question_type_binary/readme/DESCRIPTION.rst
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.
Loading

0 comments on commit 5b8e4ee

Please sign in to comment.