This is the main issue I am trying to solve. I often do

def show():
     record = db.mytable[request.args(0)]

This can fail in 3 ways:
- request.args(0) is None or the id of a non-existing record =>
record=None
- request.args(0) is a string but not is_digiT() => raise Exception
- request.args(0) is a field name => record is a field object

So I have to check for three different things to avoid lots of
tickets.
Now I can do:

def show():
     record = db.mytable(request.args(0)) or handle_error()

And I never get a ticket.

If my table has a different key or different unique field, for example
a uuid I can also do

def show():
     record = db.mytable(uuid=request.args(0)) or handle_error()

and it still works.

Often I need to fetch the record and then perform checks on it, such
as active=True or I am the owner. I can also do

def show():
     record =
db.mytable(request.args(0),active=True,owner=auth.user_id) or
handle_error()

I think this is going to be very convenient. Of course it should bot
be used where there is more than one record. I would not object to a
check in place that IF there more than one record, it also returns
None or raises an Exception.

Do you think we should add such check? What should be the behavior in
this case?







On Aug 1, 12:49 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> smells fishy
>
> --
> Thadeus
>
> On Sat, Jul 31, 2010 at 9:23 PM, mdipierro <mdipie...@cs.depaul.edu> wrote:
> > You have db(...).select()
> > This new syntax is a shortcut for  db().select().first()
>
> > On 31 Lug, 20:40, Thadeus Burgess <thade...@thadeusb.com> wrote:
> >> If there are multiple records we need to know about it =/
>
> >> --
> >> Thadeus
>
> >> On Sat, Jul 31, 2010 at 11:36 AM, mdipierro <mdipie...@cs.depaul.edu> 
> >> wrote:
> >> > No. Only the first.
>
> >> > Massimo
>
> >> > On Jul 31, 10:50 am, David Marko <dma...@tiscali.cz> wrote:
> >> >> ### print db.person(name='john')
> >> >> Does it also return many items when many items matches the condition?
>
> >> >> David
>
> >> >> On 31 čnc, 13:57, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> >> >> > given
>
> >> >> >   db.define_table('person',Field('name'))
> >> >> >   id = db.person.insert(name='john')
>
> >> >> > You can now do
>
> >> >> >   print db.person(id)
> >> >> >   print db.person(db.person.name=='john')
> >> >> >   print db.person(name='john')
> >> >> >   print db.person(id,name='john')
>
> >> >> > they all return the same record 'john'. On failure (record does not
> >> >> > exist) they return None. This allows the following syntax:
>
> >> >> >   record = db.person(request.args(0)) or redirect(URL('error'))
>
> >> >> > this is better than
>
> >> >> >   record = db.person[request.args(0)] or redirect(URL('error'))
>
> >> >> > since the latter raises an exception in case request.args(0) is not
> >> >> > None or an int.

Reply via email to