[IMP] Controller splitted among "start a survey" and "fill a survey"
[IMP] User input admin view bzr revid: rim@openerp.com-20131126151726-8en9gh9u9ygcp2uz
This commit is contained in:
@@ -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/<model("survey.survey"):survey>',
|
||||
'/survey/start/<model("survey.survey"):survey>/<string:token>'],
|
||||
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/<model("survey.survey"):survey>/<string:token>'],
|
||||
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/<model("survey.survey"):survey>/'],
|
||||
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
|
||||
###
|
||||
|
||||
@@ -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
|
||||
|
||||
+5
-13
@@ -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"),
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
<field name="arch" type="xml">
|
||||
<form string="Survey User inputs" version="7.0">
|
||||
<header>
|
||||
<button name="action_cancel" states="new,skip" string="Cancel" type="object" icon="gtk-stop"/>
|
||||
<field name="state" widget="statusbar"/>
|
||||
</header>
|
||||
<sheet>
|
||||
@@ -46,15 +45,25 @@
|
||||
<group>
|
||||
<field name="survey_id"/>
|
||||
<field name="date_create"/>
|
||||
<field name="deadline"/>
|
||||
<field name="type"/>
|
||||
</group>
|
||||
<group>
|
||||
<field name="partner_id"/>
|
||||
<field name="email"/>
|
||||
<field name="token" groups="base.group_no_one"/>
|
||||
</group>
|
||||
<group>
|
||||
<field name="deadline"/>
|
||||
<field name="partner_id"/>
|
||||
<field name="email" widget="email"/>
|
||||
<field name="test_entry" groups="base.group_no_one"/>
|
||||
</group>
|
||||
</group>
|
||||
<field name="user_input_line_ids" mode="tree" attrs="{'readonly': False}">
|
||||
<tree>
|
||||
<field name="page_id"/>
|
||||
<field name="question_id"/>
|
||||
<field name="answer_type"/>
|
||||
<field name="skipped"/>
|
||||
<field name="date_create"/>
|
||||
</tree>
|
||||
</field>
|
||||
</sheet>
|
||||
</form>
|
||||
</field>
|
||||
@@ -167,7 +176,6 @@
|
||||
<field name="user_input_line_ids"/>
|
||||
</tree>
|
||||
</field>
|
||||
|
||||
</page>
|
||||
</notebook>
|
||||
</sheet>
|
||||
|
||||
Reference in New Issue
Block a user