From dea7a84bd384cae13f4671ad4a424c189b3b2f2a Mon Sep 17 00:00:00 2001 From: clementthomas Date: Mon, 6 Nov 2023 17:11:32 +0100 Subject: [PATCH] [IMP] survey_event_registration_generation: answer filter on survey question "event" --- .../controllers/main.py | 46 +++++++++------- .../models/survey_question.py | 12 +++++ .../views/survey_question_views.xml | 5 +- .../views/survey_templates.xml | 52 +++++++++---------- 4 files changed, 67 insertions(+), 48 deletions(-) diff --git a/survey_event_registration_generation/controllers/main.py b/survey_event_registration_generation/controllers/main.py index f065aba..d827759 100644 --- a/survey_event_registration_generation/controllers/main.py +++ b/survey_event_registration_generation/controllers/main.py @@ -18,16 +18,35 @@ class Survey(main.Survey): return [{'id':event.id,'name':event.name} for event in events] - def _prepare_survey_data(self, survey_sudo, answer_sudo, **post): + def _prepare_survey_data(self, survey_sudo, answer_sudo, **post): result = super(Survey, self)._prepare_survey_data(survey_sudo, answer_sudo, **post) - result['event_products'] = request.env['product.product'].sudo().search([('detailed_type','=','event')]) - next_event_question = self._get_next_event_question(answer_sudo) - if next_event_question: - event_products_ids = None - if next_event_question.event_product_question_id: - event_products_ids = self._get_answer_event_product(next_event_question.event_product_question_id, answer_sudo).id - result['events'] = request.env['event.event'].sudo().get_events_from_event_products(event_products_ids) + result['event_products'] = {} + result['events'] = {} + + for question in answer_sudo.predefined_question_ids: + if question.question_type == 'event_product': + # set event products answers (by question) + if question.show_events_and_event_products_only_visible_in_survey: + result['event_products'][question.id] = request.env['product.product'].sudo().search([('detailed_type','=','event'),('visible_in_survey','=',True)]) + else: + result['event_products'][question.id] = request.env['product.product'].sudo().search([('detailed_type','=','event')]) + + if question.question_type == 'event': + + # set events answers (by question) + if question.event_answer_filter == 'all': + if question.show_events_and_event_products_only_visible_in_survey: + result['events'][question.id] = request.env['event.event'].sudo().search([('visible_in_survey','=',True)]) + else: + result['events'][question.id] = request.env['event.event'].sudo().search([]) + else: + if question.event_answer_filter == 'event_product': + event_products_ids = [question.event_filter_event_product_id.id] + elif question.event_answer_filter == 'event_product_question': + event_products_ids = self._get_answer_event_product(question.event_product_question_id, answer_sudo).id + result['events'][question.id] = request.env['event.event'].sudo().get_events_from_event_products(event_products_ids, + only_visible_in_survey=question.show_events_and_event_products_only_visible_in_survey) return result @@ -50,14 +69,3 @@ class Survey(main.Survey): if question.question_type == 'multiple_event_products': return user_input_line.value_multiple_event_products - - def _get_next_event_question(self, answer_sudo): - future_question = False - for question in answer_sudo.predefined_question_ids: - if question == answer_sudo.last_displayed_page_id: - future_question = True - continue - if not future_question: - continue - if question.question_type == 'event': - return question diff --git a/survey_event_registration_generation/models/survey_question.py b/survey_event_registration_generation/models/survey_question.py index 2fb0a01..53b8e8b 100644 --- a/survey_event_registration_generation/models/survey_question.py +++ b/survey_event_registration_generation/models/survey_question.py @@ -15,6 +15,11 @@ class SurveyQuestion(models.Model): If question display events, they are filtered with only events in step 'Visible in survey'. If question display event products, they are filtered with only products of events in step 'Visible in survey'.""") + event_answer_filter = fields.Selection( + [('all','All events'), ('event_product_question','By event product question'), ('event_product', 'By fixed event product')], + string="Filter event answer by.. ", default='all') + + event_filter_event_product_id = fields.Many2one('product.product', string="Event product", domain="[('detailed_type','=','event')]") event_product_question_id = fields.Many2one( 'survey.question', string="Event product question", copy=False, compute="_compute_event_product_question_id", @@ -73,3 +78,10 @@ class SurveyQuestion(models.Model): if not question.question_type == 'event' or question.event_product_question_id is None: question.event_product_question_id = False + + @api.onchange('event_answer_filter') + def onchange_event_answer_filter(self): + if self.event_answer_filter in ('all', 'event_product_question'): + self.event_filter_event_product_id = False + if self.event_answer_filter in ('all', 'event_product'): + self.event_product_question_id = False diff --git a/survey_event_registration_generation/views/survey_question_views.xml b/survey_event_registration_generation/views/survey_question_views.xml index 1bedb2d..99e599d 100644 --- a/survey_event_registration_generation/views/survey_question_views.xml +++ b/survey_event_registration_generation/views/survey_question_views.xml @@ -18,9 +18,12 @@ attrs="{'invisible': [('question_type', 'not in', ['event_product', 'event', 'multiple_event_products'])]}" /> + + diff --git a/survey_event_registration_generation/views/survey_templates.xml b/survey_event_registration_generation/views/survey_templates.xml index 8c0ad54..3272e50 100644 --- a/survey_event_registration_generation/views/survey_templates.xml +++ b/survey_event_registration_generation/views/survey_templates.xml @@ -22,13 +22,11 @@ t-att-placeholder="question.question_placeholder" t-att-data-question-type="question.question_type"> - - - - + + @@ -42,19 +40,19 @@
- - - - + +
@@ -67,19 +65,17 @@