From d1e4ecfd7fe0121b882c20e433379c2072ef9ff6 Mon Sep 17 00:00:00 2001 From: RemiFr82 Date: Fri, 31 Jan 2025 23:11:25 +0100 Subject: [PATCH] [MIG] survey_select_input --- survey_select_input/__manifest__.py | 11 +++- survey_select_input/models/survey_survey.py | 53 ++++++++++++++----- .../models/survey_user_input.py | 25 ++++++--- .../src/js/survey_state_selection_field.js | 24 +++++++++ survey_select_input/views/survey_survey.xml | 26 ++++++--- .../views/survey_user_input.xml | 47 ++++++++-------- 6 files changed, 135 insertions(+), 51 deletions(-) create mode 100644 survey_select_input/static/src/js/survey_state_selection_field.js diff --git a/survey_select_input/__manifest__.py b/survey_select_input/__manifest__.py index 7362ec5..fad3fdf 100644 --- a/survey_select_input/__manifest__.py +++ b/survey_select_input/__manifest__.py @@ -17,7 +17,7 @@ # "price": 0, # "currency": "EUR", "application": False, - "installable": False, + "installable": True, "auto_install": False, "pre_init_hook": "", "post_init_hook": "", @@ -31,6 +31,15 @@ "views/survey_survey.xml", "views/survey_user_input.xml", ], + "assets": { + "web.assets_backend": [ + ( + "after", + "web/static/src/views/fields/state_selection/state_selection_field.js", + "survey_select_input/static/src/js/survey_state_selection_field.js", + ) + ] + }, "css": [], "images": [], "js": [], diff --git a/survey_select_input/models/survey_survey.py b/survey_select_input/models/survey_survey.py index c1d4ade..f5ba858 100644 --- a/survey_select_input/models/survey_survey.py +++ b/survey_select_input/models/survey_survey.py @@ -4,25 +4,54 @@ from odoo import models, fields, api, _ class SurveySurvey(models.Model): _inherit = "survey.survey" - tot_selected_survey = fields.Integer( - "Number of selected surveys", compute="_get_selected_input" + answer_selection = fields.Boolean( + string="Manual selection", + help="Checking this box allows you to select completed real answers (regardless of the scoring success, if enable).", + ) + answer_selected_count = fields.Integer( + "Number of selected answers", compute="_compute_selected_answer" + ) + answer_selected_ratio = fields.Integer( + "Ratio of selected anwers", compute="_compute_selected_answer" ) - @api.depends("user_input_ids", "user_input_ids.selected") - def _get_selected_input(self): - selected_survey = self.env["survey.user_input"].search( - [("survey_id", "in", self.ids), ("selected", "=", True)] + @api.depends("user_input_ids", "user_input_ids.select") + def _compute_selected_answer(self): + default_vals = { + "answer_count": 0, + "answer_selected_count": 0, + } + stats = dict((sid, default_vals) for sid in self.ids) + UserInput = self.env["survey.user_input"] + base_domain = [ + ("survey_id", "in", self.ids), + ("state", "=", "done"), + ("test_entry", "=", False), + ] + read_group_res = UserInput._read_group( + base_domain, + ["survey_id", "select"], + ["survey_id", "select"], + lazy=False, ) - for survey in self: - survey.tot_selected_survey = len( - selected_survey.filtered( - lambda user_input: user_input.survey_id == survey - ) + + for item in read_group_res: + stats[item["survey_id"][0]]["answer_count"] += item["__count"] + if item["select"] == "done": + stats[item["survey_id"][0]]["answer_selected_count"] += item["__count"] + + for survey_stats in stats.values(): + survey_stats["answer_selected_ratio"] = ( + survey_stats["answer_selected_count"] + / (survey_stats["answer_count"] or 1) + * 100 ) + for survey in self: + survey.update(stats.get(survey._origin.id, default_vals)) + # ACTIONS - @api.multi def action_survey_user_input(self): action = super(SurveySurvey, self).action_survey_user_input() if self.env.context.get("search_default_selected", False): diff --git a/survey_select_input/models/survey_user_input.py b/survey_select_input/models/survey_user_input.py index 94e2e82..e1abce4 100644 --- a/survey_select_input/models/survey_user_input.py +++ b/survey_select_input/models/survey_user_input.py @@ -1,13 +1,24 @@ from odoo import models, fields, api +SELECT_OPTIONS = [ + ("normal", "Undefined"), + ("done", "Selected"), + ("blocked", "Not selected"), +] + + class SurveyUserInput(models.Model): _inherit = "survey.user_input" - selected = fields.Boolean(string="Selected", default=False) - - @api.multi - def toggle_selected(self): - unselect = self.filtered("selected") - unselect.write({"selected": False}) - (self - unselect).write({"selected": True}) + answer_selection = fields.Boolean( + related="survey_id.answer_selection", + store=True, + ) + select = fields.Selection( + SELECT_OPTIONS, + string="Selection", + copy=False, + default="normal", + required=True, + ) diff --git a/survey_select_input/static/src/js/survey_state_selection_field.js b/survey_select_input/static/src/js/survey_state_selection_field.js new file mode 100644 index 0000000..f11837e --- /dev/null +++ b/survey_select_input/static/src/js/survey_state_selection_field.js @@ -0,0 +1,24 @@ +/** @odoo-module **/ + +import { Dropdown } from "@web/core/dropdown/dropdown"; +import { DropdownItem } from "@web/core/dropdown/dropdown_item"; +import { registry } from "@web/core/registry"; +import { StateSelectionField } from '@web/views/fields/state_selection/state_selection_field'; +import { _lt } from "@web/core/l10n/translation"; +import { standardFieldProps } from "@web/views/fields/standard_field_props"; + +export class SurveyStateSelectionField extends StateSelectionField { +// export class StateSelectionField extends StateSelectionField { + setup() { + super.setup(); + } + /** + * @override + */ + get showLabel() { + return !this.props.hideLabel; + } + +} + +registry.category("fields").add("form.survey_state_selection", SurveyStateSelectionField); diff --git a/survey_select_input/views/survey_survey.xml b/survey_select_input/views/survey_survey.xml index ac08bc3..1a836bd 100644 --- a/survey_select_input/views/survey_survey.xml +++ b/survey_select_input/views/survey_survey.xml @@ -1,20 +1,32 @@ - + survey_select_input survey.survey form survey.survey - + 26 - + + + + o_label_nowrap + + + + diff --git a/survey_select_input/views/survey_user_input.xml b/survey_select_input/views/survey_user_input.xml index c2a557b..f19aaea 100644 --- a/survey_select_input/views/survey_user_input.xml +++ b/survey_select_input/views/survey_user_input.xml @@ -1,62 +1,61 @@ - + survey_select_input survey.user_input form survey.user_input - + 26 - -
- -
+ + + +
- + survey_select_input survey.user_input tree survey.user_input - + 26 - - + + - + survey_select_input survey.user_input kanban survey.user_input - + 26 - - + + + - Selected + - + survey_select_input survey.user_input search survey.user_input - + 26 - - - + +