Odoo modules related to surveys
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.

91 lines
4.4 KiB

  1. from odoo import models, fields, api
  2. def dict_keys_startswith(dictionary, string):
  3. """Returns a dictionary containing the elements of <dict> whose keys start with <string>.
  4. .. note::
  5. This function uses dictionary comprehensions (Python >= 2.7)
  6. """
  7. return {k: v for k, v in dictionary.items() if k.startswith(string)}
  8. class SurveyUserInputLine(models.Model):
  9. _inherit = 'survey.user_input_line'
  10. answer_type = fields.Selection([
  11. ('text', 'Text'),
  12. ('number', 'Number'),
  13. ('date', 'Date'),
  14. ('free_text', 'Free Text'),
  15. ('suggestion', 'Suggestion'), ('dropdown', 'Dropdown')], string='Answer Type')
  16. value_dropdown = fields.Many2one('survey.label.value', string="Value Dropdown")
  17. matrix_subtype_id = fields.Selection(related='question_id.matrix_subtype', string="Matrix subtype")
  18. @api.model
  19. def save_line_matrix(self, user_input_id, question, post, answer_tag):
  20. vals = {
  21. 'user_input_id': user_input_id,
  22. 'question_id': question.id,
  23. 'survey_id': question.survey_id.id,
  24. 'skipped': False
  25. }
  26. old_uil = self.search([
  27. ('user_input_id', '=', user_input_id),
  28. ('survey_id', '=', question.survey_id.id),
  29. ('question_id', '=', question.id)
  30. ])
  31. old_uil.sudo().unlink()
  32. no_answers = True
  33. ca_dict = dict_keys_startswith(post, answer_tag + '_')
  34. comment_answer = ca_dict.pop(("%s_%s" % (answer_tag, 'comment')), '').strip()
  35. if comment_answer:
  36. vals.update({'answer_type': 'text', 'value_text': comment_answer})
  37. self.create(vals)
  38. no_answers = False
  39. if question.matrix_subtype == 'simple':
  40. for row in question.labels_ids_2:
  41. a_tag = "%s_%s" % (answer_tag, row.id)
  42. if a_tag in ca_dict:
  43. no_answers = False
  44. vals.update({'answer_type': 'suggestion', 'value_suggested': ca_dict[a_tag], 'value_suggested_row': row.id})
  45. self.create(vals)
  46. elif question.matrix_subtype == 'multiple':
  47. for col in question.labels_ids:
  48. for row in question.labels_ids_2:
  49. a_tag = "%s_%s_%s" % (answer_tag, row.id, col.id)
  50. if a_tag in ca_dict:
  51. no_answers = False
  52. vals.update({'answer_type': 'suggestion', 'value_suggested': col.id, 'value_suggested_row': row.id})
  53. self.create(vals)
  54. elif question.matrix_subtype == 'custom_row':
  55. for col in question.labels_ids:
  56. for row in question.labels_ids_2:
  57. a_tag = "%s_%s_%s" % (answer_tag, row.id, col.id)
  58. if a_tag in ca_dict:
  59. no_answers = False
  60. if post.get(a_tag):
  61. sline = a_tag.split('_')[-1]
  62. label_obj = question.labels_ids.browse(int(sline))
  63. if label_obj.type == 'textbox':
  64. vals.update({'answer_type': 'text', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_text': post.get(a_tag)})
  65. elif label_obj.type == 'free_text':
  66. vals.update({'answer_type': 'free_text', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_free_text': post.get(a_tag)})
  67. elif label_obj.type == 'numerical_box':
  68. vals.update({'answer_type': 'number', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_number': post.get(a_tag)})
  69. elif label_obj.type == 'date':
  70. vals.update({'answer_type': 'date', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_date': post.get(a_tag)})
  71. elif label_obj.type == 'dropdown':
  72. vals.update({'answer_type': 'dropdown', 'value_suggested': col.id, 'value_suggested_row': row.id, 'value_dropdown': int(post.get(a_tag))})
  73. else:
  74. vals.update({'answer_type': 'suggestion', 'value_suggested': col.id, 'value_suggested_row': row.id})
  75. self.create(vals)
  76. if no_answers:
  77. vals.update({'answer_type': None, 'skipped': True})
  78. self.create(vals)
  79. return True