From 9b98a4e0f216fce8e79ab07c8bf266f472053dd8 Mon Sep 17 00:00:00 2001 From: "Richard Mathot (OpenERP)" Date: Fri, 8 Nov 2013 15:24:06 +0100 Subject: [PATCH] [IMP] Mandatory questions validation bzr revid: rim@openerp.com-20131108142406-pmqubepahqg2qyqx --- addons/survey/__openerp__.py | 2 +- addons/survey/controllers/main.py | 147 ++++++++++++++++++----- addons/survey/survey_view.xml | 34 +++--- addons/survey/views/survey_templates.xml | 54 ++++----- 4 files changed, 162 insertions(+), 75 deletions(-) diff --git a/addons/survey/__openerp__.py b/addons/survey/__openerp__.py index f33fe5ec127..603f2ed142b 100644 --- a/addons/survey/__openerp__.py +++ b/addons/survey/__openerp__.py @@ -36,7 +36,7 @@ sent mails with user name and password for the invitation of the survey. 'summary': 'Create surveys, collect answers and print statistics', 'author': 'OpenERP SA', 'website': 'https://www.openerp.com/', - 'depends': ['email_template', 'mail'], + 'depends': ['email_template', 'mail', 'website'], 'data': [ 'security/survey_security.xml', 'security/ir.model.access.csv', diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index ebf4f2712ab..f9022dbef48 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -58,21 +58,41 @@ class WebsiteSurvey(http.Controller): if survey_obj.exists(cr, uid, survey.id, context=context) == []: return werkzeug.utils.redirect("/survey/") - _logger.debug('Post request data:\n %s', post) + _logger.debug('Post request data: %s', post) # Answer validation and storage problems = [] - if post: + if post and int(post['current']) != -1: for question in survey.page_ids[int(post['current'])].question_ids: - problems = problems + self.__check_constraints(survey.id, - post['current'], question, post) - problems = problems + self.__do_validation(survey.id, - post['current'], question, post) - _logger.debug('Problems in the survey:\n %s', problems) + # validation methods are dynamically loaded, if defined for the + # question type + # + # eg: if question type is 'new_question_type', this will try to + # load validate_new_question_type - # Pagination - pagination = {'current': -1, 'next': 0} + try: + checker = getattr(self, 'validate_' + question.type) + except AttributeError: + _logger.warning(question.type + ": This type of question has no validation method") + else: + problems = problems + checker(survey.id, post['current'], question, post) + + # Return the same survey page if problems occur + if problems: + _logger.debug('Problems in the survey: %s', problems) + pagination = {'current': int(post['current']), 'next': int(post['next'])} + return request.website.render('survey.survey', + {'survey': survey, + 'pagination': pagination, + 'problems': problems}) + + # Display success message if totally succeeded + if post and post['next'] == "finished": + return request.website.render('survey.finished') + + # Page selection + pagination = {'current': -1, 'next': 0} # Default pagination if first opening if 'current' in post and 'next' in post and post['next'] != "finished": oldnext = int(post['next']) if oldnext not in range(0, len(survey.page_ids)): @@ -84,32 +104,103 @@ class WebsiteSurvey(http.Controller): else: pagination['next'] = oldnext + 1 - # Display success message if totally succeeded - if post and post['next'] == "finished": - return request.website.render('survey.finished') - return request.website.render('survey.survey', {'survey': survey, 'pagination': pagination, - 'problems': problems}) + 'problems': None}) - @website.route(["/survey/print//"], + @website.route(['/survey/print//'], type='http', auth='public', multilang=True) - def print_empty_survey(self, survey_id=None, **post): + def print_empty_survey(self, survey=None, **post): '''Display an empty survey in printable view''' - cr, uid, context = request.cr, request.uid, request.context - survey_obj = request.registry['survey.survey'] - survey = survey_obj.browse(cr, uid, survey_id, context=context) + pagination = {'current': -1, 'next': 0} return request.website.render('survey.survey_print', - {'survey': survey}) + {'survey': survey, + 'pagination': pagination}) - def __check_constraints(self, survey_id, page_nr, question, post): - print(survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__()) + def validate_free_text(self, survey_id, page_nr, question, post): + answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + problems = [] if question.constr_mandatory: - if (survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__()) not in post: - return [{'qlabel': question.question, - 'errmsg': "This question is mandatory"}] - return [] + problems = problems + self.__has_empty_input(question, post, answer_tag) + return problems - def __do_validation(self, survey_id, page_nr, question, post): - return [] + def validate_textbox(self, survey_id, page_nr, question, post): + answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + problems = [] + if question.constr_mandatory: + problems = problems + self.__has_empty_input(question, post, answer_tag) + return problems + + def validate_numerical_box(self, survey_id, page_nr, question, post): + answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + # Check for an empty input + problems = [] + if question.constr_mandatory: + problems = problems + self.__has_empty_input(question, post, answer_tag) + # Check for a non number input + if answer_tag in post and post[answer_tag].strip() != "": + try: + float(post[answer_tag].strip()) + except ValueError: + problems = problems + [{'qlabel': question.question, + 'errmsg': "This is not a number"}] + + return problems + + def validate_datetime(self, survey_id, page_nr, question, post): + answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + problems = [] + if question.constr_mandatory: + problems = problems + self.__has_empty_input(question, post, answer_tag) + + # TODO CHECK, check if this is a date + + return problems + + def validate_simple_choice_scale(self, survey_id, page_nr, question, post): + answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + problems = [] + if question.constr_mandatory: + problems = problems + self.__has_empty_input(question, post, answer_tag) + return problems + + def validate_simple_choice_dropdown(self, survey_id, page_nr, question, post): + answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + problems = [] + if question.constr_mandatory: + problems = problems + self.__has_empty_input(question, post, answer_tag) + return problems + + # def validate_multiple_choice(self, survey_id, page_nr, question, post): + # answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + # problems = [] + # return problems + + # def validate_vector(self, survey_id, page_nr, question, post): + # answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + # problems = [] + # return problems + + # def validate_matrix(self, survey_id, page_nr, question, post): + # answer_tag = survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__() + # problems = [] + # return problems + + def __has_empty_input(self, question, post, answer_tag): + ''' Check for an empty input ''' + print(answer_tag) + if answer_tag not in post or post[answer_tag].strip() == "": + return [{'qlabel': question.question, + 'errmsg': "This question is mandatory"}] + else: + return [] + + +def __dict_keys_startswith(self, dictionary, string): + '''Returns a dictionary containing the elements of whose keys start + with . + + .. note:: + This function uses dictionary comprehensions (Python >= 2.7)''' + return {k: dictionary[k] for k in filter(lambda key: key.startswith(string), dictionary.keys())} diff --git a/addons/survey/survey_view.xml b/addons/survey/survey_view.xml index 8d8d6d05070..12b6574f92e 100644 --- a/addons/survey/survey_view.xml +++ b/addons/survey/survey_view.xml @@ -426,6 +426,21 @@ + + + + + + + + + + + + + + + @@ -444,29 +459,16 @@ - + - - - - - - - - - - - - - - + diff --git a/addons/survey/views/survey_templates.xml b/addons/survey/views/survey_templates.xml index ed4a36815ad..c448c7041d3 100644 --- a/addons/survey/views/survey_templates.xml +++ b/addons/survey/views/survey_templates.xml @@ -12,9 +12,9 @@ TitleCategoryDescriptionPublic URL - - - + + + @@ -45,16 +45,14 @@
-
-
-
+

- +
- +

@@ -69,7 +67,11 @@ @@ -78,8 +80,6 @@
-
-
@@ -99,8 +99,8 @@ @@ -263,10 +257,10 @@

- +
- +