Skip to content

Commit

Permalink
[REF] survey : make survey form a widget
Browse files Browse the repository at this point in the history
Purpose of this commit is
- to make the survey js controller a widget, to be more 'odoo-standard'.
- to lessen number of RPCs and clean old code for prefilling and validation of
surveys.

Clean Prefill :
Prefill can be done directly in the template as the template has already
all the needed values (in answer object).

Clean Validation :
Validation is done at server side and is independant from prefilling values.

Dates:
Dates are now formatted directly in the template, at rendering, using a
format_date fonction pointer called in the template.

Don't use widget in review mode:
Remove o_survey_form class from review template as only dates were processed in
the widget for review template.

Breadcrumb :
Remove button previous, prev=prev and go_back mechanism :
The previous page is handled by the breadcrump
Remove redirect url mechanism.
Breadcrumb now saves the answers when going to a previous page.
Move o_survey_form class to a higher div to englobe breadcrumb in the widget
and ease his handling.

Remove locale load as already done in session.js#load_modules

This commit modifies the route type of survey submit to work in json.
The js survey form controller calls now manually the route via rpc.

Task ID : 1930132
PR #32419
  • Loading branch information
dbeguin committed Nov 19, 2019
1 parent 52a6b00 commit b1e3297
Show file tree
Hide file tree
Showing 8 changed files with 569 additions and 516 deletions.
241 changes: 74 additions & 167 deletions addons/survey/controllers/main.py

Large diffs are not rendered by default.

83 changes: 58 additions & 25 deletions addons/survey/models/survey_survey.py
Expand Up @@ -348,19 +348,42 @@ def _get_number_of_attempts_lefts(self, partner, email, invite_token):
# ------------------------------------------------------------
# ACTIONS
# ------------------------------------------------------------
@api.model
def _get_pages_or_questions(self, user_input):
if self.questions_layout == 'one_page':
return None
elif self.questions_layout == 'page_per_question' and self.questions_selection == 'random':
return list(enumerate(
user_input.question_ids
))
else:
return list(enumerate(
self.question_ids if self.questions_layout == 'page_per_question' else self.page_ids
))

@api.model
def _previous_page_or_question_id(self, user_input, page_or_question_id):
survey = user_input.survey_id
pages_or_questions = survey._get_pages_or_questions(user_input)

current_page_index = pages_or_questions.index(next(p for p in pages_or_questions if p[1].id == page_or_question_id))
# is first page
if current_page_index == 0:
return None

previous_page_id = pages_or_questions[current_page_index - 1][1].id

return previous_page_id


@api.model
def next_page_or_question(self, user_input, page_or_question_id, go_back=False):
def next_page_or_question(self, user_input, page_or_question_id):
""" The next page to display to the user, knowing that page_id is the id
of the last displayed page.
If page_id == 0, it will always return the first page of the survey.
If all the pages have been displayed and go_back == False, it will
return None
If go_back == True, it will return the *previous* page instead of the
next page.
If all the pages have been displayed, it will return None
.. note::
It is assumed here that a careful user will not try to set go_back
Expand All @@ -369,16 +392,9 @@ def next_page_or_question(self, user_input, page_or_question_id, go_back=False):
"""
survey = user_input.survey_id

if survey.questions_layout == 'one_page':
pages_or_questions = survey._get_pages_or_questions(user_input)
if not pages_or_questions:
return (None, False)
elif survey.questions_layout == 'page_per_question' and survey.questions_selection == 'random':
pages_or_questions = list(enumerate(
user_input.question_ids
))
else:
pages_or_questions = list(enumerate(
survey.question_ids if survey.questions_layout == 'page_per_question' else survey.page_ids
))

# First page
if page_or_question_id == 0:
Expand All @@ -387,18 +403,35 @@ def next_page_or_question(self, user_input, page_or_question_id, go_back=False):
current_page_index = pages_or_questions.index(next(p for p in pages_or_questions if p[1].id == page_or_question_id))

# All the pages have been displayed
if current_page_index == len(pages_or_questions) - 1 and not go_back:
if current_page_index == len(pages_or_questions) - 1:
return (None, False)
# Let's get back, baby!
elif go_back and survey.users_can_go_back:
return (pages_or_questions[current_page_index - 1][1], False)
else:
# This will show the last page
if current_page_index == len(pages_or_questions) - 2:
return (pages_or_questions[current_page_index + 1][1], True)
# This will show a regular page
else:
return (pages_or_questions[current_page_index + 1][1], False)
show_last = current_page_index == len(pages_or_questions) - 2
return (pages_or_questions[current_page_index + 1][1], show_last)

def _get_survey_questions(self, answer=None, page_id=None, question_id=None):
questions, page_or_question_id = None, None

if self.questions_layout == 'page_per_section':
if not page_id:
raise ValueError("Page id is needed for question layout 'page_per_section'")
page_id = int(page_id)
questions = self.env['survey.question'].sudo().search([('survey_id', '=', self.id), ('page_id', '=', page_id)])
page_or_question_id = page_id
elif self.questions_layout == 'page_per_question':
if not question_id:
raise ValueError("Question id is needed for question layout 'page_per_question'")
question_id = int(question_id)
questions = self.env['survey.question'].sudo().browse(question_id)
page_or_question_id = question_id
else:
questions = self.question_ids

# we need the intersection of the questions of this page AND the questions prepared for that user_input
# (because randomized surveys do not use all the questions of every page)
if answer:
questions = questions & answer.question_ids
return questions, page_or_question_id

def action_draft(self):
self.write({'state': 'draft'})
Expand Down
244 changes: 0 additions & 244 deletions addons/survey/static/src/js/survey.js

This file was deleted.

0 comments on commit b1e3297

Please sign in to comment.