1. ronald martinez
  2. app-tweet

Commits

ronald martinez  committed 639b435

bit status_code

  • Participants
  • Parent commits 9325d28
  • Branches master

Comments (0)

Files changed (2)

File models.py

View file
     )
 
     password = me.StringField(
-        required=True,
-        min_length=4,
-        max_length=4
+        required=True
     )
 
     access_token = me.DictField()

File server.py

View file
 from tornado.web import RequestHandler, Application, asynchronous, url
 from tornado.auth import TwitterMixin
 
-from models import *
-from mongoengine import *
+from models import User
+
+import mongoengine as me
 
 
 class BaseHandler(RequestHandler):
 class TwitterApp(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 post(self):
 
+        status_code = 0
+
         nick = self.get_argument('nick', None)
         password = self.get_argument('password', None)
-        status_code = 0
+
+        logging.info('nick: %s' % nick)
+        logging.info('password: %s' % password)
 
         if not nick or not password:
             status_code = 1
+            logging.error('no nick or password')
         else:
 
             user = User.objects(
                 nick=nick,
-                password=password
+                #password=password
             ).first()
 
             if not user:
+                logging.info('no exists database')
                 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
+                user.nick = nick
+                user.password = password
 
+                try:
+                    user.save()
+                except Exception as exc:
+                    logging.error(exc)
+                    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
-
-        logging.info('status_code: %s' % status_code)
-        self.get(status_code=status_code)
+                    logging.info('El usuario se grabo!')
 
-    def post2(self):
-        nick = self.get_argument('nick', None)
-        password = self.get_argument('password', None)
+            else:
 
-        send_data = {}
+                logging.info('exists user')
 
-        if not nick or not password:
-            send_data['status_code'] = 1
-            send_data['message'] = 'twitter auth failed'
-            self.finish(send_data)
+                if user.password != password:
+                    logging.error('different password')
+                    status_code |= 4
+                else:
+                    logging.info('same password')
 
-        user = User.objects(
-            nick=nick,
-            password=password
-        ).first()
+        logging.info('status_code: %s' % status_code)
 
-        if user:
-            access_token = user.access_token
-            self.write(
-                'usuario existe con token: %s' % access_token.get('key')
-            )
-        else:
+        if status_code == 0:
             data = cPickle.dumps({
                 'nick': nick,
                 'password': password
             }, -1)
 
             self.set_secure_cookie("twitter_user", str(data))
-            self.redirect(self.reverse_url('twitter_handler'))
+            logging.info('set cookie user')
+            self.redirect(self.reverse_url('twitter_add_account'))
+            return
+
+        return
+        self.get(status_code=status_code)
 
 
 class TwitterHandler(BaseHandler, TwitterMixin):
 
         if not data_user:
             status_code = 1
+            logging.error('no data_user')
+        else:
+            data_user_cookie = self.get_secure_cookie('twitter_user')
 
-        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 not data_user_cookie:
+                status_code = 1
+                logging.error('no data cookie')
+            else:
 
-            if check_account_twiiter_user:
+                data_user = data_user.get('access_token')
+                data_user_cookie = cPickle.loads(data_user_cookie)
+                self.clear_cookie("twitter_user")
+                nick = data_user_cookie.get('nick')
+                password = data_user_cookie.get('password')
 
-                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')
-                
                 try:
-                    user.save()
+                    user = User.objects(nick=nick, password=password).first()
                 except Exception as exc:
                     logging.error(exc)
-                    status_code = 4
+                    status_code = 1
                 else:
-                    logging.info('added user')
-                    #self.write('El usuario se grabo!')
-        except Exception as exc:
-            logging.error(exc)
-            status_code = 3
-
-        self.redirect(
-            self.reverse_url('twitter_add_account') + '?status_code=%s' % status_code)
-        return
-
-    def _on_auth2(self, data_user):
-
-        if not data_user:
-            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 not user.user_id:
+                        logging.info('not relationship twitter')
+                        user.user_id = data_user.get('user_id')
+                        user.access_token = data_user
+
+                        try:
+                            user.update()
+                        except Exception as exc:
+                            logging.error(exc)
+                            status_code = 1
+                        else:
+                            logging.info('update user')
+                    else:
+                        logging.info('relationship twitter')
 
-            if check_account_twiiter_user:
-                self.write("""los datos ingresados ya estan
-                 asociados a una cuenta de twitter""")
-            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"
-            self.write(str(exc))
-
-        self.finish()
+        logging.info('status_code: %s' % status_code)
         return
+        self.redirect('%s?status_code=%s' % (
+            self.reverse_url('twitter_add_account'), status_code)
+        )
 
 
 class AddAcount(BaseHandler):