Commits

ronald martinez committed 4412351

add account tpl

  • Participants
  • Parent commits 0183d79

Comments (0)

Files changed (1)

 from mongoengine import *
 
 
-class Favorite(RequestHandler, TwitterMixin):
+class BaseHandler(RequestHandler):
+
+    def __init__(self, *args, **kwargs):
+        super(BaseHandler, self).__init__(*args, **kwargs)
+
+    def render_string(self, template, **kwargs):
+        kwargs.update({'handler': self})
+        return self.settings.get('template_env')\
+            .get_template(template).render(**kwargs)
+
+    def render(self, template, **kwargs):
+        self.finish(self.render_string(template, **kwargs))
+
+
+class Favorite(BaseHandler, TwitterMixin):
 
     @asynchronous
     def get(self):
         self.finish(dict(status_code=0))
 
 
-class DirectMesaage(RequestHandler, TwitterMixin):
+class DirectMesaage(BaseHandler, TwitterMixin):
 
     @asynchronous
     def get(self):
         self.finish("send direct message!")
 
 
-class Timeline(RequestHandler, TwitterMixin):
+class Timeline(BaseHandler, TwitterMixin):
 
     @asynchronous
     def get(self):
         self.finish(send_data)
 
 
-class Retwit(RequestHandler, TwitterMixin):
+class Retwit(BaseHandler, TwitterMixin):
 
     @asynchronous
     def get(self):
         self.finish(dict(status_code=0))
 
 
-class Twit(RequestHandler, TwitterMixin):
+class Twit(BaseHandler, TwitterMixin):
 
     @asynchronous
     def get(self):
         self.finish(send_data)
 
 
-class Auth(RequestHandler):
+class Auth(BaseHandler):
 
     def get(self):
         nick = self.get_argument('nick', None)
             self.finish(send_data)
 
 
-class TwitterApp(RequestHandler):
+class AddAcount(BaseHandler):
+
+    def get(self):
+
+        status_code = None
+
+        if self.get_argument('status_code', None):
+            status_code = self.get_argument('status_code')
+
+        self.render('site/add_account.jade', status_code=status_code)
+
+
+class TwitterApp(BaseHandler):
 
     def get(self, **kwargs):
 
         self.render('site/index.jade', 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
+            if not user:
+                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()
+                logging.info('El usuario se grabo!')
+
+                """
+                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_add_account'))
+            return
 
-            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)
 
             self.redirect(self.reverse_url('twitter_handler'))
 
 
-class TwitterHandler(RequestHandler, TwitterMixin):
+class TwitterHandler(BaseHandler, TwitterMixin):
 
     @asynchronous
     def get(self):
 
         self.authorize_redirect()
 
-    def _on_auth2(self, data_user):
+    def _on_auth(self, data_user):
 
         status_code = 0
 
         if not data_user:
-            raise tornado.web.HTTPError(500, "Twitter auth failed")
+            status_code = 1
 
         try:
             data_user = data_user.get('access_token')
             ).first()
 
             if check_account_twiiter_user:
-                self.write("""los datos ingresados ya estan
+
+                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!')
+                
+                try:
+                    user.save()
+                except Exception as exc:
+                    logging.error(exc)
+                    status_code = 4
+                else:
+                    logging.info('added user')
+                    #self.write('El usuario se grabo!')
         except Exception as exc:
-            #print "dsdsd"
-            self.write(str(exc))
+            logging.error(exc)
+            status_code = 3
 
-        self.finish()
+        self.redirect(
+            self.reverse_url('twitter_add_account') + '?status_code=%s' % status_code)
         return
 
-
-    def _on_auth(self, data_user):
-
-        status_code = 0
+    def _on_auth2(self, data_user):
 
         if not data_user:
-            status_code = 1
-            #raise tornado.web.HTTPError(500, "Twitter auth failed")
+            raise tornado.web.HTTPError(500, "Twitter auth failed")
 
         try:
             data_user = data_user.get('access_token')
             ).first()
 
             if check_account_twiiter_user:
-
-                logging.info("""los datos ingresados ya estan
+                self.write("""los datos ingresados ya estan
                  asociados a una cuenta de twitter""")
-                status_code = 2
             else:
                 user = User()
                 user.access_token = data_user
                 self.write('El usuario se grabo!')
         except Exception as exc:
             #print "dsdsd"
-            status_code = 3
-            #self.write(str(exc))
-
-        print "OKKKKKKKKKKKKKKKKK"
+            self.write(str(exc))
 
-        self.redirect(self.reverse_url('twitter_app') + '?status_code=%s' % status_code)
+        self.finish()
         return
-        #logging.info(status_code)
-        #self.render('login.html', status_code=status_code)
-        #TwitterApp.get(status_code=status_code)
-        #self.finish()
-        #return
+
+
+class AddAcount(BaseHandler):
+
+    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('site/index.jade', status_code=status_code)
 
 
 def main():
             url('/retwit', Retwit),
             url('/direct_message', DirectMesaage),
             url('/auth', Auth),
-            url('/twitter_app', TwitterApp, name='twitter_app'),
             url('/twitter_handler', TwitterHandler, name='twitter_handler'),
             url('/timeline', Timeline),
             url('/favorite', Favorite),
+            url('/', TwitterApp, name='twitter_app'),
+            url('/add_account', AddAcount, name='twitter_add_account'),
         ), **global_settings
     )