Files
odoo_source/addons/website_event_questions/controllers/main.py
T
Aurélien Warnon 7b79c3244d [IMP] website_event_questions: allow 'free text' questions
PURPOSE

Allow "free text" questions in the event questions asked to attendees, in
addition to the existing "selection" type.
e.g: "What are your allergies?"

SPECS

The implementation is heavily based on the modelisation we chose for the survey
module since the features become similar in terms of question types and inputs.

- Introduce a 'question_type' that is either 'simple_choice'
  (= previous behavior) or 'text_box' that will contain the attendee text
  answer.
- Rename the event.answer model to event.question.answer to make it more
  obvious that it contains answer "suggestions" to a question of type
  'simple_choice'.
- Introduce a 'event.user_input.line' that is an answer to a specific question
  for a specific registration.
  It works very similarly to 'survey.user_input.line' but it's not contained in
  a 'user_input' model, it's directly linked to the registration using a
  'user_input_line_ids' o2m field.

The frontend view has been adapted accordingly to display either a selection
input or a textarea based on the question type.
The frontend orm processing now takes in both answer types (id of suggested
answer or text input).

On the backend side, views have been adapted to display event questions and
attendee answers in a more convenient and logical manner.

LINKS

PR #45497
Upgrade PR odoo/upgrade#799
Task ID 2189529
2020-03-31 10:39:05 +00:00

48 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.http import request
from odoo.addons.website_event.controllers.main import WebsiteEventController
class WebsiteEvent(WebsiteEventController):
def _process_attendees_form(self, event, form_details):
""" Process data posted from the attendee details form.
Extracts question answers:
- For both questions asked 'once_per_order' and questions asked to every attendee
- For questions of type 'simple_choice', extracting the suggested answer id
- For questions of type 'text_box', extracting the text answer of the attendee. """
registrations = super(WebsiteEvent, self)._process_attendees_form(event, form_details)
for registration in registrations:
registration['registration_answer_ids'] = []
general_answer_ids = []
for key, value in form_details.items():
if 'question_answer' in key and value:
dummy, registration_index, question_id = key.split('-')
question = request.env['event.question'].browse(int(question_id))
answer_values = None
if question.question_type == 'simple_choice':
answer_values = {
'question_id': int(question_id),
'value_answer_id': int(value)
}
elif question.question_type == 'text_box':
answer_values = {
'question_id': int(question_id),
'value_text_box': value
}
if answer_values and not int(registration_index):
general_answer_ids.append((0, 0, answer_values))
elif answer_values:
registrations[int(registration_index) - 1]['registration_answer_ids'].append((0, 0, answer_values))
for registration in registrations:
registration['registration_answer_ids'].extend(general_answer_ids)
return registrations