def upload454666(): # 下面这一段是通用的. result = Auth.identify(Auth, request) print(898989898989, result) if not result["status"]: return jsonify({"status": 401}) if result["status"]: username2id = result['data'] # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) print(66666, user.username) wenjianjia = user.username # # wenjianjia = user.username # # # data = request.get_json() # print(data) # print(data['filename']) # print(data['data']) # import json # with open(r"uploads/"+wenjianjia+r'//'+data['filename']+".json", 'w', encoding='utf-8') as f: # json.dump(data['data'], f, ensure_ascii=False) return r'环境保存到服务器额数据库了'
def random(): auth = Auth.identify(Auth, request) if auth['status']: t1 = request.args.get('t1') t2 = request.args.get('t2') result = [] if t1 and t2: print(123123) r = db.aggregate([{ "$match": { "t1": t1, "t2": t2 } }, { "$sample": { "size": 5 } }]) else: print('2xxx') r = db.aggregate([{"$sample": {"size": 5}}]) for i in r: i['_id'] = 1 result.append(i) return jsonify(common.trueReturn(result, 'success')) else: return auth
def get_book_count(): auth = Auth.identify(Auth, request) if auth['status']: r = db.find().count() return jsonify(common.trueReturn(r, 'success')) else: return auth
def error_predict(): parmStr = request.get_data() paramDict = json.loads(parmStr) testline = paramDict['index'] """ 获取用户信息 :return: json """ auth = Auth() result = auth.identify(request) if (result['status']): test_datas = readDataLine(testline) if test_datas != None: test_feature_datas = tf_transformer.transform(test_datas) # 进行预测 pred = model.predict(test_feature_datas) result = le.inverse_transform(pred) print ('预测结果:%s,预测内容:%s' % (result[0], testline)) #logging.debug('预测结果:%s,预测内容:%s' % (result[0], testline)) return result[0] return "this is None" else: return jsonify(result)
def get_lease_list(): auth = Auth.identify(Auth, request) if auth['status']: limit = int(request.args.get('limit')) offset = int(request.args.get('offset')) result = udb.find_one({"username": auth['data']}) if 'lease_history' in result: result = result['lease_history'][offset:limit + offset] else: result = [] lease_list = [] for i in result: if time.time() > i['end_time']: i['expired'] = True else: i['expired'] = False lease_list.append(i) udb.update_one({"username": auth['data']}, {"$set": { "lease_history": result }}) r = {"lease_list": lease_list, "total": len(result)} return jsonify(common.trueReturn(r, 'success')) else: return auth
def book_detail(book_id): auth = Auth.identify(Auth, request) if auth['status']: r = db.find_one({"book_id": book_id}, {"_id": 0}) Users.add_view_history(auth, r) Users.rate_to_book(auth, book_id, 0.1) return jsonify(common.trueReturn(r, 'success')) else: return auth
def get_recommand(): auth = Auth.identify(Auth, request) if auth['status']: r = Recommand.gen_matrix(Recommand) top_n = Recommand.get_top_n(r, 5) result = [] for i in top_n.get(auth['data']): result.append(Book.get_book_by_bookId(i[0])) return jsonify(common.trueReturn(result, 'success')) else: return auth
def get_view_history(): auth = Auth.identify(Auth, request) if auth['status']: limit = int(request.args.get('limit')) offset = int(request.args.get('offset')) result = db.find_one( {"username": auth['data']}, {"view_history": 1})['view_history'][offset:limit + offset] return jsonify(common.trueReturn(result, 'success')) else: return auth
def get_map(): auth = Auth.identify(Auth, request) if auth['status']: result = db.find_one({"username": auth['data']}) if 'rate_map' in result: return jsonify(common.trueReturn(result['rate_map'], 'success')) else: return jsonify(common.falseReturn({}, 'false')) else: pass
def check_login(): result = Auth.identify(Auth, request) if (result and result['status'] and result['data']): user_info = UserAuths.getInfo(result['data']) returnUser = { 'id': result['data'], 'username': user_info.name, 'login_time': user_info.login_time } return jsonify(common.trueReturn(returnUser, "请求成功")) else: return jsonify(common.falseReturn("", "请求失败"))
def like_book_list(): auth = Auth.identify(Auth, request) if auth['status']: result = db.find_one({"username": auth['data']}, {"like_book_list": 1}) if 'like_book_list' in result: return jsonify( common.trueReturn(result['like_book_list'], 'success')) else: return jsonify(common.trueReturn([], 'success')) else: return auth
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
def upload454666888(): # 下面这一段是通用的. result = Auth.identify(Auth, request) print(898989898989, result) if not result["status"]: return jsonify({"status": 401}) if result["status"]: username2id = result['data'] # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) print(66666, user.username) wenjianjia = user.username username2id = result['data'] # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) # result['data'] 表示的是用户id号. print(66666, user.username) print(66666, user.param1) # 把所有需要保存的文件名放入param1里面 print(type(user.param1)) import json a = json.loads((user.param1)) # 通过loads读取json字符串 print(888888, a) fanhui = {} print("zonggiongshi j", a) for i in a: iii = r"uploads/" + user.username + r'/' + i + ".json" print(iii) with open(iii, encoding='utf-8') as i2: import json tmp = json.load(i2) fanhui[i] = tmp print(i) print(len(fanhui)) print(fanhui.keys()) print(fanhui['古文观止2']) # # wenjianjia = user.username # # # data = request.get_json() # print(data) # print(data['filename']) # print(data['data']) # import json # with open(r"uploads/"+wenjianjia+r'//'+data['filename']+".json", 'w', encoding='utf-8') as f: # json.dump(data['data'], f, ensure_ascii=False) return jsonify(fanhui)
def user_detail(): auth = Auth.identify(Auth, request) if auth['status']: result = db.find_one({"username": auth['data']}, {"_id": 0}) if request.method == 'POST': return jsonify(common.trueReturn(result, 'success')) elif request.method == 'PATCH': for i in request.json: if request.json[i]: Users.update_user(auth['data'], i, request.json[i]) return jsonify(common.trueReturn('1', 'success')) else: return auth
def get_book_type(): auth = Auth.identify(Auth, request) if auth['status']: r = db.find({}, {"t1": 1, "_id": 0}) book_cnt = {} for i in r: if i['t1'] in book_cnt.keys(): book_cnt[i['t1']] += 1 else: book_cnt[i['t1']] = 1 return jsonify(common.trueReturn(book_cnt, 'success')) else: return auth
def get(): """ 获取用户信息 :return: json """ result = Auth.identify(Auth, request) if result['status'] and result['data']: user = Users.get(Users, result['data']) return_user = { 'id': user.id, 'username': user.username, 'email': user.email, 'login_time': user.login_time } result = common.resp(return_user, "请求成功") return jsonify(result)
def get(): """ 获取用户信息 :return: json """ auth = Auth() result = auth.identify(request) if (result['status'] and result['data']): user = Users.query.filter_by(id=result['data']).first() returnUser = { 'id': user.id, 'user_name': user.user_name, 'email': user.email } result = common.trueReturn(returnUser, "请求成功") return jsonify(result)
def back_book(): auth = Auth.identify(Auth, request) if auth['status']: book_name = request.json.get('book_name') result = udb.find_one({"username": auth['data']}, { "_id": 0, "lease_history": 1 })['lease_history'] for i in result: if i['book']['book_name'] == book_name and i['valid']: i['valid'] = False udb.update_one({"username": auth['data']}, {"$set": { "lease_history": result }}) return jsonify(common.trueReturn(result, 'success')) else: return auth
def user_img(): auth = Auth.identify(Auth, request) if auth['status']: result = db.find_one({"username": auth['data']}, { "_id": 0, "rate_map": 1 })['rate_map'] map = {} for i in result: book = Book.get_book_by_bookId(i) body = {"t1": book['t1'], "value": float(result[i])} if book['t2'] in map: map[book['t2']]['value'] += body['value'] else: map[book['t2']] = body return jsonify(common.trueReturn(map, 'success')) else: return auth
def upload222222223(): # 下面这一段是通用的. result = Auth.identify(Auth, request) print(898989898989, result) if not result["status"]: return jsonify({"status": 401}) if result["status"]: username2id = result['data'] beidelete = request.args['delete'] # 获取参数的方法 # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) print(66666, user.username) wenjianjia = user.username if not os.path.exists(r"uploads/" + wenjianjia): return jsonify([]) os.remove(r"uploads/" + wenjianjia + '/' + str(beidelete)) tmp = os.listdir(r"uploads/" + wenjianjia) tmp2 = [] for i in tmp: tmp2.append(i.split('.')[-2]) print(tmp2) # name2=request.args['file'] # 获取参数的方法 # print(111111111111111111111111,name2) # 为什么目录跳了一级??? # return send_file(r"..\uploads\fuck222.png") # return send_file(r"..\uploads\1.txt") # attachment_filename the filename for the attachment if it differs from the file’s filename. # # 可以了,上传下载都可以了 # with open(r"uploads/"+wenjianjia+r'/'+name2+".json",encoding='utf-8') as f: # # tmp=json.load(f) # print(tmp) # print(type(tmp),343434343) # tmp2={'data':tmp} # # tmp2=jsonify(tmp2) # print(9999999999) # print(tmp2['data'][0]) return jsonify(tmp2)
def upload4422(): # 下面这一段是通用的. result = Auth.identify(Auth, request) print(898989898989, result) if not result["status"]: return jsonify({"status": 401}) if result["status"]: username2id = result['data'] # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) print(66666, user.username) wenjianjia = user.username + "_juzifenxi" # 这里规定带有这个后缀的是句子,不带的是篇章 f = request.files #http://www.voidcn.com/article/p-hfxcccsv-btt.html print(7777777777777, f) print(7777777777777, f.values()) k = f.getlist('file') changdu = len(k) print(k) num = 0 for i in k: print(i) print(type(i)) # 先获取文件的名字. print(1111111111111, i.filename) name2 = i.filename.split('.')[-2] + '.json' basepath = os.path.dirname(__file__) # 当前文件所在路径 upload_path = os.path.join(basepath, '/uploads') #注意:没有的文件夹一定要先创建,不然会提示没有该路径 if not os.path.exists(r"uploads/" + wenjianjia): os.mkdir(r"uploads/" + wenjianjia) pa34 = r"uploads/" + wenjianjia + r"/" + name2 # i.save(pa34) # 带字符串的一定要前面写上r.这样少了很多转义字符,方便多了!!!!!!!!!!!!!11 pageToSentences(i, pa34) # 这里面有保存代码 print("句子解析的结果!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") print("保存在", pa34) print(name2) return "成功完成" + str(changdu) + "个文件的句子对齐任务" return "bug"
def get_all_book(): auth = Auth.identify(Auth, request) if auth['status']: limit = int(request.args.get('limit')) offset = int(request.args.get('offset')) search = request.args.get('book_name') type = request.args.get('book_type') if search: if type: r = db.find({ "book_name": { "$regex": search }, "t1": type }, { "_id": 0 }).limit(limit).skip(offset) else: r = db.find({ "book_name": { "$regex": search } }, { "_id": 0 }).limit(limit).skip(offset) else: if type: r = db.find({ "t1": type }, { "_id": 0 }).limit(limit).skip(offset) else: r = db.find({}, {"_id": 0}).limit(limit).skip(offset) book_list = [] cnt = r.count() for i in r: book_list.append(i) result = {'book': book_list, 'total': cnt} return jsonify(common.trueReturn(result, 'success')) else: return auth
def upload454(): # 下面这一段是通用的. result = Auth.identify(Auth, request) print(898989898989, result) if not result["status"]: return jsonify({"status": 401}) if result["status"]: username2id = result['data'] # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) print(66666, user.username) wenjianjia = user.username data = request.get_json() print(data) print(data['filename']) print(data['data']) print(data['page']) import json # print(json.load(r"uploads/"+wenjianjia+r'/'+data['filename']+".json",ensure_ascii=False)) print(99999999999999999999999999999) with open(r"uploads/" + wenjianjia + r'/' + data['filename'] + ".json", 'r', encoding='utf-8') as f: # print(f) tmp = json.load(f) # print(8888888888,tmp) # json.dump(data['data'], f, ensure_ascii=False) print(999999999333333333333333) tmp[data['page'] - 1] = data['data'] # print(tmp[data['page']]) with open(r"uploads/" + wenjianjia + r'/' + data['filename'] + ".json", 'w', encoding='utf-8') as f: json.dump(tmp, f, ensure_ascii=False) return r'json保存到服务器了'
def upload2(): # 下面这一段是通用的. result = Auth.identify(Auth, request) print(898989898989, result) if not result["status"]: return jsonify({"status": 401}) if result["status"]: username2id = result['data'] # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) print(66666, user.username) wenjianjia = user.username name2 = request.args['file'] # 获取参数的方法 print(111111111111111111111111, name2) # 为什么目录跳了一级??? # return send_file(r"..\uploads\fuck222.png") # return send_file(r"..\uploads\1.txt") # attachment_filename the filename for the attachment if it differs from the file’s filename. # 可以了,上传下载都可以了 path = r"../uploads/" + wenjianjia + "/" + name2 + ".json" path2 = os.path.abspath(path) print(path2) # return jsonify(path2) tmp = send_from_directory(r"../uploads/" + wenjianjia, filename=name2 + ".json", as_attachment=True) print(tmp) print(type(tmp)) print(tmp.headers) # 下面这段是修改header的方法.!!!!!!!!!!!!!!!!!!!!!! #<class 'werkzeug.datastructures.Headers'> 看源码 print(type(tmp.headers)) print("45645645", tmp.headers) tmp.headers.add('Content-Type', 'application/octet-stream') # tmp.add_header('Authorization', 'Bearer {xxxxxxx}') # tmp.header['content-type']='application/octet-stream' return tmp
def like_book(): auth = Auth.identify(Auth, request) if auth['status']: book_name = request.json.get('book_name') book_id = Book.get_bookId_by_bookName(book_name) like = request.json.get('like') result = db.find_one({"username": auth['data']}) if like: if 'like_book_list' in result: if book_name in result['like_book_list']: return jsonify(common.falseReturn('已经喜欢过该书', 'false')) else: result['like_book_list'].append(book_name) db.update_one({"username": auth['data']}, { "$set": { "like_book_list": result['like_book_list'] } }) Users.rate_to_book(auth, book_id, 0.5) return jsonify(common.trueReturn('成功', 'true')) else: like_book_list = [] like_book_list.append(book_name) db.update_one({"username": auth['data']}, {"$set": { "like_book_list": like_book_list }}) Users.rate_to_book(auth, book_id, 0.5) return jsonify(common.trueReturn('成功', 'true')) else: result['like_book_list'].remove(book_name) db.update_one( {"username": auth['data']}, {"$set": { "like_book_list": result['like_book_list'] }}) Users.rate_to_book(auth, book_id, -0.5) return jsonify(common.trueReturn('成功', 'true')) else: return auth
def upload2222(): # 下面这一段是通用的. result = Auth.identify(Auth, request) print(898989898989, result) if not result["status"]: return jsonify({"status": 401}) if result["status"]: username2id = result['data'] # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) print(66666, user.username) wenjianjia = user.username name2 = request.args['file'] # 获取参数的方法 page2 = request.args['page'] # 获取参数的方法 page2 = int(page2) print(111111111111111111111111, name2) # 为什么目录跳了一级??? # return send_file(r"..\uploads\fuck222.png") # return send_file(r"..\uploads\1.txt") # attachment_filename the filename for the attachment if it differs from the file’s filename. # 可以了,上传下载都可以了 with open(r"uploads/" + wenjianjia + r'/' + name2 + ".json", encoding='utf-8') as f: tmp = json.load(f) # print("111111111111111111111111",tmp) # print(22222222222222,page2) # print(tmp[page2-1]) tmp3 = tmp[page2 - 1] # print(tmp) # print(type(tmp),343434343) # tmp2={'data':tmp} # # tmp2=jsonify(tmp2) # print(9999999999) # print(tmp2['data'][0]) print(type(tmp3)) tmp3['totalpage'] = len(tmp) return jsonify(tmp3)
def upload2223(): # 下面这一段是通用的. result = Auth.identify(Auth, request) print(898989898989, result) if not result["status"]: return jsonify({"status": 401}) if result["status"]: username2id = result['data'] # 因为是类函数所以第一个参数传入这个类他自己. user = Users.get(Users, result['data']) print(66666, user.username) wenjianjia = user.username + "_juzifenxi" # 这里规定带有这个后缀的是句子,不带的是篇章 name2 = request.args['file'] # 获取参数的方法 print(111111111111111111111111, name2) # 为什么目录跳了一级??? # return send_file(r"..\uploads\fuck222.png") # return send_file(r"..\uploads\1.txt") # attachment_filename the filename for the attachment if it differs from the file’s filename. tmp = send_from_directory(r"../uploads/" + wenjianjia, filename=name2 + ".json", as_attachment=True) print(tmp) print(type(tmp)) print(tmp.headers) # 下面这段是修改header的方法.!!!!!!!!!!!!!!!!!!!!!! #<class 'werkzeug.datastructures.Headers'> 看源码 print(type(tmp.headers)) print("45645645", tmp.headers) tmp.headers.add('Content-Type', 'application/octet-stream') return tmp # 可以了,上传下载都可以了 return send_from_directory(r"../uploads/" + wenjianjia, filename=name2 + ".json", as_attachment=True)
def before_request(): Auth.identify(Auth, request)