diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index b3a2592b013..ebf4f2712ab 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -26,7 +26,10 @@ from openerp.addons.website.models import website # from openerp.tools.safe_eval import safe_eval # import simplejson -# import werkzeug +import werkzeug +import logging + +_logger = logging.getLogger(__name__) class WebsiteSurvey(http.Controller): @@ -35,48 +38,78 @@ class WebsiteSurvey(http.Controller): '/survey/list/'], type='http', auth='public', multilang=True) def list_surveys(self, **post): - '''All the public surveys''' + '''Lists all the public surveys''' cr, uid, context = request.cr, request.uid, request.context survey_obj = request.registry['survey.survey'] - survey_ids = survey_obj.search(cr, uid, [('state', '=', 'open')], + survey_ids = survey_obj.search(cr, uid, [('state', '=', 'open'), + ('page_ids', '!=', 'None')], context=context) surveys = survey_obj.browse(cr, uid, survey_ids, context=context) return request.website.render('survey.list', {'surveys': surveys}) - @website.route(["/survey/fill//", - "/survey/fill//page/", - "/survey/fill//page//"], + @website.route(['/survey/fill//'], type='http', auth='public', multilang=True) - def fill_survey(self, survey_id=None, page_index=None, **post): - '''Display a survey''' + def fill_survey(self, survey=None, **post): + '''Display and validates a survey''' 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} - if page_index is not None: - if page_index not in range(0, len(survey.page_ids)): + # In case of bad survey, redirect to surveys list + if survey_obj.exists(cr, uid, survey.id, context=context) == []: + return werkzeug.utils.redirect("/survey/") + + _logger.debug('Post request data:\n %s', post) + + # Answer validation and storage + problems = [] + if post: + 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) + + # Pagination + pagination = {'current': -1, 'next': 0} + 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)): raise Exception("This page does not exist") - pagination['current'] = page_index - if page_index == len(survey.page_ids) - 1: - pagination['next'] = -1 else: - pagination['next'] = page_index + 1 + pagination['current'] = oldnext + if oldnext == len(survey.page_ids) - 1: + pagination['next'] = 'finished' + 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, - 'debug': False, - 'validation_error': None}) + 'problems': problems}) @website.route(["/survey/print//"], type='http', auth='public', multilang=True) def print_empty_survey(self, survey_id=None, **post): - '''Display a survey''' + '''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) - return request.website.render('survey.survey_print', {'survey': survey}) + + def __check_constraints(self, survey_id, page_nr, question, post): + print(survey_id.__str__() + '*' + page_nr + '*' + question.id.__str__()) + 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 [] + + def __do_validation(self, survey_id, page_nr, question, post): + return [] diff --git a/addons/survey/survey.py b/addons/survey/survey.py index 478809e428a..3a8a333ed74 100644 --- a/addons/survey/survey.py +++ b/addons/survey/survey.py @@ -368,7 +368,7 @@ class survey_question(osv.osv): ('has_length', 'Must have a specific length'), ('is_integer', 'Must be an integer'), ('is_decimal', 'Must be a decimal number'), - ('is_date', 'Must be a date'), + #('is_date', 'Must be a date'), ('is_email', 'Must be an email address') ], 'Validation type'), 'validation_length': fields.integer('Specific length'), @@ -376,8 +376,8 @@ class survey_question(osv.osv): 'validation_max_float_value': fields.float('Maximum value'), 'validation_min_int_value': fields.integer('Minimum value'), 'validation_max_int_value': fields.integer('Maximum value'), - 'validation_min_date': fields.date('Start date range'), - 'validation_max_date': fields.date('End date range'), + #'validation_min_date': fields.date('Start date range'), + #'validation_max_date': fields.date('End date range'), 'validation_error_msg': fields.char("Error message if validation \ fails", oldname='validation_valid_err_msg'), diff --git a/addons/survey/views/survey_templates.xml b/addons/survey/views/survey_templates.xml index edb2d4f6fbd..ed4a36815ad 100644 --- a/addons/survey/views/survey_templates.xml +++ b/addons/survey/views/survey_templates.xml @@ -25,6 +25,20 @@ + + + + @@ -118,32 +144,32 @@

-
+

- + - + - + - + - - + + TODO: replace html5 datetime widget by OpenERP-CMS datetime widget @@ -151,7 +177,7 @@
@@ -159,7 +185,7 @@
- @@ -170,7 +196,7 @@