You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

119 lines
4.8 KiB

# -*- coding: utf-8 -*-
##############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2024-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 models, _
from odoo.exceptions import UserError
class SurveyUserInput(models.Model):
"""
This class extends the 'survey.user_input' model to add custom
functionality for saving user answers.
Methods:
_save_lines: Save the user's answer for the given question
_save_line_file:Save the user's file upload answer for the given
question
_get_line_answer_file_upload_values:
Get the values to use when creating or updating a user input line
for a file upload answer
"""
_inherit = "survey.user_input"
def _save_line_file_upload(self, question, old_answers, answer):
""" Save the user's file upload answer for the given question."""
print('old_answers', old_answers)
vals = self._get_line_answer_file_upload_values(question,
'upload_file', answer)
if old_answers:
old_answers.write(vals)
return old_answers
else:
return self.env['survey.user_input.line'].create(vals)
def _save_lines(self, question, answer, comment=None,
overwrite_existing=True):
""" Save answers to questions, depending on question type.
:param bool overwrite_existing: if an answer already exists for question and user_input_id
it will be overwritten (or deleted for 'choice' questions) in order to maintain data consistency.
:raises UserError: if line exists and overwrite_existing is False
"""
old_answers = self.env['survey.user_input.line'].search([
('user_input_id', '=', self.id),
('question_id', '=', question.id)
])
if old_answers and not overwrite_existing:
raise UserError(_("This answer cannot be overwritten."))
if question.question_type in ['char_box', 'text_box', 'numerical_box',
'date', 'datetime']:
self._save_line_simple_answer(question, old_answers, answer)
if question.save_as_email and answer:
self.write({'email': answer})
if question.save_as_nickname and answer:
self.write({'nickname': answer})
elif question.question_type in ['simple_choice', 'multiple_choice']:
self._save_line_choice(question, old_answers, answer, comment)
elif question.question_type == 'matrix':
self._save_line_matrix(question, old_answers, answer, comment)
elif question.question_type == 'upload_file':
self._save_line_file_upload(question, old_answers, answer)
else:
raise AttributeError(
question.question_type + ": This type of question has no saving function")
def _get_line_answer_file_upload_values(self, question, answer_type,
answer):
answer = answer[0]
vals = {
'user_input_id': self.id,
'question_id': question.id,
'skipped': False,
'answer_type': answer_type,
'display_name': 'Upload File',
}
if answer_type == 'upload_file':
# Parse JSON if needed
attachments = []
for file_info in answer:
if not isinstance(file_info, dict):
raise UserError("Each uploaded file must be a dictionary.")
file_data = file_info.get('data')
file_name = file_info.get('name')
if file_data and file_name:
attachment = self.env['ir.attachment'].create({
'name': file_name,
'type': 'binary',
'datas': file_data,
})
attachments.append((4, attachment.id))
else:
raise UserError("Missing file name or data.")
vals['value_file_data_ids'] = attachments
return vals