Files
odoo_source/addons/website_event_questions/models/event_event.py
T
Pierre-Yves Dufays 2faefd4757 [IMP] website_event[_questions]: allow setting event questions as mandatory
Purpose:

Allow users to make sure they will get an answer to the questions they ask
attendees while they register.
e.g.:
- You organize activities for kids and want to make sure you have a second
phone number available in case of emergencies
- You organize B2B events and need to know in which industry your attendees
work
- You use the Event/CRM bridge and plan to filter attendees based on those
answers
...

Specifications:

In the question form view, a boolean "Mandatory Answer" has been added.

In the list view, that field has been added as column.

In the registration form:
- "*", next to each mandatory question has been added
- "*" has been added next to name and email which were already mandatory
- the mandatory question have been actually made mandatory by preventing
user to submit empty answer

Remark: once_per_order was missing in the list view of the event template
questions. We have added it as well as "mandatory answer" field. The
question list view of the template and the event are now almost identical
(the button to the stat is obviously not present in the template).

Technical remarks:
- To ensures that multiple choice answer are filled by the users, we have
added an empty choice selected by default which forces the user to choose
one option.
- Empty answer were already filtered by the server and not inserted in the
database (in controller method _process_attendees_form, value evaluated
to false are not inserted)

Task-2745835

closes odoo/odoo#83586

Signed-off-by: Warnon Aurélien (awa) <awa@odoo.com>
2022-03-02 10:00:50 +00:00

70 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class EventType(models.Model):
_inherit = 'event.type'
question_ids = fields.One2many(
'event.question', 'event_type_id',
string='Questions', copy=True)
class EventEvent(models.Model):
""" Override Event model to add optional questions when buying tickets. """
_inherit = 'event.event'
question_ids = fields.One2many(
'event.question', 'event_id', 'Questions', copy=True,
compute='_compute_question_ids', readonly=False, store=True)
general_question_ids = fields.One2many('event.question', 'event_id', 'General Questions',
domain=[('once_per_order', '=', True)])
specific_question_ids = fields.One2many('event.question', 'event_id', 'Specific Questions',
domain=[('once_per_order', '=', False)])
@api.depends('event_type_id')
def _compute_question_ids(self):
""" Update event questions from its event type. Depends are set only on
event_type_id itself to emulate an onchange. Changing event type content
itself should not trigger this method.
When synchronizing questions:
* lines that no answer are removed;
* type lines are added;
"""
if self._origin.question_ids:
# lines to keep: those with already given answers
questions_tokeep_ids = self.env['event.registration.answer'].search(
[('question_id', 'in', self._origin.question_ids.ids)]
).question_id.ids
else:
questions_tokeep_ids = []
for event in self:
if not event.event_type_id and not event.question_ids:
event.question_ids = False
continue
if questions_tokeep_ids:
questions_toremove = event._origin.question_ids.filtered(lambda question: question.id not in questions_tokeep_ids)
command = [(3, question.id) for question in questions_toremove]
else:
command = [(5, 0)]
if event.event_type_id.question_ids:
command += [
(0, 0, {
'title': question.title,
'question_type': question.question_type,
'sequence': question.sequence,
'once_per_order': question.once_per_order,
'is_mandatory_answer': question.is_mandatory_answer,
'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
]
event.question_ids = command