Exemple #1
0
 def lease():
     auth = Auth.identify(Auth, request)
     if auth['status']:
         duration = int(request.json.get('duration'))
         book_name = request.json.get('book_name')
         current_time = int(time.time() * 1000)
         end_time = 2592000000 * duration + current_time
         book_id = Book.get_bookId_by_bookName(book_name)
         book = db.find_one({"book_name": book_name}, {"_id": 0})
         user_info = Users.find_by_username(auth['data'])[0]
         if 'lease_history' in user_info:
             lease_history_list = []
             for i in user_info['lease_history']:
                 if i['end_time'] > current_time and i['valid']:
                     lease_history_list.append(i['book']['book_id'])
                 else:
                     pass
             if book_id in lease_history_list:
                 return jsonify(common.falseReturn('您正在借此书', '失败'))
             else:
                 result = udb.find_one({"username":
                                        auth['data']})['lease_history']
                 book_obj = {
                     "book": book,
                     "lease_time": current_time,
                     "end_time": end_time,
                     "valid": True
                 }
                 result.insert(0, book_obj)
                 udb.update_one({"username": auth['data']},
                                {"$set": {
                                    "lease_history": result
                                }})
                 Users.rate_to_book(auth, book_id, 2)
                 return jsonify(common.trueReturn(result, 'success'))
         else:
             result = []
             book_obj = {
                 "book": book,
                 "lease_time": current_time,
                 "end_time": end_time,
                 "valid": True
             }
             result.append(book_obj)
             udb.update_one({"username": auth['data']},
                            {"$set": {
                                "lease_history": result
                            }})
             Users.rate_to_book(auth, book_id, 2)
             return jsonify(common.trueReturn(result, 'success'))
     else:
         return auth
Exemple #2
0
 def login():
     username = request.json.get('username')
     password = request.json.get('password')
     if (not username or not password):
         return jsonify(common.falseReturn('', '用户名和密码不能为空'))
     else:
         user_info = Users.find_by_username(username)
         if user_info.count() != 0 and Users.check_password(
                 Users, user_info[0]['password'], password):
             login_tiem = int(time.time())
             token = Auth.authenticate(Auth, username, login_tiem)
             return jsonify(common.trueReturn('成功', token))
         else:
             return jsonify(common.falseReturn('失败', '用户名或密码错误'))
Exemple #3
0
 def identify(self, request):
     """
     用户鉴权
     :return: list
     """
     auth_header = request.headers.get('Authorization')
     payload = self.decode_auth_token(auth_header)
     user = Users.find_by_username(payload['data']['username'])
     if user.count() == 0:
         result = common.falseReturn('', '找不到该用户信息')
     else:
         result = common.trueReturn(user[0]['username'], '请求成功')
     print(result)
     return result
Exemple #4
0
 def reagister():
     """
     用户注册
     :return:json
     """
     username = request.form.get('username')
     password = request.form.get('password')
     if len(username) < 5:
         return jsonify(common.falseReturn('fail', '用户名过短'))
     if len(password) < 5:
         return jsonify(common.falseReturn('fail', '密码过短'))
     user_info = Users.find_by_username(username)
     if user_info.count() == 0:
         user = Users(username, Users.set_password(Users, password))
         result = Users.add_user(user)
         return jsonify(common.trueReturn('成功', '注册成功'))
     else:
         return jsonify(common.falseReturn('失败', '用户名重复'))