[web2py] Strange behavior storing data - web2py 2.3.2 - Mac OS X 10.7.5

2013-01-14 Thread Luciano Laporta Podazza
Hello,

I've been experimenting a really annoying issue that came up from nowhere 
'cause the app was working perfectly. This is the scenario:

A mobile app user(iOS) enter his credentials on Facebook and gets the 
corresponding auth token and personal data(JSON).
The mobile app sends for the first time that data(name,last name, token, 
etc) to the server in JSON and the server processes it and store it if no 
errors occurred.
This is the code(yes, it's really ugly but I'm a n00b):

Controller:
@service.json
def first_time():
data = gluon.contrib.simplejson.loads(request.body.read())
graph = GraphAPI(data['token'])
#We check that email and token exists and are valid, otherwise we store 
it for the first time.
mail_check = db(db.auth_user.email==data['email']).select()
if mail_check:
token = db((db.auth_user.facebook_token!=data['token'])(db.
auth_user.email==data['email'])).select()
if token:
try:
profile = graph.get_object(me)
if profile:
id = db(db.auth_user.email==data['email']).update(
facebook_token=data['token'])
return token_updated
except:
return invalid_token1
else:
#We create a new user!
try:
profile = graph.get_object(me)
if profile:
new = db.auth_user.insert(
first_name=data['first_name'], 
last_name=data['last_name'],
birthday= datetime.strptime(data['birthday'],'%d/%m/%Y'
).strftime('%Y-%m-%d'),
email=data['email'],
facebook_id=data['id'],
facebook_token=data['token'],
wizard=data['wizard'])
return new_user 
except:
return ivalid_token2
return user_exists


db.py:

from gluon.tools import Auth
auth = Auth(db, hmac_key=Auth.get_or_create_key())


auth.settings.extra_fields['auth_user']= [ 
Field('birthday', 'date'),
Field('facebook_id', 'string'),
Field('facebook_token', 'string'),
Field('wizard', 'boolean'),
]


JSON:
{ 
  first_name: Luciano,
  last_name: Laporta Podazza,
  country : Argentina,
  date : 2013-01-14,
  email : m...@hotmail.com,
  province : Tucumán,
  timestamp : 2013-01-14 06:13:45,
  token : (token)
}



This was actually working and I didn't modified anything(really). Now, the 
issue that I have is that with valid information the data isn't stored in 
the DB and get the invalid_token2 error.

Notes:

   - Facebook data is valid(specially tokens), I've tested all of them 
   externally with GraphAPI and they work.
   - If I comment the whole .insert I get the new_user string, which is 
   what I'm looking for. So this makes me think that something with the insert 
   is wrong
   - All tables and fields are correct, they have been tested and working 
   before this issue.
   - I've tried changing database adapters(I was using mysql and now sqlite 
   and still get the same error).
   - All facebook accounts are valid and verified.
   - For some reason, some times it stores the data, and some times not, 
   it's kind of random. 
   
Any suggestions?. Thanks in advance!

-- 





Re: [web2py] Strange behavior storing data - web2py 2.3.2 - Mac OS X 10.7.5

2013-01-14 Thread Luciano Laporta Podazza

On Jan 14, 2013, at 2:45 PM, Massimo Di Pierro wrote:

 You say This was actually working and I didn't modified anything(really). 
 did you upgrade or did it simply stopped working?

It simply stopped working.

 
 On Monday, 14 January 2013 05:19:05 UTC-6, Luciano Laporta Podazza wrote:
 Hello,
 
 I've been experimenting a really annoying issue that came up from nowhere 
 'cause the app was working perfectly. This is the scenario:
 
 A mobile app user(iOS) enter his credentials on Facebook and gets the 
 corresponding auth token and personal data(JSON).
 The mobile app sends for the first time that data(name,last name, token, etc) 
 to the server in JSON and the server processes it and store it if no errors 
 occurred.
 This is the code(yes, it's really ugly but I'm a n00b):
 
 Controller:
 @service.json
 def first_time():
 data = gluon.contrib.simplejson.loads(request.body.read())
 graph = GraphAPI(data['token'])
 #We check that email and token exists and are valid, otherwise we store 
 it for the first time.
 mail_check = db(db.auth_user.email==data['email']).select()
 if mail_check:
 token = 
 db((db.auth_user.facebook_token!=data['token'])(db.auth_user.email==data['email'])).select()
 if token:
 try:
 profile = graph.get_object(me)
 if profile:
 id = 
 db(db.auth_user.email==data['email']).update(facebook_token=data['token'])
 return token_updated
 except:
 return invalid_token1
 else:
 #We create a new user!
 try:
 profile = graph.get_object(me)
 if profile:
 new = db.auth_user.insert(
 first_name=data['first_name'], 
 last_name=data['last_name'],
 birthday= 
 datetime.strptime(data['birthday'],'%d/%m/%Y').strftime('%Y-%m-%d'),
 email=data['email'],
 facebook_id=data['id'],
 facebook_token=data['token'],
 wizard=data['wizard'])
 return new_user 
 except:
 return ivalid_token2
 return user_exists
 
 
 db.py:
 
 from gluon.tools import Auth
 auth = Auth(db, hmac_key=Auth.get_or_create_key())
 
 
 auth.settings.extra_fields['auth_user']= [ 
 Field('birthday', 'date'),
 Field('facebook_id', 'string'),
 Field('facebook_token', 'string'),
 Field('wizard', 'boolean'),
 ]
 
 
 JSON:
 { 
   first_name: Luciano,
   last_name: Laporta Podazza,
   country : Argentina,
   date : 2013-01-14,
   email : ma...@hotmail.com,
   province : Tucumán,
   timestamp : 2013-01-14 06:13:45,
   token : (token)
 }
 
 
 
 This was actually working and I didn't modified anything(really). Now, the 
 issue that I have is that with valid information the data isn't stored in the 
 DB and get the invalid_token2 error.
 
 Notes:
 Facebook data is valid(specially tokens), I've tested all of them externally 
 with GraphAPI and they work.
 If I comment the whole .insert I get the new_user string, which is what I'm 
 looking for. So this makes me think that something with the insert is wrong
 All tables and fields are correct, they have been tested and working before 
 this issue.
 I've tried changing database adapters(I was using mysql and now sqlite and 
 still get the same error).
 All facebook accounts are valid and verified.
 For some reason, some times it stores the data, and some times not, it's kind 
 of random. 
 Any suggestions?. Thanks in advance!
 
 -- 
  
  
  

-- 





Re: [web2py] Strange behavior storing data - web2py 2.3.2 - Mac OS X 10.7.5

2013-01-14 Thread Alan Etkin
 except:
return ivalid_token2

In order to be able to debug the error, I'd suggest to temporarily remove 
the try/except that encloses the new user insertion code, so you can access 
the error ticket created by web2py (with the actual error information 
raised by the graph library).

-- 





Re: [web2py] Strange behavior storing data - web2py 2.3.2 - Mac OS X 10.7.5

2013-01-14 Thread Massimo Di Pierro
Also notice that f you do

try:
db.table.insert()
except:
return ivalid_token2

and indeed the try fails in the insert than you must db.rollback() in the 
except, else the database remains in an invalid state.

On Monday, January 14, 2013 5:09:59 PM UTC-6, Alan Etkin wrote:

 except:
 return ivalid_token2

 In order to be able to debug the error, I'd suggest to temporarily remove 
 the try/except that encloses the new user insertion code, so you can access 
 the error ticket created by web2py (with the actual error information 
 raised by the graph library).



--