How general does it need to be? Are you always checking a field named
"uuid" (but potentially in different tables)? Does the id being checked
always come from request.args(0)?
Note, request.args(0) simply returns None if there are no request.args, so
as long as no records have a uuid == None, you don't have to bother
checking for request.args -- just use request.args(0), and you won't get
any records when there are no request.args. Also, if you add .first() after
.select(), you'll get the first row of the returned set, or None if no
records where returned -- so again, you don't have to test for the length
of the returned set.
Maybe something like:
def provider(table, id=request.args(0)):
return dict(provider=db(db[table].uuid == id).select().first())
myprovider = provider('auth_user')
In that case, if there are no request.args, as long as no records have
uuid==None, the function will return None. Likewise, if there is a
request.args(0) but its value is not one of the uuid's in the table (i.e.,
no records are returned), the function will return None.
Anthony
On Sunday, April 22, 2012 8:33:13 AM UTC-4, sebastian wrote:
>
>
> Hi All,
>
> I'm using this function at least twice... and I'll use it again... how can
> I reuse it in some way ? for example passing the table and arg as function
> parameters...
>
> def provider():
> theProvider = None
> if len((request.args))>=1:
> providers=db(db.auth_user.uuid==request.args[0]).select()
> if len(providers) == 1:
> theProvider = providers[0]
>
> return dict(theProvider=theProvider)
>
> thanks
> --
> Sebastian E. Ovide
>
>
>
>
>