Here's part of a controller I've written to evaluate answers in a
language quizzing app (Forgive the poor coding--I'm a religious
studies professor and am just self-taught at all of this). In the
snippet below I'm checking evaluating the answer and updating the
table that holds students' records. Since I want a high level of
granularity in my tracking of student performance I'm tracking several
pieces of data on each student, for each individual question. The
problem is that different db fields need to be updated depending on a
student's answer. E.g., if the student has answered correctly, I need
to update db.question_records.last_right but not
db.question_records.last_wrong. If the student submits a wrong answer
I need to update .times_wrong but not .times_write. I also need to
create a new row in the question_records table for the student/
question combination if this is the student's first attempt. But if
not, I need to update an existing row. (This is why I'm not just
issuing different update() instructions for correct, partially
correct, and incorrect answers. In that case I would need an if/else
and two different update instructions for each kind of answer--either
to update or to create the necessary table row)
It's all working, but doesn't feel very "DRY" or efficient. I'm moving
through an if/elif tree, creating needless variables with default
values for the fields that don't need updating, making unnecessary db
calls along the way. What I'd like to be able to do is define a sub-
function (or external function) to provide the common architecture for
handling a student response, and then create related functions that
inherit its architecture and just change a few specifics (e.g., are we
updating the last_right field or last_wrong field?) What I've been
thinking about is creating an external py file with a prototype python
class and a few sub-classes, then just creating instances of these
classes in my controller. But I keep thinking that there must be some
way to do it within web2py's own architecture.
Here's the snippet from my controller:
#Evaluate answer, update records, and redirect to 'respond' page
if a response is being submitted
if request.vars.user_response:
#include this question in the number of questions attempted in
this session
if session.q_counter:
session.q_counter += 1
else:
session.q_counter = 1
#get the question that was asked
q_ID = session.qID
the_q = db(db.question_records.question==q_ID).select()
#see whether answer matches any of the three answer
fields
if
re.match(session.answer,request.vars.user_response):
session.response = 'correct'
rightCount = 1
wrongCount = 0
if the_q:
wrongDate = the_q[0].last_wrong
else:
wrongDate = request.now
rightDate = request.now
session.score = 1
elif re.match(session.answer2,request.vars.user_response) and
session.answer2 != 'null':
session.response = 'partial'
rightCount = 0
wrongCount = 0
if the_q:
wrongDate = the_q[0].last_wrong
rightDate = the_q[0].last_right
else:
wrongDate = request.now
rightDate = request.now
session.score = 0.5
elif re.match(session.answer3,request.vars.user_response) and
session.answer2 != 'null':
session.response = 'partial'
rightCount = 0
wrongCount = 0
if the_q:
wrongDate = the_q[0].last_wrong
rightDate = the_q[0].last_right
else:
wrongDate = request.now
rightDate = request.now
session.score = 0.3
else:
session.response = "wrong"
wrongCount = 1
rightCount = 0
if the_q:
rightDate = the_q[0].last_right
else:
rightDate = request.now
wrongDate = request.now
session.score = 0
#If the user has already attempted this question once, update
their record for this question
if
db((db.question_records.name==auth.user_id)&(db.question_records.question==q_ID)).select():
timesR = the_q[0].times_right
timesW = the_q[0].times_wrong
newTimesR = int(timesR) + int(rightCount)
newTimesW = int(timesW) + int(wrongCount)
db(db.question_records.question==q_ID).update(times_right=newTimesR,
times_wrong=newTimesW, last_right=rightDate, last_wrong=wrongDate)
#if the user hasn't attempted this question, create a new
record for it
else:
db.question_records.insert(question=q_ID,
times_right=rightCount, times_wrong=wrongCount)
#display the answer page
redirect(URL('respond'))
On Aug 30, 10:27 pm, Massimo Di Pierro <[email protected]>
wrote:
> Within one controller, one function can call another.
>
> If you have two controllers in different apps they should not depend
> on each other because the two apps can be distributed separately.
>
> It remains the case of two controllers in the same app. If they depend
> one on the other they should be the same controller.
>
> Perhaps I am missing something Can you make an example of use case?
>
> On Aug 30, 6:51 pm, monotasker <[email protected]> wrote:
>
>
>
>
>
>
>
> > Hi all,
>
> > I'm fairly new to python as well as to web2py, so this may be a silly
> > question. But is there any way to make one controller extend another,
> > like view inheritance?
>
> > More broadly, I'm finding that it's hard to take advantage of python's
> > OO structure in controllers. I keep writing nested "if else"
> > statements to handle different kinds of input, instead of being able
> > to invoke different objects to handle it. I feel like I'm back to old-
> > style javascript functions for controllers.
>
> > Thanks for your help,
>
> > Ian