ronald martinez avatar ronald martinez committed 6cadae0

ok

Comments (0)

Files changed (1)

controllers/user.py

 import settings
 import simplejson
 import cPickle
+import datetime
 
 from models import User
-from controllers import Session, BaseHandler
-
-from tornado.web import HTTPError, authenticated, asynchronous
-
-import datetime
 
 from tornado import gen
+from controllers import Session, BaseHandler
+from tornado.web import HTTPError, asynchronous
+from tornado.httpclient import AsyncHTTPClient
 
-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):
 
         date_of_birth = datetime.datetime.strptime(
             date_of_birth, '%d/%m/%Y').date()
 
-        print 'c', date_of_birth
-
         if date_of_birth > datetime.date.today().replace(
                 year=date_of_birth.year):
             tmp = datetime.date.today().year - date_of_birth.year - 1
             tmp = datetime.date.today().year - date_of_birth.year
 
     except Exception as exc:
-
         logging.error(exc)
 
     return tmp
 
 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)
 
-            print "a"
-
-            #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 = Session.query(User).filter_by(
+                fbid=user_data_facebook.get('user_id'))
 
-            #print result
+            if not user.first():
 
+                params = dict(
+                    access_token=user_data_facebook.get('oauth_token'),
+                    fields='email,birthday,gender,name'
+                )
 
+                url = '%s?%s' % (
+                    '%s/%s' % (
+                        settings.FACEBOOK_GRAPH,
+                        user_data_facebook.get('user_id')
+                    ),
+                     urllib.urlencode(params)
+                )
 
-            #self.update_user(user_data_facebook)
+                http_client = AsyncHTTPClient()
+                http_client.fetch(url, self.on_update_user)
 
-            print "b"
+            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')
 
             self.redirect(self.reverse_url('list_friends'))
 
-    def _ona(self):
+    def on_update_user(self, user_data_facebook_graph):
+
+        if user_data_facebook_graph.error:
+            raise HTTPError(500)
+        else:
+
+            user_data_facebook_graph = simplejson.loads(
+                user_data_facebook_graph.body)
 
-        print "cccccccccccccccccc"
+            cookie = self.get_secure_cookie('user')
+            cookie_user = cPickle.loads(cookie) if cookie else None
+
+            if not cookie_user:
+                raise HTTPError(500)
+
+            user = User()
+            user.name = user_data_facebook_graph.get('name')
+            user.fbid = cookie_user.get('user_id')
+            user.email = user_data_facebook_graph.get('email')
+            user.access_token = cookie_user.get('access_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')
 
 
 class ListFriends(BaseHandler):
 
-    @authenticated
     def get(self):
 
+        cookie = self.get_secure_cookie('user')
+        cookie_user = cPickle.loads(cookie) if cookie else None
+
+        if not cookie_user:
+            raise HTTPError(500)
+
         limit, offset = self.pagination(
             settings.PAGES_LIMIT,
             self.get_argument('page', '1')
         )
 
         params = dict(
-            access_token=self.current_user.access_token,
+            access_token=cookie_user.get('access_token'),
             fields='name',
             limit=limit,
             offset=offset
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.