Hi all,
I having a weird problem. I'm clueless. I do not even know how to look it
up in the manual.

I'm trying to store in the database (training table) user defined
configurations that will lead to specific queries (accessed through
list_problems_by_training controller's default view).
These configurations are stored, in the db, as options that are mapped into
query chunks afterwards.

If I split the controller code in 2 functions it stops to work, but if I
collapse everything in one single function it works (see code at the end).

OPTION 1) 1 function WORKS

OPTION 2) 2 functions FAILS

I'd like to know why?

Thanks in advance, Jon.

NOTE: I simplified the code below (removing options) to make the email
shorter.

#----------------- in the models
#----- option to query mapping
player = {
'any':None,
'me':((db.probdata.white=='jonsubscriptions')|(db.probdata.black=='jonsubscriptions'))
}
color = {
'all':None,
'black':(db.probdata.color==False),
'white':(db.probdata.color==True),
}

#----- field option sets
PLAYER_SET = ('any','me')
COLOR_SET = ('all','black','white')

#----- tables
db.define_table(
    "training",
    Field("player",requires=IS_IN_SET(PLAYER_SET),default='any'),
    Field("color",requires=IS_IN_SET(COLOR_SET),default='all'),
    )
db.define_table(
    "user_training",
    Field("training","reference
training",requires=(IS_NOT_EMPTY(),IS_IN_DB(db,'training.id'))),
    Field("ucomment","string"),
    auth.signature)

#----------------- in the controllers
#OPTION 1: 1 function WORKS

def list_problems_by_training():
    training_id = request.args(0,cast=int,default=1)
    page = request.args(1,cast=int,default=0)
    start = page*PROBS_PER_PAGE
    stop = start+PROBS_PER_PAGE
    #query = get_training_query(training_id)
    training = db.training[training_id]
    player_query = player[training.player]
    color_query = color[training.color]
    query_total = None
    for q in [player_query,color_query]:
        if q is None:
            continue
        if query_total is None:
            query_total = q
        else:
            query_total = (query_total & q)
    total = db(query_total).count()
    rows = db(query_total).select(db.probdata.problem,
                                  db.probdata.color,
                                  limitby=(start,stop))
    return locals()


#OPTION 2: 2 functions FAILS
#----- query generator based on db
def get_training_query(training_id):
    training = db.training[training_id]
    player_query = player[training.player]
    color_query = color[training.color]
    query_total = None
    for q in [player_query,color_query]:
        if q is None:
            continue
        if query_total is None:
            query_total = q
        else:
            query_total = (query_total & q)
    return locals()

#example URL
http://127.0.0.1:8000/myapp/default/list_problems_by_training/4/0
def list_problems_by_training():
    training_id = request.args(0,cast=int,default=1)
    page = request.args(1,cast=int,default=0)
    start = page*PROBS_PER_PAGE
    stop = start+PROBS_PER_PAGE
    query = get_training_query(training_id)
#WHENEVER I UNCOMMENT THIS STATEMENT BELOW I GET AN ERROR <type
'exceptions.KeyError'> 'op'
    #rows = db(query).select(limitby=(start,stop))
    return locals()
I get this error.
Traceback (most recent call last):
  File "C:\Users\Jon\Downloads\web2py_win_CB\web2py\gluon\restricted.py",
line 219, in restricted
    exec(ccode, environment)
  File
"C:/Users/Jon/Downloads/web2py_win_CB/web2py/applications/myapp/controllers/default.py",
line 1156, in <module>
  File "C:\Users\Jon\Downloads\web2py_win_CB\web2py\gluon\globals.py", line
419, in <lambda>
    self._caller = lambda f: f()
  File "C:\Users\Jon\Downloads\web2py_win_CB\web2py\gluon\tools.py", line
3982, in f
    return action(*a, **b)
  File
"C:/Users/Jon/Downloads/web2py_win_CB/web2py/applications/myapp/controllers/default.py",
line 809, in list_problems_by_training
    rows = db(query).select(limitby=(start,stop))
  File
"C:\Users\Jon\Downloads\web2py_win_CB\web2py\gluon\packages\dal\pydal\base.py",
line 687, in __call__
    return self.where(query, ignore_common_filters)
  File
"C:\Users\Jon\Downloads\web2py_win_CB\web2py\gluon\packages\dal\pydal\base.py",
line 698, in where
    return Set(self, query, ignore_common_filters=ignore_common_filters)
  File
"C:\Users\Jon\Downloads\web2py_win_CB\web2py\gluon\packages\dal\pydal\objects.py",
line 2081, in __init__
    query = self.parse(query)
  File
"C:\Users\Jon\Downloads\web2py_win_CB\web2py\gluon\packages\dal\pydal\objects.py",
line 2152, in parse
    return self.build(self.dquery)
  File
"C:\Users\Jon\Downloads\web2py_win_CB\web2py\gluon\packages\dal\pydal\objects.py",
line 2156, in build
    op, first, second = (d["op"], d["first"],
KeyError: 'op'

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to