-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] survey_question_type_five_star: Migration to 17.0
- Loading branch information
youstina.nabil
committed
May 9, 2024
1 parent
ef5a882
commit 17ccba9
Showing
14 changed files
with
195 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,56 @@ | ||
# Copyright 2018 ACSONE SA/NV | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
import collections | ||
import contextlib | ||
|
||
from odoo import fields, models, tools | ||
from odoo import fields, models | ||
|
||
|
||
class SurveyQuestion(models.Model): | ||
_inherit = "survey.question" | ||
|
||
question_type = fields.Selection( | ||
selection_add=[("star_rate", "Five Stars Rating")], ondelete={"foo": "set null"} | ||
) | ||
_inherit = "survey.question" | ||
|
||
def _get_stats_summary_data(self, user_input_lines): | ||
stats = super()._get_stats_summary_data(user_input_lines) | ||
if self.question_type in ["star_rate"]: | ||
stats.update(self._get_stats_summary_data_numerical(user_input_lines)) | ||
stats.update( | ||
{ | ||
"common_lines": collections.Counter( | ||
user_input_lines.filtered(lambda line: not line.skipped).mapped( | ||
"value_numerical_box" | ||
) | ||
).most_common(5), | ||
"right_inputs_count": len( | ||
user_input_lines.filtered( | ||
lambda line: line.answer_is_correct | ||
).mapped("user_input_id") | ||
), | ||
} | ||
) | ||
return stats | ||
question_type = fields.Selection( | ||
selection_add=[("star_rate", "Five Stars Rating")], | ||
ondelete={"foo": "set null"}, | ||
) | ||
|
||
def validate_star_rate(self, post, answer_tag): | ||
self.ensure_one() | ||
errors = {} | ||
answer = post[answer_tag].strip() | ||
# Empty answer to mandatory question | ||
if self.constr_mandatory and not answer: | ||
errors.update({answer_tag: self.constr_error_msg}) | ||
# Checks if user input is a number | ||
if answer: | ||
try: | ||
floatanswer = float(answer) | ||
except ValueError: | ||
errors.update({answer_tag: "This is not a number"}) | ||
return errors | ||
# Answer is not in the right range | ||
with tools.ignore(Exception): | ||
# 0 answer to mandatory question | ||
if self.constr_mandatory: | ||
if floatanswer == 0: | ||
errors.update({answer_tag: self.constr_error_msg}) | ||
if not (0 <= floatanswer <= 5): | ||
errors.update({answer_tag: "Answer is not in the right range"}) | ||
def _get_stats_summary_data(self, user_input_lines): | ||
stats = super()._get_stats_summary_data(user_input_lines) | ||
if self.question_type in ["star_rate"]: | ||
stats.update(self._get_stats_summary_data_numerical(user_input_lines)) | ||
stats.update({ | ||
"common_lines": | ||
collections.Counter( | ||
user_input_lines.filtered(lambda line: not line.skipped).mapped( | ||
"value_numerical_box")).most_common(5), | ||
"right_inputs_count": | ||
len( | ||
user_input_lines.filtered(lambda line: line.answer_is_correct).mapped( | ||
"user_input_id")), | ||
}) | ||
return stats | ||
|
||
def validate_star_rate(self, post, answer_tag): | ||
self.ensure_one() | ||
errors = {} | ||
answer = post[answer_tag].strip() | ||
# Empty answer to mandatory question | ||
if self.constr_mandatory and not answer: | ||
errors.update({answer_tag: self.constr_error_msg}) | ||
# Checks if user input is a number | ||
if answer: | ||
try: | ||
floatanswer = float(answer) | ||
except ValueError: | ||
errors.update({answer_tag: "This is not a number"}) | ||
return errors | ||
# Answer is not in the right range | ||
with contextlib.suppress(Exception): | ||
# 0 answer to mandatory question | ||
if self.constr_mandatory: | ||
if floatanswer == 0: | ||
errors.update({answer_tag: self.constr_error_msg}) | ||
if not (0 <= floatanswer <= 5): | ||
errors.update({answer_tag: "Answer is not in the right range"}) | ||
return errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* Souheil Bejaoui <souheil.bejaoui@acsone.eu> | ||
* Olga Marco <olga.marco@creublanca.es> | ||
* Benoit Aimont <benoit.aimont@acsone.eu> |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
To use this module, you need to: | ||
|
||
#. Go to Surveys, create a new survey, add a question of type "Five Stars Rating" | ||
|
||
.. image:: /OCA/survey/survey_question_type_five_star/static/description/five_star_type.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,39 @@ | ||
/* Copyright 2018 ACSONE SA/NV | ||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).*/ | ||
odoo.define("survey_question_type_five_star.survey", function (require) { | ||
"use strict"; | ||
var SurveyFormWidget = require("survey.form"); | ||
SurveyFormWidget.include({ | ||
events: _.extend({}, SurveyFormWidget.prototype.events, { | ||
"click .rate > label": "_onClickFiveStarLabel", | ||
}), | ||
_onClickFiveStarLabel: function (event) { | ||
if (this.readonly) { | ||
return; | ||
} | ||
var target = event.target; | ||
var label_items = $(target).parent().find("label"); | ||
odoo.define("survey_question_type_five_star", [], function (require) { | ||
"use strict"; | ||
|
||
var value = label_items.length - $(target).index(); | ||
label_items.removeClass("checked fa-star").addClass("fa-star-o"); | ||
label_items | ||
.slice($(target).index()) | ||
.addClass("checked fa-star") | ||
.removeClass("fa-star-o"); | ||
var $input = $(target).parent().find("input"); | ||
$input.val(value); | ||
// We will trigger the change in order to make it compatible with conditional. | ||
// If it is not installed, it has no effects | ||
$input.trigger("change"); | ||
}, | ||
_prepareSubmitValues: function (formData, params) { | ||
this._super.apply(this, arguments); | ||
this.$("[data-question-type]").each(function () { | ||
switch ($(this).data("questionType")) { | ||
case "star_rate": | ||
params[this.name] = this.value; | ||
break; | ||
} | ||
}); | ||
}, | ||
}); | ||
const survey_form = odoo.loader.modules.get("@survey/js/survey_form")[Symbol.for("default")]; | ||
|
||
survey_form.include({ | ||
events: $.extend({}, survey_form.prototype.events, { | ||
"click .rate > label": "_onClickFiveStarLabel", | ||
}), | ||
_onClickFiveStarLabel: function (event) { | ||
if (this.readonly) { | ||
return; | ||
} | ||
var target = event.target; | ||
var label_items = $(target).parent().find("label"); | ||
|
||
var value = label_items.length - $(target).index(); | ||
label_items.removeClass("checked fa-star").addClass("fa-star-o"); | ||
label_items.slice($(target).index()).addClass("checked fa-star").removeClass("fa-star-o"); | ||
var $input = $(target).parent().find("input"); | ||
$input.val(value); | ||
// We will trigger the change in order to make it compatible with conditional. | ||
// If it is not installed, it has no effects | ||
$input.trigger("change"); | ||
}, | ||
_prepareSubmitValues: function (formData, params) { | ||
this._super.apply(this, arguments); | ||
this.$("[data-question-type]").each(function () { | ||
switch ($(this).data("questionType")) { | ||
case "star_rate": | ||
params[this.name] = this.value; | ||
break; | ||
} | ||
}); | ||
}, | ||
}); | ||
}); |
Oops, something went wrong.