summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAurélien Warnon <awa@odoo.com>2019-11-19 12:28:12 +0000
committerAurélien Warnon <awa@odoo.com>2020-03-31 10:39:05 +0000
commitf508a09640130845ee30a8717b0b2fff7b3c011c (patch)
treeea67c6db69b9cf94379bead6153d5d84ffbac59b
parenta1d6b6af5dfafab2a424a80c9bdfe9c655be2ead (diff)
[IMP] website_event_questions: improve question_ids copying from event.type
When applying an event.category on an event.event, we copy the event.questions from the event.type, but we don't immediately copy the answers associated to these questions. This was not done for technical reasons, we were limited by the JS framework. Since #46807 We now correctly handle nested o2m when triggering compute/onchange. This commit cleans the code by correctly copying the answers along with the questions when changing the event.type of an event.event. LINKS PR #45497 Task 2189529
-rw-r--r--addons/website_event_questions/models/event_event.py4
-rw-r--r--addons/website_event_questions/models/event_question.py12
-rw-r--r--addons/website_event_questions/views/event_views.xml29
3 files changed, 33 insertions, 12 deletions
diff --git a/addons/website_event_questions/models/event_event.py b/addons/website_event_questions/models/event_event.py
index bd2461a75cd..95d1d80a0f1 100644
--- a/addons/website_event_questions/models/event_event.py
+++ b/addons/website_event_questions/models/event_event.py
@@ -36,6 +36,10 @@ class EventEvent(models.Model):
'question_type': question.question_type,
'sequence': question.sequence,
'once_per_order': question.once_per_order,
+ 'answer_ids': [(0, 0, {
+ 'name': answer.name,
+ 'sequence': answer.sequence
+ }) for answer in question.answer_ids],
})
for question in event.event_type_id.question_ids
]
diff --git a/addons/website_event_questions/models/event_question.py b/addons/website_event_questions/models/event_question.py
index 4fbafa62eae..117cc0a2303 100644
--- a/addons/website_event_questions/models/event_question.py
+++ b/addons/website_event_questions/models/event_question.py
@@ -28,18 +28,6 @@ class EventQuestion(models.Model):
if any(question.event_type_id and question.event_id for question in self):
raise UserError(_('Question cannot belong to both the event category and itself.'))
- @api.model
- def create(self, vals):
- event_id = vals.get('event_id', False)
- if event_id:
- event = self.env['event.event'].browse([event_id])
- if event.event_type_id.use_questions and event.event_type_id.question_ids:
- vals['answer_ids'] = vals.get('answer_ids', []) + [(0, 0, {
- 'name': answer.name,
- 'sequence': answer.sequence,
- }) for answer in event.event_type_id.question_ids.filtered(lambda question: question.title == vals.get('title')).mapped('answer_ids')]
- return super(EventQuestion, self).create(vals)
-
def write(self, vals):
""" We add a check to prevent changing the question_type of a question that already has answers.
Indeed, it would mess up the event.registration.answer (answer type not matching the question type). """
diff --git a/addons/website_event_questions/views/event_views.xml b/addons/website_event_questions/views/event_views.xml
index 6f7814ba59e..5e3c8aa6bba 100644
--- a/addons/website_event_questions/views/event_views.xml
+++ b/addons/website_event_questions/views/event_views.xml
@@ -91,6 +91,35 @@
<field name="answer_ids" widget="many2many_tags"
attrs="{'invisible': [('question_type', '!=', 'simple_choice')]}" />
</tree>
+ <!-- Need to repeat the whole tree form here to be able to create answers properly
+ Without this, the sub-fields of answer_ids are unknown to the web framework.
+ We need this because we create questions and answers when the event type changes. -->
+ <form string="Question">
+ <sheet>
+ <h1><field name="title" /></h1>
+ <group>
+ <group>
+ <div colspan="2">
+ <field name="once_per_order"/>
+ <label for="once_per_order"/>
+ </div>
+ <field name="question_type" widget="radio" options="{'horizontal': true}" />
+ </group>
+ </group>
+ <notebook attrs="{'invisible': [('question_type', '!=', 'simple_choice')]}">
+ <page string="Answers" name="answers">
+ <field name="answer_ids">
+ <tree editable="bottom">
+ <!-- 'display_name' is necessary for the many2many_tags to work on the event view -->
+ <field name="display_name" invisible="1" />
+ <field name="sequence" widget="handle" />
+ <field name="name"/>
+ </tree>
+ </field>
+ </page>
+ </notebook>
+ </sheet>
+ </form>
</field>
</page>
</page>