request.args always contain strings. str(request.args(0)) is
extraneous

There's a logic hole here: What if my username is "1" but my record's
id is 22. This code will return the record db.auth_user[1] when it's
supposed to return db.auth_user[22]

Anyway, ou get "invalid literal for int() with base 10" error because
auth_user.id expects integers and you're sending it a string,
"kenneth". Quick fix:
user = db(db.auth_user.id == request.args[0] if
request.args[0].isdigit() else 0).select()

That fixes the syntax error but not the logic error I pointed out
earlier.



On Jul 15, 5:06 am, Kenneth Lundström <[email protected]>
wrote:
> Hello,
>
> why can�t I do this:
> user = db(db.auth_user.id == 'kenneth').select()
>
> I know that this should result in len(user) = 0 but I�m sending
> sometimes a text and sometimes a number and I�m trying to do this:
>          user = db(db.auth_user.id == str(request.args[0])).select()
>          if len(user):
>              user_id = user[0].id
>          else:
>              user = db(db.auth_user.username == request.args[0]).select()
>              if len(user):
>                  user_id = user[0].id
>              else:
>                  user_id = 0
>
>     Error ticket for "kenneths"
>
>       Ticket ID
>
> 85.76.66.169.2011-07-15.00-45-45.b6793e0f-2144-4a02-802f-d7b8a45ec8a5
>
>       Version
>
> web2py^(TM)     Version 1.97.1 (2011-06-26 19:25:44)
> Python  Python 2.6.5: /usr/bin/python
>
>       Traceback
>
> 1.
> 2.
> 3.
> 4.
> 5.
> 6.
> 7.
> 8.
> 9.
> 10.
> 11.
> 12.
> 13.
> 14.
> 15.
> 16.
> 17.
> 18.
> 19.
> 20.
> 21.
> 22.
> 23.
> 24.
>
> Traceback(most recent call last):
>    File"/data/domains/web2py/gluon/restricted.py",line192,inrestricted
>      execccodeinenvironment
>    File"/data/domains/web2py/applications/kenneths/controllers/ticket.py"  
> <https://web2py.nudata.fi/admin/default/edit/kenneths/controllers/tick...>,line43,in<module>
>    File"/data/domains/web2py/gluon/globals.py",line137,in<lambda>
>      self._caller=lambdaf:f()
>    File"/data/domains/web2py/applications/kenneths/controllers/ticket.py"  
> <https://web2py.nudata.fi/admin/default/edit/kenneths/controllers/tick...>,line9,innew_ticket
>      user=db(db.auth_user.id=='kenneth').select()
>    File"/data/domains/web2py/gluon/dal.py",line5394,inselect
>      return self.db._adapter.select(self.query,fields,attributes)
>    File"/data/domains/web2py/gluon/dal.py",line1168,inselect
>      sql=self._select(query,fields,attributes)
>    File"/data/domains/web2py/gluon/dal.py",line1078,in_select
>      sql_w=' WHERE '+self.expand(query)
>    File"/data/domains/web2py/gluon/dal.py",line937,inexpand
>      returnexpression.op(expression.first,expression.second)
>    File"/data/domains/web2py/gluon/dal.py",line886,inEQ
>      return'(%s = %s)'% (self.expand(first),self.expand(second,first.type))
>    File"/data/domains/web2py/gluon/dal.py",line943,inexpand
>      return self.represent(expression,field_type)
>    File"/data/domains/web2py/gluon/dal.py",line1280,inrepresent
>      returnstr(int(obj))
> ValueError:invalid literalforint()with base10:'kenneth'
>
>       Error snapshot
>
> |<type 'exceptions.ValueError'>(invalid literal for int() with base 10:
> 'kenneth')|

Reply via email to