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.

74 lines
4.7 KiB

  1. import json
  2. from odoo import http
  3. from odoo.http import request
  4. import logging
  5. from odoo.addons.survey.controllers.main import Survey
  6. _logger = logging.getLogger(__name__)
  7. class Survey(Survey):
  8. @http.route(['/survey/prefill/<model("survey.survey"):survey>/<string:token>',
  9. '/survey/prefill/<model("survey.survey"):survey>/<string:token>/<model("survey.page"):page>'],
  10. type='http', auth='public', website=True)
  11. def prefill(self, survey, token, page=None, **post):
  12. UserInputLine = request.env['survey.user_input_line']
  13. ret = {}
  14. # Fetch previous answers
  15. if page:
  16. previous_answers = UserInputLine.sudo().search([('user_input_id.token', '=', token), ('page_id', '=', page.id)])
  17. else:
  18. previous_answers = UserInputLine.sudo().search([('user_input_id.token', '=', token)])
  19. # Return non empty answers in a JSON compatible format
  20. for answer in previous_answers:
  21. if not answer.skipped:
  22. answer_tag = '%s_%s_%s' % (answer.survey_id.id, answer.page_id.id, answer.question_id.id)
  23. answer_value = None
  24. if answer.question_id.matrix_subtype == 'custom_row':
  25. if answer.answer_type == 'free_text':
  26. answer_tag = "%s_%s_%s" % (answer_tag, answer.value_suggested_row.id, answer.value_suggested.id)
  27. answer_value = answer.value_free_text
  28. elif answer.answer_type == 'text':
  29. answer_tag = "%s_%s_%s" % (answer_tag, answer.value_suggested_row.id, answer.value_suggested.id)
  30. answer_value = answer.value_text
  31. elif answer.answer_type == 'number':
  32. answer_tag = "%s_%s_%s" % (answer_tag, answer.value_suggested_row.id, answer.value_suggested.id)
  33. answer_value = str(answer.value_number)
  34. elif answer.answer_type == 'date':
  35. answer_tag = "%s_%s_%s" % (answer_tag, answer.value_suggested_row.id, answer.value_suggested.id)
  36. answer_value = answer.value_date
  37. elif answer.answer_type == 'dropdown':
  38. answer_tag = "%s_%s_%s" % (answer_tag, answer.value_suggested_row.id, answer.value_suggested.id)
  39. answer_value = answer.value_dropdown.id
  40. elif answer.answer_type == 'suggestion' and not answer.value_suggested_row:
  41. answer_tag = "%s_%s_%s" % (answer_tag, answer.value_suggested_row.id, answer.value_suggested.id)
  42. answer_value = answer.value_suggested.id
  43. elif answer.answer_type == 'suggestion' and answer.value_suggested_row:
  44. answer_tag = "%s_%s_%s" % (answer_tag, answer.value_suggested_row.id, answer.value_suggested.id)
  45. answer_value = answer.value_suggested.id
  46. if answer_value:
  47. ret.setdefault(answer_tag, []).append(answer_value)
  48. else:
  49. _logger.warning("[survey] No answer has been found for question %s marked as non skipped" % answer_tag)
  50. else:
  51. if answer.answer_type == 'free_text':
  52. answer_value = answer.value_free_text
  53. elif answer.answer_type == 'text' and answer.question_id.type == 'textbox':
  54. answer_value = answer.value_text
  55. elif answer.answer_type == 'text' and answer.question_id.type != 'textbox':
  56. # here come comment answers for matrices, simple choice and multiple choice
  57. answer_tag = "%s_%s" % (answer_tag, 'comment')
  58. answer_value = answer.value_text
  59. elif answer.answer_type == 'number':
  60. answer_value = str(answer.value_number)
  61. elif answer.answer_type == 'date':
  62. answer_value = answer.value_date
  63. elif answer.answer_type == 'suggestion' and not answer.value_suggested_row:
  64. answer_value = answer.value_suggested.id
  65. elif answer.answer_type == 'suggestion' and answer.value_suggested_row:
  66. answer_tag = "%s_%s" % (answer_tag, answer.value_suggested_row.id)
  67. answer_value = answer.value_suggested.id
  68. if answer_value:
  69. ret.setdefault(answer_tag, []).append(answer_value)
  70. else:
  71. _logger.warning("[survey] No answer has been found for question %s marked as non skipped" % answer_tag)
  72. return json.dumps(ret)