In the controller below, I display a list of jokes for visitors to the
site.  They can choose to sort by author or category.  They can also
filter the list to a single category and/or a single author.   The
view includes a form with select/option drop downs for each choice.
Then, I'll change the queries to reflect the visitor's choices
(haven't implemented that below, yet).

For just presenting the choices, the controller below works.  It just
seems like it's a bit clunky--like I am not doing this in the most
compact way that web2py makes possible.  I've queried for only those
categories and authors for which there are jokes.  Then, the list
comprehension "pulls" out the part of the rows that appear in the
options of the form.  Seems like too many steps somehow...

Then, I'll need an "if form.process().accepts...: ...." to process the
user's choices and then sort and re-present the list of jokes.  Seems
like there should be some slick way to get the user's choices into the
request.vars so that I can do one of those slick web2py things like
"...orderby=request.args(1) or <default sort field>)".

If this is fine, I'm happy but seems like this is a sort of common
thing to do--let user's pick sort field and filter values...

most of the applicable controller:  (funny wrapping because of width
of this text box)

sort_choices=['Category','Author']
sort_ctrl = FORM(SELECT(sort_choices))
#
category_choices =
jodb(jodb.joke_category.category==jodb.category.id).select(jodb.category.name,jodb.category.id,
    orderby = jodb.category.name, distinct=True).as_list()
category_choices = [OPTION(i['name'],_value=i['id']) for i in
category_choices]
cat_ctrl = FORM(SELECT(category_choices))
#
author_choices =
jodb(jodb.joke.created_by==jodb.auth_user.id).select(jodb.auth_user.first_name,
        jodb.auth_user.last_name, jodb.auth_user.id, orderby =
jodb.auth_user.last_name, distinct=True).as_list()
author_choices = [OPTION(i['first_name'],' ',
i['last_name'],_value=i['id']) for i in author_choices]
author_ctrl = FORM(SELECT(author_choices))
#
query1 = ((jodb.joke.id==jodb.joke_category.joke) &
        (jodb.joke_category.category == jodb.category.id) &
        (jodb.joke.created_by == jodb.auth_user.id))
jokes_cats = jodb(query1).select(jodb.joke.joketext,
jodb.joke.created_by,
        jodb.auth_user.first_name, jodb.auth_user.last_name,
        jodb.category.name,orderby = jodb.category.name)
return
dict(jokes=jokes_cats,sort_ctrl=sort_ctrl,author_ctrl=author_ctrl,
cat_ctrl = cat_ctrl)

Reply via email to