Commits

ronald martinez committed 211a0c2

render

  • Participants
  • Parent commits b5ddabc

Comments (0)

Files changed (1)

 
 class TwitterApp(RequestHandler):
 
-    def get(self):
+    def get(self, **kwargs):
+
+        status_code = None
+
+        if 'status_code' in kwargs:
+            status_code = kwargs['status_code']
+        else:
+
+            if self.get_argument('status_code', None):
+                status_code = self.get_argument('status_code')
+
+        self.render('login.html', status_code=status_code)
+
+    def post(self):
+        
+        nick = self.get_argument('nick', None)
+        password = self.get_argument('password', None)
+
+        status_code = 0
+        
+        send_data = {}
+
+        if not nick or not password:
+            status_code = 1
+        else:
+
+            #send_data['status_code'] = 1
+            #send_data['message'] = 'twitter auth failed'
+            #self.finish(send_data)
+
+            user = User.objects(
+                nick=nick,
+                password=password
+            ).first()
+
+            if user:
+                access_token = user.access_token
+                logging.info(
+                    'usuario existe con token: %s' % access_token.get('key')
+                )
+                status_code = 2
+
+            else:
+                data = cPickle.dumps({
+                    'nick': nick,
+                    'password': password
+                }, -1)
+
+                self.set_secure_cookie("twitter_user", str(data))
+                self.redirect(self.reverse_url('twitter_handler'))
+                return
+    
+        logging.info('status_code: %s' % status_code)
+        self.get(status_code=status_code)
+
+    def post2(self):
         nick = self.get_argument('nick', None)
         password = self.get_argument('password', None)
 
 
         self.authorize_redirect()
 
-    def _on_auth(self, data_user):
+    def _on_auth2(self, data_user):
+
+        status_code = 0
+
         if not data_user:
             raise tornado.web.HTTPError(500, "Twitter auth failed")
 
         return
 
 
+    def _on_auth(self, data_user):
+
+        status_code = 0
+
+        if not data_user:
+            status_code = 1
+            #raise tornado.web.HTTPError(500, "Twitter auth failed")
+
+        try:
+            data_user = data_user.get('access_token')
+            data_user_cookie = cPickle.loads(
+                self.get_secure_cookie('twitter_user')
+            )
+
+            self.clear_cookie("twitter_user")
+
+            check_account_twiiter_user = User.objects(
+                user_id=data_user.get('user_id')
+            ).first()
+
+            if check_account_twiiter_user:
+
+                logging.info("""los datos ingresados ya estan
+                 asociados a una cuenta de twitter""")
+                status_code = 2
+            else:
+                user = User()
+                user.access_token = data_user
+                user.user_id = data_user.get('user_id')
+                user.nick = data_user_cookie.get('nick')
+                user.password = data_user_cookie.get('password')
+                user.save()
+                self.write('El usuario se grabo!')
+        except Exception as exc:
+            #print "dsdsd"
+            status_code = 3
+            #self.write(str(exc))
+
+        print "OKKKKKKKKKKKKKKKKK"
+
+        self.redirect(self.reverse_url('twitter_app') + '?status_code=%s' % status_code)
+        return
+        #logging.info(status_code)
+        #self.render('login.html', status_code=status_code)
+        #TwitterApp.get(status_code=status_code)
+        #self.finish()
+        #return
+
+
 def main():
     global_settings = dict((setting.lower(), getattr(settings, setting))
         for setting in dir(settings) if setting.isupper())
             url('/retwit', Retwit),
             url('/direct_message', DirectMesaage),
             url('/auth', Auth),
-            url('/twitter_app', TwitterApp),
+            url('/twitter_app', TwitterApp, name='twitter_app'),
             url('/twitter_handler', TwitterHandler, name='twitter_handler'),
             url('/timeline', Timeline),
             url('/favorite', Favorite),