11# Copyright 2023 Le Filament (https://le-filament.com)
22# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
33
4+ from odoo .exceptions import UserError
45from odoo .http import request
56
67from odoo .addons .website_event_questions .controllers .main import WebsiteEvent
@@ -11,33 +12,85 @@ def _process_attendees_form(self, event, form_details):
1112 """Process data posted from the attendee details form.
1213 Extracts question answers:
1314 - For questions of type 'multiple_choice', extracting the suggested answer id"""
14- registrations = super (WebsiteEvent , self )._process_attendees_form (
15- event , form_details
15+ registrations = super ()._process_attendees_form (event , form_details )
16+
17+ # list all question_multi_answer with is_mandatory_answer
18+ mandatory_multiple_general_question_answer_count = {}
19+ mandatory_multiple_specific_question_ids = set ()
20+ req_filter = (
21+ lambda q : q .question_type == "multiple_choice" and q .is_mandatory_answer
1622 )
23+ for general_question in event .general_question_ids .filtered (req_filter ):
24+ mandatory_multiple_general_question_answer_count [general_question .id ] = 0
25+ for specific_question in event .specific_question_ids .filtered (req_filter ):
26+ mandatory_multiple_specific_question_ids .add (specific_question .id )
27+
28+ mandatory_multiple_specific_question_answer_count = [
29+ {i : 0 for i in mandatory_multiple_specific_question_ids }
30+ for j in range (len (registrations ))
31+ ]
1732
1833 general_answer_ids = []
1934 for key , _value in form_details .items ():
20- if "question_multi_answer" in key :
21- dummy , registration_index , question_answer = key .split ("-" )
22- question_id , answer_id = question_answer .split ("_" )
23- question_sudo = request .env ["event.question" ].browse (int (question_id ))
35+ # test html input prefix
36+ if key .startswith ("question_multi_answer" ):
37+ _html_input_prefix , registration_index_str , question_answer = key .split (
38+ "-"
39+ )
40+ registration_index = int (registration_index_str )
41+ question_id_str , answer_id = question_answer .split ("_" )
42+ question_id = int (question_id_str )
43+ question_sudo = request .env ["event.question" ].browse (question_id )
2444 answer_sudo = request .env ["event.question.answer" ].browse (
2545 int (answer_id )
2646 )
27- answer_values = None
28- if question_sudo .question_type == "multiple_choice" :
29- answer_values = {
30- "question_id" : int (question_id ),
31- "value_text_box" : answer_sudo .name ,
32- }
33-
34- if answer_values and not int (registration_index ):
47+ assert (
48+ question_sudo .question_type == "multiple_choice"
49+ ) # otherwise, html is malformed
50+ answer_values = {
51+ "question_id" : question_id ,
52+ "value_text_box" : answer_sudo .name ,
53+ }
54+ # question with null registration index are general
55+ if registration_index == 0 :
3556 general_answer_ids .append ((0 , 0 , answer_values ))
36- elif answer_values :
37- registrations [int (registration_index ) - 1 ][
38- "registration_answer_ids"
39- ].append ((0 , 0 , answer_values ))
57+ if question_sudo .is_mandatory_answer :
58+ mandatory_multiple_general_question_answer_count [
59+ question_id
60+ ] += 1
61+ # question with registration index are specific to one registration
62+ else :
63+ rindex = registration_index - 1 # zero-based array ¹indexing
64+ registrations [rindex ]["registration_answer_ids" ].append (
65+ (0 , 0 , answer_values )
66+ )
67+ if question_sudo .is_mandatory_answer :
68+ mandatory_multiple_specific_question_answer_count [rindex ][
69+ question_id
70+ ] += 1
71+
72+ # check that answers contain at least one for mandatory question
73+ # general
74+ for q , c in mandatory_multiple_general_question_answer_count .items ():
75+ if c == 0 :
76+ raise UserError (
77+ "Question "
78+ + str (q )
79+ + " is mandatory but did not receive an answer."
80+ )
81+ # specific
82+ for r , cc in enumerate (mandatory_multiple_specific_question_answer_count ):
83+ for q , c in cc .items ():
84+ if c == 0 :
85+ raise UserError (
86+ "Question "
87+ + str (q )
88+ + " is mandatory but did not receive an answer in ticket number "
89+ + str (r )
90+ + "."
91+ )
4092
93+ # append general question to all items
4194 for registration in registrations :
4295 registration ["registration_answer_ids" ].extend (general_answer_ids )
4396
0 commit comments