survey_upload_image/models/survey_question.py
2025-12-29 13:48:22 +07:00

87 lines
3.9 KiB
Python
Executable File

# -*- coding: utf-8 -*-
##############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2025-TODAY Cybrosys Technologies(<https://www.cybrosys.com>)
# Author: Mohammed Dilshad Tk (odoo@cybrosys.com)
#
# You can modify it under the terms of the GNU LESSER
# GENERAL PUBLIC LICENSE (LGPL v3), Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# (LGPL v3) along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from odoo import fields, models
class SurveyQuestion(models.Model):
"""
This class extends the 'survey.question' model to add new functionality
for file uploads.
"""
_inherit = 'survey.question'
question_type = fields.Selection(
selection_add=[('upload_file', 'Upload File')],
help='Select the type of question to create.')
upload_multiple_file = fields.Boolean(string='Upload Multiple File',
help='Check this box if you want to '
'allow users to upload '
'multiple files')
def validate_question(self, answer, comment=None):
"""Validate question answer."""
self.ensure_one()
if self.question_type == 'upload_file':
if self.constr_mandatory:
# Answer comes as a JSON string '[dataURLs, fileNames]' or raw list from previous steps?
# The controller passes what it received.
# If we parsed it in `_save_lines`, that's too late. Validation happens before.
# We need to handle the string if it's not parsed yet, or parsed if Odoo does something.
# Standard Odoo validation receives raw inputs usually.
import logging
from odoo.http import request
_logger = logging.getLogger(__name__)
# Fallback: Check for prefixed param if answer is empty
# because standard controller looks for str(id)
if not answer or answer == '[]':
# Try multiple key variants including empty string (common issue with t-att-name)
keys_to_check = [f'upload_{self.id}', str(self.id), '']
for key in keys_to_check:
val = request.params.get(key)
if val and val != '[]':
answer = val
break
is_answered = False
if answer and answer != '[]':
import json
try:
# If answer is a string, try parsing it
if isinstance(answer, str):
answer_data = json.loads(answer)
else:
answer_data = answer
# Check if we have dataURLs (first element of list)
if isinstance(answer_data, list) and len(answer_data) > 0 and answer_data[0]:
is_answered = True
except Exception as e:
_logger.error(f"VALIDATE QUESTION {self.id}: Error parsing answer: {e}")
is_answered = False
if not is_answered:
return {self.id: "CUSTOM TAG: This question requires an answer."}
return {}
return super(SurveyQuestion, self).validate_question(answer, comment)