diff options
| author | Thibault Delavallée <tde@odoo.com> | 2019-11-25 12:26:57 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2019-12-05 15:21:28 +0000 |
| commit | 2592e72f2cd80f09fb23b4b16f8eb92ba253ff67 (patch) | |
| tree | ed6af2529339fc37003f28384aab8499bde85aa0 /addons/survey/models | |
| parent | 84bb9c748db26656d3ecd062183afe5f042dc569 (diff) | |
[REF] survey: globally rename survey.label model to survey.question.answer
PURPOSE
As new features are about to land in survey, notably live interactions [1]
and new survey building [2] performing a pre cleaning is necessary. In this
PR we clean survey models by: removing unnecessary fields, cleaning some code
and finally renaming models.
SPECIFICATIONS
In this commit we rename the long standing ``survey.label`` model. Indeed
a label is something before a question, like an input label. Labels in survey
are used for suggested answers and sometimes as rows for matrix answers.
After much thoughts we rename ``survey.label`` to ``survey.question.answer``.
It indicates this model holds answers. Moreover it is namespaced within the
``survey.question`` model name to avoid conflict with user input / user answer
model.
As model naming changes, some fields also evolve. In survey.question model
* ``labels_id`` is renamed to ``suggested_answer_ids`` to indicate it is
used to display suggested values to the user;
* ``labels_id_2`` is renamed to ``matrix_row_ids`` to indicate it is used
to generate the rows of the matrix-type question. A matrix is therefore
done using ``matrix_row_ids`` for rows and ``suggested_answer_ids`` for
columns which seems easier to understand;
In survey.question.answer (old survey.label) model
* ``question_id_2`` is renamed to ``matrix_question_id`` to ease its
understanding, notably that it is used for matrix questions;
LINKS
[0] Related to Task ID 2061901 (survey models cleaning and preparation)
[1] Task ID 1972640 (live interactions)
[2] Task ID 2119587 (new frontend for building surveys)
PR #40765
Diffstat (limited to 'addons/survey/models')
| -rw-r--r-- | addons/survey/models/survey_question.py | 35 | ||||
| -rw-r--r-- | addons/survey/models/survey_survey.py | 10 | ||||
| -rw-r--r-- | addons/survey/models/survey_user.py | 10 |
3 files changed, 31 insertions, 24 deletions
diff --git a/addons/survey/models/survey_question.py b/addons/survey/models/survey_question.py index dcfb8ef0126..f58f8347282 100644 --- a/addons/survey/models/survey_question.py +++ b/addons/survey/models/survey_question.py @@ -74,15 +74,15 @@ class SurveyQuestion(models.Model): ('multiple_choice', 'Multiple choice: multiple answers allowed'), ('matrix', 'Matrix')], string='Question Type') # -- simple choice / multiple choice / matrix - labels_ids = fields.One2many( - 'survey.label', 'question_id', string='Types of answers', copy=True, + suggested_answer_ids = fields.One2many( + 'survey.question.answer', 'question_id', string='Types of answers', copy=True, help='Labels used for proposed choices: simple choice, multiple choice and columns of matrix') # -- matrix matrix_subtype = fields.Selection([ ('simple', 'One choice per row'), ('multiple', 'Multiple choices per row')], string='Matrix Type', default='simple') - labels_ids_2 = fields.One2many( - 'survey.label', 'question_id_2', string='Rows of the Matrix', copy=True, + matrix_row_ids = fields.One2many( + 'survey.question.answer', 'matrix_question_id', string='Matrix Rows', copy=True, help='Labels used for proposed choices: rows of matrix') # -- display options column_nb = fields.Selection([ @@ -261,7 +261,7 @@ class SurveyQuestion(models.Model): def _validate_matrix(self, answers): # Validate that each line has been answered - if self.constr_mandatory and len(self.labels_ids_2) != len(answers): + if self.constr_mandatory and len(self.matrix_row_ids) != len(answers): return {self.id: self.constr_error_msg} return {} @@ -276,26 +276,33 @@ class SurveyQuestion(models.Model): def get_correct_answer_ids(self): self.ensure_one() - return self.labels_ids.filtered(lambda label: label.is_correct) + return self.suggested_answer_ids.filtered(lambda label: label.is_correct) -class SurveyLabel(models.Model): - """ A suggested answer for a question """ - _name = 'survey.label' +class SurveyQuestionAnswer(models.Model): + """ A preconfigured answer for a question. This model stores values used + for + + * simple choice, multiple choice: proposed values for the selection / + radio; + * matrix: row and column values; + + """ + _name = 'survey.question.answer' _rec_name = 'value' - _order = 'sequence,id' + _order = 'sequence, id' _description = 'Survey Label' question_id = fields.Many2one('survey.question', string='Question', ondelete='cascade') - question_id_2 = fields.Many2one('survey.question', string='Question 2', ondelete='cascade') + matrix_question_id = fields.Many2one('survey.question', string='Question (as matrix row)', ondelete='cascade') sequence = fields.Integer('Label Sequence order', default=10) value = fields.Char('Suggested value', translate=True, required=True) is_correct = fields.Boolean('Is a correct answer') answer_score = fields.Float('Score for this choice', help="A positive score indicates a correct choice; a negative or null score indicates a wrong answer") - @api.constrains('question_id', 'question_id_2') + @api.constrains('question_id', 'matrix_question_id') def _check_question_not_empty(self): - """Ensure that field question_id XOR field question_id_2 is not null""" + """Ensure that field question_id XOR field matrix_question_id is not null""" for label in self: - if not bool(label.question_id) != bool(label.question_id_2): + if not bool(label.question_id) != bool(label.matrix_question_id): raise ValidationError(_("A label must be attached to only one question.")) diff --git a/addons/survey/models/survey_survey.py b/addons/survey/models/survey_survey.py index fbd8be671cb..25a8b2df3e9 100644 --- a/addons/survey/models/survey_survey.py +++ b/addons/survey/models/survey_survey.py @@ -593,7 +593,7 @@ class Survey(models.Model): """ filter_display_data = [] if filters: - Label = self.env['survey.label'] + Label = self.env['survey.question.answer'] for current_filter in filters: row_id, answer_id = current_filter['row_id'], current_filter['answer_id'] label = Label.browse(answer_id) @@ -616,7 +616,7 @@ class Survey(models.Model): # Calculate and return statistics for choice if question.question_type in ['simple_choice', 'multiple_choice']: comments = [] - answers = OrderedDict((label.id, {'text': label.value, 'count': 0, 'answer_id': label.id, 'answer_score': label.answer_score}) for label in question.labels_ids) + answers = OrderedDict((label.id, {'text': label.value, 'count': 0, 'answer_id': label.id, 'answer_score': label.answer_score}) for label in question.suggested_answer_ids) for input_line in input_lines: if input_line.answer_type == 'suggestion' and answers.get(input_line.value_suggested.id) and (not(current_filters) or input_line.user_input_id.id in current_filters): answers[input_line.value_suggested.id]['count'] += 1 @@ -630,8 +630,8 @@ class Survey(models.Model): answers = OrderedDict() res = dict() comments = [] - [rows.update({label.id: label.value}) for label in question.labels_ids_2] - [answers.update({label.id: label.value}) for label in question.labels_ids] + [rows.update({label.id: label.value}) for label in question.matrix_row_ids] + [answers.update({label.id: label.value}) for label in question.suggested_answer_ids] for cell in product(rows, answers): res[cell] = 0 for input_line in input_lines: @@ -692,7 +692,7 @@ class Survey(models.Model): ) for question in scored_questions: - question_answer_correct = question.labels_ids.filtered(lambda answer: answer.is_correct) + question_answer_correct = question.suggested_answer_ids.filtered(lambda answer: answer.is_correct) for user_answer in user_answers: user_answer_lines_question = user_answer.user_input_line_ids.filtered(lambda line: line.question_id == question) user_answer_correct = user_answer_lines_question.filtered(lambda line: line.answer_is_correct and not line.skipped).mapped('value_suggested') diff --git a/addons/survey/models/survey_user.py b/addons/survey/models/survey_user.py index e535253605f..eb0a0127c12 100644 --- a/addons/survey/models/survey_user.py +++ b/addons/survey/models/survey_user.py @@ -54,7 +54,7 @@ class SurveyUserInput(models.Model): for user_input in self: total_possible_score = sum([ answer_score if answer_score > 0 else 0 - for answer_score in user_input.question_ids.mapped('labels_ids.answer_score') + for answer_score in user_input.question_ids.mapped('suggested_answer_ids.answer_score') ]) if total_possible_score == 0: @@ -196,8 +196,8 @@ class SurveyUserInputLine(models.Model): value_date = fields.Date('Date answer') value_datetime = fields.Datetime('Datetime answer') value_free_text = fields.Text('Free Text answer') - value_suggested = fields.Many2one('survey.label', string="Suggested answer") - value_suggested_row = fields.Many2one('survey.label', string="Row answer") + value_suggested = fields.Many2one('survey.question.answer', string="Suggested answer") + value_suggested_row = fields.Many2one('survey.question.answer', string="Row answer") answer_score = fields.Float('Score') answer_is_correct = fields.Boolean('Correct', compute='_compute_answer_is_correct') @@ -233,13 +233,13 @@ class SurveyUserInputLine(models.Model): for vals in vals_list: value_suggested = vals.get('value_suggested') if value_suggested: - vals.update({'answer_score': self.env['survey.label'].browse(int(value_suggested)).answer_score}) + vals.update({'answer_score': self.env['survey.question.answer'].browse(int(value_suggested)).answer_score}) return super(SurveyUserInputLine, self).create(vals_list) def write(self, vals): value_suggested = vals.get('value_suggested') if value_suggested: - vals.update({'answer_score': self.env['survey.label'].browse(int(value_suggested)).answer_score}) + vals.update({'answer_score': self.env['survey.question.answer'].browse(int(value_suggested)).answer_score}) return super(SurveyUserInputLine, self).write(vals) def _get_save_line_values(self, answer, answer_type): |
