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.

146 lines
6.3 KiB

  1. # -*- coding: utf-8 -*-
  2. import json
  3. from odoo import http
  4. from odoo.http import request
  5. from odoo.addons.survey.controllers.main import Survey
  6. import logging
  7. _logger = logging.getLogger(__name__)
  8. class Survey(Survey):
  9. @http.route(
  10. [
  11. '/survey/prefill/<model("survey.survey"):survey>/<string:token>',
  12. '/survey/prefill/<model("survey.survey"):survey>/<string:token>/<model("survey.page"):page>',
  13. ],
  14. type="http",
  15. auth="public",
  16. website=True,
  17. )
  18. def prefill(self, survey, token, page=None, **post):
  19. UserInputLine = request.env["survey.user_input_line"]
  20. ret = {}
  21. # Fetch previous answers
  22. if page:
  23. previous_answers = UserInputLine.sudo().search(
  24. [("user_input_id.token", "=", token), ("page_id", "=", page.id)]
  25. )
  26. else:
  27. previous_answers = UserInputLine.sudo().search(
  28. [("user_input_id.token", "=", token)]
  29. )
  30. # Return non empty answers in a JSON compatible format
  31. for answer in previous_answers:
  32. if not answer.skipped:
  33. answer_tag = "%s_%s_%s" % (
  34. answer.survey_id.id,
  35. answer.page_id.id,
  36. answer.question_id.id,
  37. )
  38. answer_value = None
  39. if answer.question_id.matrix_subtype == "custom":
  40. if answer.answer_type == "free_text":
  41. answer_tag = "%s_%s_%s" % (
  42. answer_tag,
  43. answer.value_suggested_row.id,
  44. answer.value_suggested.id,
  45. )
  46. answer_value = answer.value_free_text
  47. elif answer.answer_type == "text":
  48. answer_tag = "%s_%s_%s" % (
  49. answer_tag,
  50. answer.value_suggested_row.id,
  51. answer.value_suggested.id,
  52. )
  53. answer_value = answer.value_text
  54. elif answer.answer_type == "number":
  55. answer_tag = "%s_%s_%s" % (
  56. answer_tag,
  57. answer.value_suggested_row.id,
  58. answer.value_suggested.id,
  59. )
  60. answer_value = str(answer.value_number)
  61. elif answer.answer_type == "date":
  62. answer_tag = "%s_%s_%s" % (
  63. answer_tag,
  64. answer.value_suggested_row.id,
  65. answer.value_suggested.id,
  66. )
  67. answer_value = answer.value_date
  68. elif answer.answer_type == "dropdown":
  69. answer_tag = "%s_%s_%s" % (
  70. answer_tag,
  71. answer.value_suggested_row.id,
  72. answer.value_suggested.id,
  73. )
  74. answer_value = answer.value_id.id
  75. elif (
  76. answer.answer_type == "suggestion"
  77. and not answer.value_suggested_row
  78. ):
  79. answer_tag = "%s_%s_%s" % (
  80. answer_tag,
  81. answer.value_suggested_row.id,
  82. answer.value_suggested.id,
  83. )
  84. answer_value = answer.value_suggested.id
  85. elif (
  86. answer.answer_type == "suggestion"
  87. and answer.value_suggested_row
  88. ):
  89. answer_tag = "%s_%s_%s" % (
  90. answer_tag,
  91. answer.value_suggested_row.id,
  92. answer.value_suggested.id,
  93. )
  94. answer_value = answer.value_suggested.id
  95. if answer_value:
  96. ret.setdefault(answer_tag, []).append(answer_value)
  97. else:
  98. _logger.warning(
  99. "[survey] No answer has been found for question %s marked as non skipped"
  100. % answer_tag
  101. )
  102. else:
  103. if answer.answer_type == "free_text":
  104. answer_value = answer.value_free_text
  105. elif (
  106. answer.answer_type == "text"
  107. and answer.question_id.type == "textbox"
  108. ):
  109. answer_value = answer.value_text
  110. elif (
  111. answer.answer_type == "text"
  112. and answer.question_id.type != "textbox"
  113. ):
  114. # here come comment answers for matrices, simple choice and multiple choice
  115. answer_tag = "%s_%s" % (answer_tag, "comment")
  116. answer_value = answer.value_text
  117. elif answer.answer_type == "number":
  118. answer_value = str(answer.value_number)
  119. elif answer.answer_type == "date":
  120. answer_value = answer.value_date
  121. elif (
  122. answer.answer_type == "suggestion"
  123. and not answer.value_suggested_row
  124. ):
  125. answer_value = answer.value_suggested.id
  126. elif (
  127. answer.answer_type == "suggestion"
  128. and answer.value_suggested_row
  129. ):
  130. answer_tag = "%s_%s" % (
  131. answer_tag,
  132. answer.value_suggested_row.id,
  133. )
  134. answer_value = answer.value_suggested.id
  135. if answer_value:
  136. ret.setdefault(answer_tag, []).append(answer_value)
  137. else:
  138. _logger.warning(
  139. "[survey] No answer has been found for question %s marked as non skipped"
  140. % answer_tag
  141. )
  142. return json.dumps(ret)