'Display and validates a survey'

@http.route(['/survey/fill/<model("survey.survey"):survey>/<string:token>', '/survey/fill/<model("survey.survey"):survey>/<string:token>/<string:prev>'], type='http', auth='public', website=True)
def fill_survey(self, survey, token, prev=None, **post):

    Survey = request.env['survey.survey']
    UserInput = request.env['survey.user_input']
    errpage = self._check_bad_cases(survey)
    if errpage:
        return errpage
    try:
        user_input = UserInput.sudo().search([('token', '=', token)], limit=1)
    except IndexError:
        return request.render('website.403')
    errpage = self._check_deadline(user_input)
    if errpage:
        return errpage
    if (user_input.state == 'new'):
        (page, page_nr, last) = Survey.next_page(user_input, 0, go_back=False)
        data = {'survey': survey, 'page': page, 'page_nr': page_nr, 'token': user_input.token}
        if last:
            data.update({'last': True})
        return request.render('survey.survey', data)
    elif (user_input.state == 'done'):
        return request.render('survey.sfinished', {'survey': survey, 'token': token, 'user_input': user_input})
    elif (user_input.state == 'skip'):
        flag = (True if (prev and (prev == 'prev')) else False)
        (page, page_nr, last) = Survey.next_page(user_input, user_input.last_displayed_page_id.id, go_back=flag)
        if (not page):
            (page, page_nr, last) = Survey.next_page(user_input, user_input.last_displayed_page_id.id, go_back=True)
        data = {'survey': survey, 'page': page, 'page_nr': page_nr, 'token': user_input.token}
        if last:
            data.update({'last': True})
        return request.render('survey.survey', data)
    else:
        return request.render('website.403')
