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.

117 lines
4.2 KiB

1 year ago
1 year ago
1 year ago
  1. from odoo import models, fields, api, _
  2. from odoo.osv.expression import normalize_domain, AND
  3. class SurveySurvey(models.Model):
  4. _inherit = "survey.survey"
  5. tot_sent_start_survey = fields.Integer(
  6. "Started sent survey count", compute="_count_sent_input"
  7. )
  8. tot_sent_comp_survey = fields.Integer(
  9. "Completed sent survey count", compute="_count_sent_input"
  10. )
  11. sent_start_ratio = fields.Integer(
  12. string="Started sent survey ratio", compute="_get_sent_start_ratio"
  13. )
  14. sent_comp_ratio = fields.Integer(
  15. string="Completed sent survey ratio", compute="_get_sent_comp_ratio"
  16. )
  17. # COMPUTES
  18. @api.multi
  19. def _count_sent_input(self):
  20. UserInput = self.env["survey.user_input"]
  21. sent_start_survey = UserInput
  22. if hasattr(UserInput, "date_start") and hasattr(UserInput, "date_done"):
  23. # Made to be more precise on searching
  24. # if survey_user_input_dates module is installed
  25. # so it also finds one page surveys started
  26. # which remain to 'new' state until submission
  27. sent_start_survey = UserInput.search(
  28. [
  29. ("survey_id", "in", self.ids),
  30. ("type", "=", "link"),
  31. ("date_start", "!=", False),
  32. ("date_done", "=", False),
  33. ]
  34. )
  35. else:
  36. sent_start_survey = UserInput.search(
  37. [
  38. ("survey_id", "in", self.ids),
  39. ("type", "=", "link"),
  40. ("state", "=", "skip"),
  41. ]
  42. )
  43. sent_comp_survey = UserInput.search(
  44. [
  45. ("survey_id", "in", self.ids),
  46. ("type", "=", "link"),
  47. ("state", "=", "done"),
  48. ]
  49. )
  50. for survey in self:
  51. survey.tot_sent_start_survey = len(
  52. sent_start_survey.filtered(
  53. lambda user_input: user_input.survey_id.id == survey.id
  54. )
  55. )
  56. survey.tot_sent_comp_survey = len(
  57. sent_comp_survey.filtered(
  58. lambda user_input: user_input.survey_id.id == survey.id
  59. )
  60. )
  61. @api.depends("tot_sent_start_survey", "tot_sent_survey")
  62. def _get_sent_start_ratio(self):
  63. for survey in self:
  64. if survey.tot_sent_survey == 0:
  65. survey.sent_start_ratio = 0
  66. else:
  67. survey.sent_start_ratio = int(
  68. round(
  69. 100 * (survey.tot_sent_start_survey) / survey.tot_sent_survey, 0
  70. )
  71. )
  72. @api.depends("tot_sent_comp_survey", "tot_sent_survey")
  73. def _get_sent_comp_ratio(self):
  74. for survey in self:
  75. if survey.tot_sent_survey == 0:
  76. survey.sent_comp_ratio = 0
  77. else:
  78. survey.sent_comp_ratio = int(
  79. round(100 * survey.tot_sent_comp_survey / survey.tot_sent_survey, 0)
  80. )
  81. # ACTIONS
  82. @api.multi
  83. def action_survey_user_input(self):
  84. ctx = dict(self.env.context)
  85. search_completed = ctx.get("search_default_completed", None)
  86. action = super(SurveySurvey, self).action_survey_user_input()
  87. if ctx.get("link_only", False):
  88. domain = action.get("domain") or []
  89. if isinstance(domain, str):
  90. domain = eval(domain)
  91. if len(domain) > 1:
  92. action["domain"] = AND(
  93. [[("type", "=", "link")], normalize_domain(domain)]
  94. )
  95. else:
  96. action["domain"] = [("type", "=", "link")]
  97. action["display_name"] += _(" (from private links)")
  98. if search_completed is not None:
  99. act_ctx = action.get("context") or {}
  100. if isinstance(act_ctx, str):
  101. act_ctx = eval(act_ctx)
  102. if "search_default_completed" in act_ctx:
  103. if bool(act_ctx["search_default_completed"]) is not bool(
  104. search_completed
  105. ):
  106. act_ctx["search_default_completed"] = int(bool(search_completed))
  107. action["context"] = act_ctx
  108. return action