Commits

ronald martinez  committed e0aaaae

ok

  • Participants
  • Parent commits 91d74c9
  • Branches dev2

Comments (0)

Files changed (1)

File controllers/user.py

 from models import User
 from controllers import Session, BaseHandler
 
-from tornado.web import HTTPError, authenticated
+from tornado.web import HTTPError, authenticated, asynchronous
 
 import datetime
 
+from tornado import gen
+
+import time
+
+
+#@gen.engine
+def suma(x, y, callback):
+    print "xxxxxxxxxxxxxxxxxxxxxxx"
+    logging.error('xxxxxxxxxxx')
+
+    print callback
+    return x + y
+
+
+def t():
+
+    print 'xxx'
 
 def get_age(date_of_birth):
 
 
 class Index(BaseHandler):
 
+    #def update_user(self, user_data_facebook, callback=yield gen.Callback('user')):
+
+    def update_user(self, user_data_facebook):
+
+        #time.sleep(1)
+        #print "wntro"
+
+        user = Session.query(User).filter_by(
+            fbid=user_data_facebook.get('user_id'))
+
+        if not user.first():
+
+            params = dict(
+                access_token=user_data_facebook.get('oauth_token'),
+                fields='email,birthday,gender,name'
+            )
+
+            user_data_facebook_graph = self.load_data_url(
+                url='%s/%s' % (settings.FACEBOOK_GRAPH,
+                    user_data_facebook.get('user_id')),
+                params=params,
+                json=True
+            )
+
+            user = User()
+            user.name = user_data_facebook_graph.get('name')
+            user.fbid = user_data_facebook.get('user_id')
+            user.email = user_data_facebook_graph.get('email')
+            user.access_token = user_data_facebook.get('oauth_token')
+
+            if 'gender' in user_data_facebook_graph:
+                user.gender = user_data_facebook_graph.get('gender')
+
+            if 'birthday' in user_data_facebook_graph:
+
+                user_age = get_age(
+                    user_data_facebook_graph.get('birthday'))
+
+                if user_age:
+                    user.age = user_age
+
+            Session.add(user)
+
+            try:
+                Session.commit()
+            except Exception as exc:
+                logging.error(exc)
+            else:
+                logging.info('save user')
+
+        else:
+
+            try:
+                user.update({'access_token': user_data_facebook.get(
+                    'oauth_token')})
+            except Exception as exc:
+                logging.error(exc)
+            else:
+                logging.info('update user token')
+
     def check_xsrf_cookie(self):
         pass
 
+    @asynchronous
+    @gen.engine
+    #@gen.engine
     def post(self):
 
+        print "posttt"
+
         signed_request = self.get_argument('signed_request', None)
 
         if not signed_request:
 
             self.set_secure_cookie('user', data)
 
-            user = Session.query(User).filter_by(
-                fbid=user_data_facebook.get('user_id'))
-
-            if not user.first():
-
-                params = dict(
-                    access_token=user_data_facebook.get('oauth_token'),
-                    fields='email,birthday,gender,name'
-                )
+            print "a"
 
-                user_data_facebook_graph = self.load_data_url(
-                    url='%s/%s' % (settings.FACEBOOK_GRAPH,
-                        user_data_facebook.get('user_id')),
-                    params=params,
-                    json=True
-                )
+            #result = yield gen.Task(self.update_user, user_data_facebook)
+            #result = yield gen.Task(self.update_user, user_data_facebook)
+            result = yield gen.Task(suma, 1, 2, callback=t)
 
-                user = User()
-                user.name = user_data_facebook_graph.get('name')
-                user.fbid = user_data_facebook.get('user_id')
-                user.email = user_data_facebook_graph.get('email')
-                user.access_token = user_data_facebook.get('oauth_token')
+            #print result
 
-                if 'gender' in user_data_facebook_graph:
-                    user.gender = user_data_facebook_graph.get('gender')
 
-                if 'birthday' in user_data_facebook_graph:
 
-                    user_age = get_age(
-                        user_data_facebook_graph.get('birthday'))
+            #self.update_user(user_data_facebook)
 
-                    if user_age:
-                        user.age = user_age
+            print "b"
 
-                Session.add(user)
 
-                try:
-                    Session.commit()
-                except Exception as exc:
-                    logging.error(exc)
-                else:
-                    logging.info('save user')
-
-            else:
+            self.redirect(self.reverse_url('list_friends'))
 
-                try:
-                    user.update({'access_token': user_data_facebook.get(
-                        'oauth_token')})
-                except Exception as exc:
-                    logging.error(exc)
-                else:
-                    logging.info('update user token')
+    def _ona(self):
 
-            self.redirect(self.reverse_url('list_friends'))
+        print "cccccccccccccccccc"
 
 
 class ListFriends(BaseHandler):