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.

64 lines
3.0 KiB

  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("Started sent survey count", compute="_count_sent_input")
  6. tot_sent_comp_survey = fields.Integer("Completed sent survey count", compute="_count_sent_input")
  7. sent_start_ratio = fields.Integer(string="Started sent survey ratio", compute="_get_sent_start_ratio")
  8. sent_comp_ratio = fields.Integer(string="Completed sent survey ratio", compute="_get_sent_comp_ratio")
  9. # COMPUTES
  10. @api.multi
  11. def _count_sent_input(self):
  12. UserInput = self.env['survey.user_input']
  13. sent_start_survey = UserInput.search([('survey_id', 'in', self.ids), ('type', '=', 'link'), ('state', '=', 'skip')])
  14. sent_comp_survey = UserInput.search([('survey_id', 'in', self.ids), ('type', '=', 'link'), ('state', '=', 'done')])
  15. for survey in self:
  16. survey.tot_sent_start_survey = len(sent_start_survey.filtered(lambda user_input: user_input.survey_id == survey))
  17. survey.tot_sent_comp_survey = len(sent_comp_survey.filtered(lambda user_input: user_input.survey_id == survey))
  18. @api.depends('tot_sent_start_survey', 'tot_sent_survey')
  19. def _get_sent_start_ratio(self):
  20. for survey in self:
  21. if survey.tot_sent_survey == 0:
  22. survey.sent_start_ratio = 0
  23. else:
  24. survey.sent_start_ratio = int(round(100 * (survey.tot_sent_start_survey) / survey.tot_sent_survey, 0))
  25. @api.depends('tot_sent_comp_survey', 'tot_sent_survey')
  26. def _get_sent_comp_ratio(self):
  27. for survey in self:
  28. if survey.tot_sent_survey == 0:
  29. survey.sent_comp_ratio = 0
  30. else:
  31. survey.sent_comp_ratio = int(round(100 * survey.tot_sent_comp_survey / survey.tot_sent_survey, 0))
  32. # ACTIONS
  33. @api.multi
  34. def action_survey_user_input(self):
  35. ctx = dict(self.env.context)
  36. search_completed = ctx.get('search_default_completed', None)
  37. action = super(SurveySurvey, self).action_survey_user_input()
  38. if ctx.get('link_only', False):
  39. domain = action.get('domain') or []
  40. if isinstance(domain, str):
  41. domain = eval(domain)
  42. if len(domain) > 1:
  43. action['domain'] = AND([[('type', '=', 'link')], normalize_domain(domain)])
  44. else:
  45. action['domain'] = [('type', '=', 'link')]
  46. action['display_name'] += _(" (from private links)")
  47. if search_completed is not None:
  48. act_ctx = action.get('context') or {}
  49. if isinstance(act_ctx, str):
  50. act_ctx = eval(act_ctx)
  51. if 'search_default_completed' in act_ctx:
  52. if bool(act_ctx['search_default_completed']) is not bool(search_completed):
  53. act_ctx['search_default_completed'] = int(bool(search_completed))
  54. action['context'] = act_ctx
  55. return action