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.

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