from odoo import models, fields, api, _ from odoo.osv.expression import normalize_domain, AND class SurveySurvey(models.Model): _inherit = "survey.survey" answer_start_count = fields.Integer( "Started answer count", compute="_compute_answer_statistics" ) answer_start_ratio = fields.Integer( string="Started answer ratio", compute="_compute_answer_statistics" ) answer_done_ratio = fields.Integer( string="Completed answer ratio", compute="_compute_answer_statistics" ) # COMPUTES def _compute_answer_statistics(self): default_vals = { "answer_count": 0, "answer_start_count": 0, "answer_done_count": 0, "answer_start_ratio": 0, "answer_done_ratio": 0, } stat = dict((sid, default_vals) for sid in self.ids) UserInput = self.env["survey.user_input"] base_domain = [ ("survey_id", "in", self.ids), ("invite_token", "!=", False), ] read_group_res = UserInput._read_group( base_domain, ["survey_id", "state"], ["survey_id", "state"], lazy=False, ) for item in read_group_res: stat[item["survey_id"][0]]["answer_count"] += item["__count"] if item["state"] in ["in_progress", "done"]: stat[item["survey_id"][0]]["answer_start_count"] += item["__count"] if item["state"] == "done": stat[item["survey_id"][0]]["answer_done_count"] += item["__count"] for survey_stats in stat.values(): survey_stats["answer_start_ratio"] = ( survey_stats["answer_start_count"] / (survey_stats["answer_count"] or 1) * 100 ) survey_stats["answer_done_ratio"] = ( survey_stats["answer_done_count"] / (survey_stats["answer_count"] or 1) * 100 ) for survey in self: survey.update(stat.get(survey._origin.id, default_vals)) @api.depends("answer_start_count", "answer_count") def _get_answer_start_ratio(self): for survey in self: survey.answer_start_ratio = int( round(100 * (survey.answer_start_count) / (survey.answer_count or 1), 0) ) @api.depends("answer_done_count", "answer_count") def _get_answer_done_ratio(self): for survey in self: survey.answer_done_ratio = int( round(100 * survey.answer_done_count / (survey.answer_count or 1), 0) ) # ACTIONS def action_survey_user_input(self): ctx = dict(self.env.context) search_done = ctx.get("search_default_completed", None) action = super(SurveySurvey, self).action_survey_user_input() if ctx.get("search_default_shared_invite", False): action["display_name"] += _(" (shared)") if search_done is not None: act_ctx = action.get("context") or dict() if isinstance(act_ctx, str): act_ctx = eval(act_ctx) if "search_default_completed" in act_ctx and bool( act_ctx["search_default_completed"] ) is not bool(search_done): act_ctx["search_default_completed"] = int(bool(search_done)) action["context"] = act_ctx return action