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.

46 lines
1.8 KiB

2 years ago
  1. # -*- coding: utf-8 -*-
  2. from odoo import api, fields, models
  3. from odoo.addons.survey.models.survey import dict_keys_startswith
  4. class SurveyQuestion(models.Model):
  5. _inherit = "survey.question"
  6. matrix_subtype = fields.Selection(
  7. selection_add=[("custom", "Custom Matrix")],
  8. required=True,
  9. )
  10. @api.onchange("matrix_subtype")
  11. def onchange_matrix_subtype(self):
  12. self.ensure_one()
  13. if self.matrix_subtype != "custom":
  14. labels = self.labels_ids
  15. labels.filtered(
  16. lambda label: label.type not in ["checkbox", "dropdown"]
  17. ).update({"type": "checkbox"})
  18. labels.filtered(lambda label: label.type == "dropdown").update(
  19. {"type": "checkbox", "value_ids": [(5)]}
  20. )
  21. @api.multi
  22. def validate_matrix(self, post, answer_tag):
  23. self.ensure_one()
  24. errors = {}
  25. if self.constr_mandatory:
  26. lines_number = len(self.labels_ids_2)
  27. answer_candidates = dict_keys_startswith(post, answer_tag)
  28. answer_candidates.pop(("%s_%s" % (answer_tag, "comment")), "").strip()
  29. # Number of lines that have been answered
  30. if self.matrix_subtype == "simple":
  31. answer_number = len(answer_candidates)
  32. elif self.matrix_subtype == "multiple":
  33. answer_number = len({sk.rsplit("_", 1)[0] for sk in answer_candidates})
  34. elif self.matrix_subtype == "custom":
  35. answer_number = len({sk.rsplit("_", 1)[0] for sk in answer_candidates})
  36. else:
  37. raise RuntimeError("Invalid matrix subtype")
  38. # Validate that each line has been answered
  39. if answer_number != lines_number:
  40. errors.update({answer_tag: self.constr_error_msg})
  41. return errors