Hello everyone. Is there anyone who could shed some light on this?
1. There are two versions of the code below which I thought are
functionally the same, but the better looking code (version 1)
doesn't work as expected while ugly version 2 kinda works,
browsing through questions work but validation of answers fail
every time ( I suspect the code is checking the answers against
the choices loaded in a previous questions (how come???) but
couldn't put my finger on it and as far as see it, the only
difference between v 1 and 2 is the use of variables d_eng and
d_loc ...)
2. When is the "readable" parameter determined? For example, the
value of variable session.q['locale'] is 2, and since "2<>1"
evaluates to False, how come the field is still readable, or
shown on screen when I expect it to be hidden?
My objective is to display a form, show a random question, validate
answers against allowed choices then store it a temporary variable, then
move to another question (or back to a previous question).When all the
questions are done, save the answers as a single record in the quiz
database, then mark it to determine the score.
I suspect that my problem stems from misunderstanding some web2py
concept but I'm not sure what... :P. Knowing what I intend to do, am I
on the right track of actually doing it right? If not, your suggestions
are very welcome.
Things I've noticed but don't know what to do:
Nik
db.define_table('language',
Field('description'),
format='%(description)s')
db.define_table('questionnaire',
Field('dialog_id'),
Field('language' db.language)
Field('dialog')
Field('choices', 'reference:string')
Field('key')
#version 1 - nice to look at but doesn't work. :-D
#"readable" locale field parameter doesn't work as expected.
session.q['locale'] is set in another view/function.
#q: shortform for db.questionnaire
q = db.questionnaire
#retrieve English dialog (default language)
d_eng =
db((q.dialog_id==session.q['draw'][session.item_id]['id'])&(q.language==1)).select(q.dialog)
#retrieve Alternative language and option list
d_loc =
db((q.dialog_id==session.q['draw'][session.item_id]['id'])&(q.language==session.q['locale'])).select(q.dialog,
q.choices)
form=SQLFORM.factory(
Field('english', 'string', writable=False,
default=d_eng[session.item_id-1]['dialog']),
Field('locale', 'string', writable=False,
readable = session.q['locale']<>1, # hide field if chosen
locale is English.
default=d_loc[session.item_id-1]['dialog']),
Field('answer',
widget=SQLFORM.widgets.checkboxes.widget,
default= session.q['answer'][session.item_id],
requires=IS_IN_SET(d_loc[session.item_id]['choices'],
multiple=True)))
#version 2 - hard to read, but browsing through questions work. when user
chooses an answer, validation fails.
# locale field readable still doesn't work.
#q: shortform for db.questionnaire
q = db.questionnaire
form=SQLFORM.factory(
Field('english', 'string', writable=False,
default=db((q.dialog_id==session.q['draw'][session.item_id]['id'])&(q.language==1)).select(q.dialog)[0]['dialog']),
Field('locale', 'string', writable=False,
readable = session.q['locale']<>1, # hide field if no locale
selected (defaults to English)
default=db((q.dialog_id==session.q['draw'][session.item_id]['id'])&(q.language==session.q['locale'])).select(q.dialog)[0]['dialog']),
Field('answer',
widget=SQLFORM.widgets.checkboxes.widget,
default= session.q['answer'][session.item_id],
requires=IS_IN_SET(db((q.dialog_id==session.q['draw'][session.item_id]['id'])&(q.language==1)).select(q.choices)[0]['choices'],
multiple=True)))
<<attachment: face-smile-big.png>>

