Exemple #1
0
    def post(self):
        new_info = self.get_data()
        if 'phone' in new_info and 'password' in new_info:
            user = User.query.get_by_phone(new_info['phone'])
            if user and user.authenticate(new_info['password']):
                user.token = uuid.uuid5(uuid.NAMESPACE_URL, "%s%s" % (user.phone,
                    options.token_secret)).hex

                info = user.user_to_dict_by_auth() # must before save
		key = 'users:%s' % user.token
                self.rdb.set(key, json_encode(user.user2dict4redis()))
                self.rdb.expire(key, 3600)

                user.save()

                self.render_json(info) 
                return 
            else:
                self.set_status(406)
                msg = self.message("Password or phone is not correct.")
        else:
            self.set_status(400)
            msg = self.message("phone, password field required.")

        self.render_json(msg)
Exemple #2
0
    def get_current_user(self):
        tk = self.get_argument('tk')
        key = 'users:%s' % tk

        if self.is_available_client():
            #return User.query.get_by_token(tk)
            user_str = self.rdb.get(key)
            user_dict = user_str and json_decode(user_str)
            if not user_dict:
                user = User.query.get_by_token(tk)
		# FIXME bug# user object must be use (liek: user.id etc..) 
		# the user.user2dict4redis method can work, I don't know why
                if user and user.id > 0:
		    #print >> sys.stderr, user
                    user_dict = user.user2dict4redis()
		    #print >> sys.stderr, user_dict
                    self.rdb.set(key, json_encode(user_dict))
                    self.rdb.expire(key, 3600)

            if user_dict:
                return QDict(user_dict)

        return None
Exemple #3
0
 def json(self, data):
     return json_encode(data)
Exemple #4
0
 def json(self, data):
     dthandler = lambda obj: obj.isoformat() if isinstance(obj, (datetime.datetime, datetime.date)) else obj
     return json_encode(data, default=dthandler)
Exemple #5
0
 def json_encode(self, v):
     return _es.json_encode(v)
Exemple #6
0
 def json_encode(self, data):
     return _es.json_encode(data)
Exemple #7
0
 def render_json(self, data, **kwargs):
     ''' Render data string(json) for response.
     '''
     self.set_header('Content-Type', 'Application/json; charset=UTF-8')
     self.write(json_encode(data))
Exemple #8
0
 def json_encode(self, v):
     return _es.json_encode(v)
Exemple #9
0
 def json_encode(self, data):
     return _es.json_encode(data)