# db_0.py
# shortcut for `not null` field creation
def req_field(*args, **kwargs):
kwargs['notnull'] = True
kwargs['required'] = True
return Field(*args, **kwargs)
# shortcut for reference field creation, `notnull` by default
def FK(ref_table, **kwargs):
if 'notnull' not in kwargs:
kwargs['notnull'] = True
kwargs['required'] = True
return Field(ref_table, 'reference %s' % ref_table, **kwargs)
# SURVEY MODEL:
#-------------------[ SURVEY ]-------------------
t_name = 'survey'
db.define_table(t_name,
req_field('name', 'string'),
)
#-------------------[ QUESTION ]-------------------
t_name = 'question'
db.define_table(t_name,
req_field('content', 'string'),
)
#-------------------[ ANSWER ]-------------------
t_name = 'answer'
db.define_table(t_name,
req_field('content', 'string'),
)
#-------------------[ SECTION ]-------------------
t_name = 'section'
db.define_table(t_name,
req_field('name', 'string'),
)
#-------------------[ SURVEY_QUESTION ]-------------------
t_name = 'survey_question'
db.define_table(t_name,
FK('survey'),
FK('section'),
FK('question'),
)
__UNIQUE_INDEXES__[t_name] = ['survey', 'section', 'question'] # or maybe
just ['survey', 'question']?
#-------------------[ SURVEY_QUESTION_ANSWER ]-------------------
t_name = 'survey_question_answer'
db.define_table(t_name,
FK('survey_question'),
FK('answer'),
req_field('score', 'integer', default = 0),
)
__UNIQUE_INDEXES__[t_name] = ['survey_question', 'answer']
# RESULTS MODEL :
#-------------------[ SURVEY_USER_RESULT ]-------------------
# if record exists then `user` check `answer` to `question` from `survey`
t_name = 'survey_user_result'
db.define_table(t_name,
FK('survey_question_answer'),
FK('auth_user'),
)
__UNIQUE_INDEXES__[t_name] = ['survey_question_answer', 'auth_user']
# assuming SQLite
def __create_indexes__():
def make_uniq(tbl, cols):
sql_str = \
'CREATE UNIQUE INDEX IF NOT EXISTS %(index_name)s ON
%(table_name)s ( %(col_names)s );' \
% dict( index_name = idx_name,
table_name = tbl,
col_names = ','.join(["'%s'"%_ for _ in cols])
)
db.executesql(sql_str)
def create_indexes():
for t in __UNIQUE_INDEXES__:
args = __UNIQUE_INDEXES__[t]
if isinstance(args[0], basestring): # single index
args = [args]
for a in args:
make_uniq(t, list(a))
create_indexes()
# to create indexes uncomment line below, it is required to be run only
once
#__create_indexes__() # after index creation you can comment this line
again
# !!! if indexes or tablenames/fields will be changed (even order of fields
in __UNIQUE_INDEXES__[tbl])
# you should somehow drop old-existing indexes first (there are dozen
utils for SQLite)
On Saturday, October 12, 2019 at 7:54:05 PM UTC+3, NGNZONE wrote:
>
> Situation Description
>
> First things first, I am not a very experience web2py developer but I have
> learned the basics of the framework, so please pardon me if my questions
> are not so constructive. I started working on a survey application which
> will enable users to create multiple choice questions. A question will have
> a number of choices, and when the user selects a particular choice, a
> corresponding score will be added to a sub total. That is if the survey is
> about Fruits for instance, if a user selects oranges, 5 will be added to
> the sub total for that section, if another user selects Mangoes, 10 will be
> added to the sub total and so on. The attached screenshot survey_layout
> will better explain what I am talking about. It has three columns, the
> first column is the question, the second, the corresponding choices and the
> third column the score linked to each choice. [Survey layout][1] There are
> six sections in the survey, and each section has a sub_total which is
> generated from the score value of each choice the user chooses on each of
> the questions. Finally the sub_totals will be added up to give a grand
> total for the entire survey and it can then be interpreted. What I have
> done so far is as seen on the follow screenshots
>
>
>
> Questions:
>
> 1.How do I create my models in such a way that the user can dynamically
> create questions for the survey for each section on the page and when the
> user takes the survey, each choice the user selects, will correspond to a
> score value which can be tracked and summed up to give the section totals?
>
> 2.How will my form be defined to output all the questions for each section
> one after the other on the screen? I have tried using SQLFORMS but don't
> know exactly how to achieve this.
>
> 1. Thinking of using pagination to separate the sections so the page
> will not be too long. any better option?
>
> References: I have watched the web2py survey by MASSIMO DI PIERRO where I
> learn how to do what I have done so far.
>
> Thanks in advance
>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/390892d4-3a3d-4d2c-b2a5-170553b7649e%40googlegroups.com.