diff --git a/addons/survey/controllers/main.py b/addons/survey/controllers/main.py index d9166aad4c4..8988b12ae1f 100644 --- a/addons/survey/controllers/main.py +++ b/addons/survey/controllers/main.py @@ -49,10 +49,69 @@ class WebsiteSurvey(http.Controller): surveys = survey_obj.browse(cr, uid, survey_ids, context=context) return request.website.render('survey.list', {'surveys': surveys}) + # Survey start + @website.route(['/survey/start/', + '/survey/start//'], + type='http', auth='public', multilang=True) + def start_survey(self, survey, token=None, **post): + cr, uid, context = request.cr, request.uid, request.context + survey_obj = request.registry['survey.survey'] + user_input_obj = request.registry['survey.user_input'] + + # In case of bad survey, redirect to surveys list + if survey_obj.exists(cr, uid, survey.id, context=context) == []: + return werkzeug.utils.redirect("/survey/") + + # In case of auth required, block public user + if survey.auth_required and uid == request.registry['website'].get_public_user(request.cr, SUPERUSER_ID, request.context).id: + return request.website.render("website.401") + + # In case of non open surveys + if survey.state != 'open': + return request.website.render("survey.notopen") + + # If enough surveys completed + if survey.user_input_limit > 0: + completed = user_input_obj.search(cr, uid, [('state', '=', 'done')], count=True) + if completed >= survey.user_input_limit: + return request.website.render("survey.notopen") + + # Manual surveying + if not token: + if survey.visible_to_user: + user_input_id = user_input_obj.create(cr, uid, {'survey_id': survey.id}) + user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0] + else: # An user cannot open hidden surveys without token + return request.website.render("website.403") + else: + try: + user_input_id = user_input_obj.search(cr, uid, [('token', '=', token)])[0] + except IndexError: # Invalid token + return request.website.render("website.403") + else: + user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0] + + # Prevent opening of the survey if the deadline has turned out + # ! This will NOT disallow access to users who have already partially filled the survey ! + # if user_input.deadline > fields.date.now() and user_input.state == 'new': + # return request.website.render("survey.notopen") + # TODO check if this is ok + + # Select the right page + + if user_input.state == 'new': # Intro page + data = {'survey': survey, 'page': None, 'token': user_input.token} + return request.website.render('survey.survey_init', data) + else: + return request.redirect('/survey/fill/%s/%s' % (survey.id, user_input.token)) + + + + # Survey displaying @website.route(['/survey/fill//'], type='http', auth='public', multilang=True) - def fill_survey(self, survey, token=None, **post): + def fill_survey(self, survey, token, **post): '''Display and validates a survey''' cr, uid, context = request.cr, request.uid, request.context survey_obj = request.registry['survey.survey'] @@ -99,8 +158,6 @@ class WebsiteSurvey(http.Controller): # return request.website.render("survey.notopen") # TODO check if this is ok - _logger.debug('Incoming data: %s', post) - # Select the right page # if user_input.state == 'new' and not post: # Intro page @@ -151,15 +208,23 @@ class WebsiteSurvey(http.Controller): user_input_obj = request.registry['survey.user_input'] try: user_input_id = user_input_obj.search(cr, uid, [('token', '=', post['token'])])[0] - except IndexError: # Invalid token + except KeyError: # Invalid token return request.website.render("website.403") + + + + + # TODO # Store here data if allowed - ret['redirect'] = True + + + + ret['redirect'] = '/survey/fill/%s/%s' % (survey.id, post['token']) return json.dumps(ret) # Printing routes @website.route(['/survey/print//'], - type='http', auth='public', multilang=True) + type='http', auth='user', multilang=True) def print_empty_survey(self, survey=None, **post): '''Display an empty survey in printable view''' return request.website.render('survey.survey_print', @@ -284,17 +349,19 @@ class WebsiteSurvey(http.Controller): def validate_simple_choice(self, question, post, answer_tag): errors = {} - comment_tag = "%s_%s" % (answer_tag, question.comment_children_ids[0].id) + if question.comments_allowed: + comment_tag = "%s_%s" % (answer_tag, question.comment_children_ids[0].id) # Empty answer to mandatory question if question.constr_mandatory and not answer_tag in post: errors.update({answer_tag: question.constr_error_msg}) + if question.constr_mandatory and answer_tag in post and post[answer_tag].strip() == '': + errors.update({answer_tag: question.constr_error_msg}) # Answer is a comment and is empty if question.constr_mandatory and answer_tag in post and post[answer_tag] == "-1" and question.comment_count_as_answer and comment_tag in post and not post[comment_tag].strip(): errors.update({answer_tag: question.constr_error_msg}) # There is a comment and it should be validated # if question.comment_allowed and comment_tag in post and post[comment_tag].strip(): - ### if comments_allowed: ### if validation des comments required ### diff --git a/addons/survey/static/src/js/survey.js b/addons/survey/static/src/js/survey.js index 13892173ea3..08553b6317d 100644 --- a/addons/survey/static/src/js/survey.js +++ b/addons/survey/static/src/js/survey.js @@ -65,7 +65,7 @@ $(document).ready(function () { return false; } else if (_.has(response, 'redirect')){ // form is ok - alert('here, it should redirect'); + window.location.replace(response.redirect); return true; } else { // server sends bad data diff --git a/addons/survey/survey.py b/addons/survey/survey.py index 7010eaca995..4d798303e03 100644 --- a/addons/survey/survey.py +++ b/addons/survey/survey.py @@ -291,10 +291,6 @@ class survey_page(osv.osv): oldname="note"), } - _defaults = { - 'sequence': 1 - } - # Public methods # # def survey_save(self, cr, uid, ids, context=None): @@ -588,15 +584,15 @@ class survey_user_input(osv.osv): help="Date by which the person can take part to the survey", oldname="date_deadline"), 'type': fields.selection([('manually', 'Manually'), ('link', 'Link')], - 'Answer Type', required=1, oldname="response_type"), + 'Answer Type', required=1, readonly=1, oldname="response_type"), 'state': fields.selection([('new', 'Not started yet'), ('skip', 'Partially completed'), ('done', 'Completed')], 'Status', readonly=True), - 'test_entry': fields.boolean('Test entry'), + 'test_entry': fields.boolean('Test entry', readonly=1), + 'token': fields.char("Identification token", readonly=1, required=1), # Optional Identification data - 'token': fields.char("Identification token", readonly=1), 'partner_id': fields.many2one('res.partner', 'Partner', readonly=1), 'email': fields.char("E-mail", readonly=1), @@ -716,10 +712,6 @@ class survey_user_input(osv.osv): 'context': context } - def action_cancel(self, cr, uid, ids, context=None): - self.pool.get('survey.survey').check_access_rights(cr, uid, 'write') - return self.write(cr, uid, ids, {'state': 'cancel'}) - def name_get(self, cr, uid, ids, context=None): if not len(ids): return [] @@ -744,7 +736,7 @@ class survey_user_input_line(osv.osv): 'user_input_id': fields.many2one('survey.user_input', 'User Input', ondelete='cascade', required=1), 'survey_id': fields.related('user_input_id', 'survey_id', - type="many2one", relation="survye.survey", string='Survey'), + type="many2one", relation="survey.survey", string='Survey'), 'question_id': fields.many2one('survey.question', 'Question', ondelete='restrict'), 'page_id': fields.related('question_id', 'page_id', type='many2one', @@ -756,7 +748,7 @@ class survey_user_input_line(osv.osv): ('free_text', 'Free Text'), ('datetime', 'Date and Time'), ('checkbox', 'Checkbox')], - 'Question Type', required=1), + 'Answer Type', required=1), 'value_text': fields.char("Text answer"), 'value_number': fields.float("Numerical answer"), 'value_date': fields.datetime("Date answer"), diff --git a/addons/survey/views/survey_views.xml b/addons/survey/views/survey_views.xml index 51c29e67bd6..a2fcbbd75ca 100644 --- a/addons/survey/views/survey_views.xml +++ b/addons/survey/views/survey_views.xml @@ -33,7 +33,6 @@
-
@@ -46,15 +45,25 @@ - - - - - + + + + + + + + + + + + + + +
@@ -167,7 +176,6 @@ -