From 0c6ce6cf44dc7d1041c39a3f899474803845ee57 Mon Sep 17 00:00:00 2001 From: aaj-odoo Date: Fri, 11 Jun 2021 05:26:35 +0000 Subject: [PATCH] [IMP] website_event_track_quiz: improve ux for quizzes on tracks Right now, if we want to add a quiz to the event track, we need to create it from the m2o field available in the form view. But it is not very intuitive as user will have to select 'Create and Edit' option from the m2o drop-down. Quick create is not really helpful here. Also, trying to add question for the quiz will pile up the modals which looks messy. With this commit, in the event track's form view, we: - Remove the m2o for `quiz_id` (from the track form view, not from model) - Introduce "Add Quiz" header button, which redirects user to form view of the quiz, with no option to create new quiz directly as only one quiz should be linked to quiz. Also, once the quiz is added, this button will be hidden. - Introduce new stat button "Go to Quiz", which redirects user to the form view of quiz (again, does not allow creation from the same reason). This button is visible only if the quiz is already added for the track. Apart from that, re-arrage the quiz form view `event_quiz_view_form` by putting 'Event' and 'Event Track' fields on the same line, and we utilize the same view while creating / updating quiz from the track. Note: Since the creation of quiz is now done in its own form view (with action button "Add Quiz" from track's form view), we are passing the default `event_track_id` while creating the quiz. It means that we don't need to compute it anymore. Instead, now we need to compute the `quiz_id` on the track, based on the `event_track_id` field alreay set on the quiz. So, we introduce new One2Many `quiz_ids` field to compute the `quiz_id` on the track, and we remove `event_track_ids` from the quiz. Basically, the logic of setting `quiz_id` (on the track) and `event_track_id` (on the quiz) is inverted with the commit, and the demo data is updated accordinlgy. TaskID-2486552 closes odoo/odoo#70067 Related: odoo/upgrade#2522 Signed-off-by: Thibault Delavallee (tde) --- .../data/quiz_demo.xml | 14 ++------ .../models/event_quiz.py | 10 +----- .../models/event_track.py | 36 ++++++++++++++++++- .../views/event_quiz_views.xml | 21 ++++------- .../views/event_track_views.xml | 19 ++++++---- 5 files changed, 59 insertions(+), 41 deletions(-) diff --git a/addons/website_event_track_quiz/data/quiz_demo.xml b/addons/website_event_track_quiz/data/quiz_demo.xml index ea0d73af800..3ac5bf4ded0 100644 --- a/addons/website_event_track_quiz/data/quiz_demo.xml +++ b/addons/website_event_track_quiz/data/quiz_demo.xml @@ -3,6 +3,7 @@ What This Event Is All About + @@ -48,12 +49,9 @@ - - - - Securing your Lumber during transport + @@ -99,12 +97,9 @@ - - - - Pretty. Ugly. Lovely. + @@ -132,9 +127,6 @@ - - - diff --git a/addons/website_event_track_quiz/models/event_quiz.py b/addons/website_event_track_quiz/models/event_quiz.py index 8ba65f703e7..da2f08a814f 100644 --- a/addons/website_event_track_quiz/models/event_quiz.py +++ b/addons/website_event_track_quiz/models/event_quiz.py @@ -11,19 +11,11 @@ class Quiz(models.Model): name = fields.Char('Name', required=True, translate=True) question_ids = fields.One2many('event.quiz.question', 'quiz_id', string="Questions") - event_track_ids = fields.One2many('event.track', 'quiz_id', string="Tracks") - event_track_id = fields.Many2one( - 'event.track', compute='_compute_event_track_id', - readonly=True, store=True) + event_track_id = fields.Many2one('event.track', readonly=True) event_id = fields.Many2one( 'event.event', related='event_track_id.event_id', readonly=True, store=True) - @api.depends('event_track_ids.quiz_id') - def _compute_event_track_id(self): - for quiz in self: - quiz.event_track_id = quiz.event_track_ids[0] if quiz.event_track_ids else False - class QuizQuestion(models.Model): _name = "event.quiz.question" diff --git a/addons/website_event_track_quiz/models/event_track.py b/addons/website_event_track_quiz/models/event_track.py index 3011b98a309..5f73d38586d 100644 --- a/addons/website_event_track_quiz/models/event_track.py +++ b/addons/website_event_track_quiz/models/event_track.py @@ -8,11 +8,17 @@ from odoo.osv import expression class EventTrack(models.Model): _inherit = ['event.track'] - quiz_id = fields.Many2one('event.quiz', string="Quiz", groups="event.group_event_user") + quiz_id = fields.Many2one('event.quiz', string="Quiz", compute='_compute_quiz_id', store=True, groups="event.group_event_user") + quiz_ids = fields.One2many('event.quiz', 'event_track_id', string="Quizzes") quiz_questions_count = fields.Integer(string="# Quiz Questions", compute='_compute_quiz_questions_count', groups="event.group_event_user") is_quiz_completed = fields.Boolean('Is Quiz Done', compute='_compute_quiz_data') quiz_points = fields.Integer('Quiz Points', compute='_compute_quiz_data') + @api.depends('quiz_ids.event_track_id') + def _compute_quiz_id(self): + for track in self: + track.quiz_id = track.quiz_ids[0] if track.quiz_ids else False + @api.depends('quiz_id.question_ids') def _compute_quiz_questions_count(self): for track in self: @@ -64,3 +70,31 @@ class EventTrack(models.Model): else: track.is_quiz_completed = False track.quiz_points = 0 + + def action_add_quiz(self): + self.ensure_one() + event_quiz_form = self.env.ref('website_event_track_quiz.event_quiz_view_form') + return { + 'type': 'ir.actions.act_window', + 'view_mode': 'form', + 'res_model': 'event.quiz', + 'view_id': event_quiz_form.id, + 'context': { + 'default_event_track_id': self.id, + 'create': False, + }, + } + + def action_view_quiz(self): + self.ensure_one() + event_quiz_form = self.env.ref('website_event_track_quiz.event_quiz_view_form') + return { + 'type': 'ir.actions.act_window', + 'view_mode': 'form', + 'res_model': 'event.quiz', + 'res_id' : self.quiz_id.id, + 'view_id': event_quiz_form.id, + 'context': { + 'create': False, + } + } diff --git a/addons/website_event_track_quiz/views/event_quiz_views.xml b/addons/website_event_track_quiz/views/event_quiz_views.xml index a7a528a55d6..abbd1e0f51b 100644 --- a/addons/website_event_track_quiz/views/event_quiz_views.xml +++ b/addons/website_event_track_quiz/views/event_quiz_views.xml @@ -39,10 +39,14 @@ placeholder="e.g. Test your Knowledge"/> - - + + + + + + - + - event.quiz.view.form - event.quiz - - primary - - - - - - Event Quizzes event.quiz diff --git a/addons/website_event_track_quiz/views/event_track_views.xml b/addons/website_event_track_quiz/views/event_track_views.xml index 13a0ceac999..9d65b981686 100644 --- a/addons/website_event_track_quiz/views/event_track_views.xml +++ b/addons/website_event_track_quiz/views/event_track_views.xml @@ -5,12 +5,19 @@ event.track - - - - - + + + + + + +