# -*- coding: utf-8 -*- from odoo import models, fields, api from markupsafe import Markup class SurveyUserInputLine(models.Model): _inherit = 'survey.user_input.line' file_display = fields.Html('File Display', compute='_compute_file_display') signature_display = fields.Html('Signature Display', compute='_compute_signature_display') many2one_display = fields.Html('Many2one Display', compute='_compute_many2one_display') many2many_display = fields.Html('Many2many Display', compute='_compute_many2many_display') extra_field_display = fields.Html('Extra Field Display', compute='_compute_extra_field_display') show_value_char_box = fields.Boolean('Show Value Char Box', compute='_compute_show_value_char_box') show_file_display = fields.Boolean('Show File Display', compute='_compute_show_file_display') show_signature_display = fields.Boolean('Show Signature Display', compute='_compute_show_signature_display') show_many2one_display = fields.Boolean('Show Many2one Display', compute='_compute_show_many2one_display') show_many2many_display = fields.Boolean('Show Many2many Display', compute='_compute_show_many2many_display') show_extra_field_display = fields.Boolean('Show Extra Field Display', compute='_compute_show_extra_field_display') answer_type_display = fields.Char('Answer Type Display', compute='_compute_answer_type_display') @api.depends('value_char_box', 'question_id.question_type', 'answer_type') def _compute_file_display(self): for line in self: if line.answer_type == 'char_box' and line.question_id and line.question_id.question_type == 'file': if line.value_char_box and line.value_char_box.isdigit(): attachment = self.env['ir.attachment'].sudo().browse(int(line.value_char_box)) if attachment.exists(): line.file_display = Markup(f'{attachment.name}') else: line.file_display = 'File not found' else: line.file_display = line.value_char_box or 'No file' else: line.file_display = False @api.depends('value_char_box', 'question_id.question_type', 'answer_type') def _compute_signature_display(self): for line in self: if line.answer_type == 'char_box' and line.question_id and line.question_id.question_type == 'signature': if line.value_char_box and line.value_char_box.startswith('data:image/'): line.signature_display = Markup(f'Signature') else: line.signature_display = 'No signature' else: line.signature_display = False @api.depends('value_char_box', 'question_id.question_type', 'answer_type') def _compute_many2one_display(self): for line in self: if line.answer_type == 'char_box' and line.question_id and line.question_id.question_type == 'many2one': if line.value_char_box and ',' in line.value_char_box: model_name, record_id = line.value_char_box.split(',', 1) try: record = self.env[model_name].sudo().browse(int(record_id)) if record.exists(): line.many2one_display = record.display_name else: line.many2one_display = 'Record not found' except: line.many2one_display = line.value_char_box else: line.many2one_display = line.value_char_box or 'No selection' else: line.many2one_display = False @api.depends('value_char_box', 'question_id.question_type', 'answer_type') def _compute_many2many_display(self): for line in self: if line.answer_type == 'char_box' and line.question_id and line.question_id.question_type == 'many2many': if line.value_char_box and line.question_id.many2many_model: record_ids = line.value_char_box.split(',') if line.value_char_box else [] names = [] for record_id in record_ids: try: record = self.env[line.question_id.many2many_model].sudo().browse(int(record_id.strip())) if record.exists(): names.append(record.display_name) except: continue line.many2many_display = ', '.join(names) if names else 'No selections' else: line.many2many_display = line.value_char_box or 'No selections' else: line.many2many_display = False @api.depends('value_char_box', 'question_id.question_type', 'answer_type') def _compute_extra_field_display(self): for line in self: if line.answer_type == 'char_box' and line.question_id and line.question_id.question_type in ['color', 'email', 'url', 'time', 'range', 'week', 'password', 'month', 'address', 'name']: if line.question_id.question_type == 'color': line.extra_field_display = Markup(f'
{line.value_char_box}') elif line.question_id.question_type == 'address': try: import json addr_data = json.loads(line.value_char_box) if line.value_char_box else {} parts = [] if addr_data.get('street'): parts.append(f"Street: {addr_data['street']}") if addr_data.get('street2'): parts.append(f"Street2: {addr_data['street2']}") if addr_data.get('zip'): parts.append(f"Zip: {addr_data['zip']}") if addr_data.get('city'): parts.append(f"City: {addr_data['city']}") if addr_data.get('state'): parts.append(f"State: {addr_data['state']}") if addr_data.get('country'): parts.append(f"Country: {addr_data['country']}") line.extra_field_display = Markup('
'.join(parts)) if parts else line.value_char_box or 'No address' except: line.extra_field_display = line.value_char_box or 'No address' elif line.question_id.question_type == 'name': try: import json name_data = json.loads(line.value_char_box) if line.value_char_box else {} parts = [] if name_data.get('first_name'): parts.append(f"First Name: {name_data['first_name']}") if name_data.get('middle_name'): parts.append(f"Middle Name: {name_data['middle_name']}") if name_data.get('last_name'): parts.append(f"Last Name: {name_data['last_name']}") line.extra_field_display = Markup('
'.join(parts)) if parts else line.value_char_box or 'No name' except: line.extra_field_display = line.value_char_box or 'No name' else: line.extra_field_display = line.value_char_box or '' else: line.extra_field_display = False @api.depends('answer_type', 'question_id.question_type') def _compute_show_value_char_box(self): for line in self: if line.answer_type == 'char_box' and line.question_id and line.question_id.question_type in ['color', 'email', 'url', 'time', 'range', 'week', 'password', 'file', 'signature', 'month', 'address', 'name', 'many2one', 'many2many']: line.show_value_char_box = False else: line.show_value_char_box = True @api.depends('answer_type', 'question_id.question_type') def _compute_show_file_display(self): for line in self: line.show_file_display = (line.answer_type == 'char_box' and line.question_id and line.question_id.question_type == 'file') @api.depends('answer_type', 'question_id.question_type') def _compute_show_signature_display(self): for line in self: line.show_signature_display = (line.answer_type == 'char_box' and line.question_id and line.question_id.question_type == 'signature') @api.depends('answer_type', 'question_id.question_type') def _compute_show_many2one_display(self): for line in self: line.show_many2one_display = (line.answer_type == 'char_box' and line.question_id and line.question_id.question_type == 'many2one') @api.depends('answer_type', 'question_id.question_type') def _compute_show_many2many_display(self): for line in self: line.show_many2many_display = (line.answer_type == 'char_box' and line.question_id and line.question_id.question_type == 'many2many') @api.depends('answer_type', 'question_id.question_type') def _compute_show_extra_field_display(self): for line in self: line.show_extra_field_display = (line.answer_type == 'char_box' and line.question_id and line.question_id.question_type in ['color', 'email', 'url', 'time', 'range', 'week', 'password', 'month', 'address', 'name']) @api.depends('answer_type', 'question_id.question_type') def _compute_answer_type_display(self): for line in self: if line.answer_type == 'char_box' and line.question_id and line.question_id.question_type in ['color', 'email', 'url', 'time', 'range', 'week', 'password', 'file', 'signature', 'month', 'address', 'name', 'many2one', 'many2many']: type_mapping = { 'color': 'Color', 'email': 'Email', 'url': 'URL', 'time': 'Time', 'range': 'Range', 'week': 'Week', 'password': 'Password', 'file': 'File', 'signature': 'Signature', 'month': 'Month', 'address': 'Address', 'name': 'Name', 'many2one': 'Selection', 'many2many': 'Multiple Selection' } line.answer_type_display = type_mapping.get(line.question_id.question_type, 'Text') else: type_mapping = { 'text_box': 'Free Text', 'char_box': 'Text', 'numerical_box': 'Number', 'scale': 'Number', 'date': 'Date', 'datetime': 'Datetime', 'suggestion': 'Suggestion' } line.answer_type_display = type_mapping.get(line.answer_type, line.answer_type or '') @api.depends( 'answer_type', 'value_text_box', 'value_numerical_box', 'value_char_box', 'value_date', 'value_datetime', 'suggested_answer_id.value', 'matrix_row_id.value', 'question_id.question_type' ) def _compute_display_name(self): for line in self: if line.answer_type == 'char_box' and line.question_id and line.question_id.question_type in ['color', 'email', 'url', 'time', 'range', 'week', 'password', 'file', 'signature', 'month', 'address', 'name', 'many2one', 'many2many']: if line.question_id.question_type == 'file': if line.value_char_box and line.value_char_box.isdigit(): attachment = self.env['ir.attachment'].sudo().browse(int(line.value_char_box)) line.display_name = attachment.name if attachment.exists() else 'File not found' else: line.display_name = line.value_char_box or 'No file' elif line.question_id.question_type == 'many2one': if line.value_char_box and ',' in line.value_char_box: model_name, record_id = line.value_char_box.split(',', 1) try: record = self.env[model_name].sudo().browse(int(record_id)) line.display_name = record.display_name if record.exists() else 'Record not found' except: line.display_name = line.value_char_box else: line.display_name = line.value_char_box or 'No selection' elif line.question_id.question_type == 'many2many': if line.value_char_box and line.question_id.many2many_model: record_ids = line.value_char_box.split(',') if line.value_char_box else [] names = [] for record_id in record_ids: try: record = self.env[line.question_id.many2many_model].sudo().browse(int(record_id.strip())) if record.exists(): names.append(record.display_name) except: continue line.display_name = ', '.join(names) if names else 'No selections' else: line.display_name = line.value_char_box or 'No selections' else: line.display_name = line.value_char_box or '' else: super(SurveyUserInputLine, line)._compute_display_name()