[IMP] Constraints validation
bzr revid: rim@openerp.com-20131108075347-3rv4i7vc09v91mx7
This commit is contained in:
parent
3290b04e55
commit
e112e7adc3
|
@ -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/<int:survey_id>/",
|
||||
"/survey/fill/<int:survey_id>/page/",
|
||||
"/survey/fill/<int:survey_id>/page/<int:page_index>/"],
|
||||
@website.route(['/survey/fill/<model("survey.survey"):survey>/'],
|
||||
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/<int:survey_id>/"],
|
||||
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 []
|
||||
|
|
|
@ -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'),
|
||||
|
||||
|
|
|
@ -25,6 +25,20 @@
|
|||
</t>
|
||||
</template>
|
||||
|
||||
<!-- List of all the surveys -->
|
||||
<template id="finished" name="Survey Finished">
|
||||
<t t-call="website.layout">
|
||||
<div class="wrap">
|
||||
<div class="container">
|
||||
<div class="jumbotron">
|
||||
<h1>Thank you!<br/><small>Your answers have been recorded.</small></h1>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</t>
|
||||
</template>
|
||||
|
||||
|
||||
<!-- A survey -->
|
||||
<template id="survey" name="Survey">
|
||||
<t t-call="website.layout">
|
||||
|
@ -44,6 +58,13 @@
|
|||
</t>
|
||||
</h1>
|
||||
</div>
|
||||
<div class="text-center">
|
||||
<form role="form" method="post" t-att-name="survey.id.__str__() + '*-1'" t-att-action="'/survey/fill/' + survey.id.__str__()" target="">
|
||||
<input type="hidden" name="current" t-att-value="pagination['current'].__str__()" />
|
||||
<input type="hidden" name="next" t-att-value="pagination['next'].__str__()" />
|
||||
<input type="submit" class="btn btn-primary active" value="Take survey"/>
|
||||
</form>
|
||||
</div>
|
||||
</t>
|
||||
<t t-if="pagination['current'] != -1">
|
||||
<div class='page-header'>
|
||||
|
@ -55,7 +76,6 @@
|
|||
<t t-call="survey.page" />
|
||||
</t>
|
||||
|
||||
<t t-call='survey.navsub' />
|
||||
|
||||
</div>
|
||||
<div class="col-md-1"> </div>
|
||||
|
@ -70,9 +90,8 @@
|
|||
<!-- Navigation & Submission -->
|
||||
<template id="navsub" name="Footer for navigation and submission">
|
||||
<div class="text-center">
|
||||
<t t-if="pagination['current'] == -1"><a t-att-href="'/survey/fill/' + survey.id.__str__() + '/page/' + pagination['next'].__str__()" class="btn btn-primary active" role="button">Take survey</a></t>
|
||||
<t t-if="pagination['current'] != -1 and pagination['next'] != -1"><a t-att-href="'/survey/fill/' + survey.id.__str__() + '/page/' + pagination['next'].__str__()" class="btn btn-primary active" role="button">Next page</a></t>
|
||||
<t t-if="pagination['next'] == -1"><button t-att-href="'#'" class="btn btn-success active" role="submit">Submit survey</button></t>
|
||||
<t t-if="pagination['current'] != -1 and pagination['next'] != 'finished'"><input type="submit" class="btn btn-primary active" value="Next page"/></t>
|
||||
<t t-if="pagination['next'] == 'finished'"><input type="submit" class="btn btn-success active" value="Submit survey"/></t>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
@ -87,21 +106,28 @@
|
|||
<hr/>
|
||||
|
||||
<!-- Panel to display input errors -->
|
||||
<t t-if="validation_error">
|
||||
<t t-if="problems">
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Errors on this page</h3>
|
||||
</div>
|
||||
<div class="panel-body text-danger">
|
||||
Some answers are missing or incorrect
|
||||
<ul>
|
||||
<t t-foreach="problems" t-as='problem'>
|
||||
<li><strong><t t-esc="problem['qlabel']"/> :</strong> <t t-esc="problem['errmsg']"/></li>
|
||||
</t>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</t>
|
||||
|
||||
<form role="form">
|
||||
<form role="form" method="post" t-att-name="survey.id.__str__() + '*' + pagination['current'].__str__()" t-att-action="'/survey/fill/' + survey.id.__str__()" target="">
|
||||
<input type="hidden" name="current" t-att-value="pagination['current'].__str__()" />
|
||||
<input type="hidden" name="next" t-att-value="pagination['next'].__str__()" />
|
||||
<t t-foreach='page.question_ids' t-as='question'>
|
||||
<t t-call='survey.question'/>
|
||||
</t>
|
||||
<t t-call='survey.navsub' />
|
||||
</form>
|
||||
|
||||
</template>
|
||||
|
@ -118,32 +144,32 @@
|
|||
<t t-if="not is_vector_element"> <!-- if -->
|
||||
<h3>
|
||||
<t t-if="question.constr_mandatory"><span class="glyphicon glyphicon-exclamation-sign"></span></t>
|
||||
<t t-esc='question.question'/><br/>
|
||||
<span t-field='question.question'/><br/>
|
||||
|
||||
<small><t t-if="question.description"><t t-esc='question.description'/></t></small>
|
||||
</h3>
|
||||
</t>
|
||||
<t t-if="is_vector_element"> <!-- else -->
|
||||
<t t-if="question.type == 'simple_choice_scale'">
|
||||
<t t-esc='question.question'/><t t-raw="'</td><td>'" />
|
||||
<t t-field='question.question'/><t t-raw="'</td><td>'" />
|
||||
</t>
|
||||
</t> <!-- endif -->
|
||||
|
||||
<!-- instanciating different types of questions -->
|
||||
<t t-if="question.type in ['free_text']">
|
||||
<textarea class="form-control" rows="3"></textarea>
|
||||
<textarea class="form-control" rows="3" t-att-name="survey.id.__str__() + '*' + pagination['current'].__str__() + '*' + question.id.__str__()"></textarea>
|
||||
</t>
|
||||
|
||||
<t t-if="question.type in ['textbox']">
|
||||
<input type="text" class="form-control" />
|
||||
<input type="text" class="form-control" t-att-name="survey.id.__str__() + '*' + pagination['current'].__str__() + '*' + question.id.__str__()"/>
|
||||
</t>
|
||||
|
||||
<t t-if="question.type in ['numerical_box']">
|
||||
<input type="number" class="form-control"/>
|
||||
<input type="number" class="form-control" t-att-name="survey.id.__str__() + '*' + pagination['current'].__str__() + '*' + question.id.__str__()"/>
|
||||
</t>
|
||||
|
||||
<t t-if="question.type in ['datetime']">
|
||||
<input type="datetime-local" class="form-control" />
|
||||
<t t-if="question.type in ['datetime']" >
|
||||
<input type="datetime-local" class="form-control" t-att-name="survey.id.__str__() + '*' + pagination['current'].__str__() + '*' + question.id.__str__()"/>
|
||||
TODO: replace html5 datetime widget by OpenERP-CMS datetime widget
|
||||
</t>
|
||||
|
||||
|
@ -151,7 +177,7 @@
|
|||
<t t-foreach='question.suggested_answers_ids' t-as='suggestion'>
|
||||
<div class="radio-inline">
|
||||
<label>
|
||||
<input type="radio" t-att-name="suggestion.question_id.id" t-att-value='suggestion.value' />
|
||||
<input type="radio" t-att-name="survey.id.__str__() + '*' + pagination['current'].__str__() + '*' + question.id.__str__()" t-att-value='suggestion.value' />
|
||||
<t t-esc='suggestion.value'/>
|
||||
</label>
|
||||
</div>
|
||||
|
@ -159,7 +185,7 @@
|
|||
</t>
|
||||
|
||||
<t t-if="question.type in ['simple_choice_dropdown']">
|
||||
<select class="form-control">
|
||||
<select class="form-control" t-att-name="survey.id.__str__() + '*' + pagination['current'].__str__() + '*' + question.id.__str__()">
|
||||
<t t-foreach='question.suggested_answers_ids' t-as='suggestion'>
|
||||
<option><t t-esc='suggestion.value'/></option>
|
||||
</t>
|
||||
|
@ -170,7 +196,7 @@
|
|||
<t t-foreach='question.suggested_answers_ids' t-as='suggestion'>
|
||||
<div class="checkbox">
|
||||
<label>
|
||||
<input type="checkbox" t-att-name="suggestion.question_id.id" t-att-value='suggestion.value' />
|
||||
<input type="checkbox" t-att-name="survey.id.__str__() + '*' + pagination['current'].__str__() + '*' + question.id.__str__() + '*' + suggestion.id.__str__()" t-att-value='suggestion.value' />
|
||||
<t t-esc='suggestion.value'/>
|
||||
</label>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue