def register(): if not request.json: resultDict = returnNoneMsg("failed!") return jsonify(resultDict) jsonData = request.get_data() dataDict = json.loads(jsonData) name = dataDict.get('name', None) password = dataDict.get('password', None) email = dataDict.get('email', None) phone = dataDict.get('phone', None) user_info = User.query.filter_by(user_name=name).first() if user_info: resultDict = returnNoneMsg(cec.code_1) return jsonify(resultDict) user = User(user_name=name, email=email, phone=phone, user_password=generate_password_hash(password), user_reg_ip=request.remote_addr) db.session.add(user) db.session.commit() resultDict = returnMsg("register success") return jsonify(resultDict)
def addArticle(): if not request.json: resultDict = returnNoneMsg("failed!") return jsonify(resultDict) jsonData = request.get_data() dataDict = json.loads(jsonData) article = Article() article.title = dataDict.get('title', None) article.author = dataDict.get('author', None) article.content = dataDict.get('content', None) article.category = dataDict.get('category', None) article.point_num = dataDict.get('point_num', None) article.commont_num = dataDict.get('commont_num', None) article.fav_num = dataDict.get('fav_num', None) article.add_ip = request.remote_addr article.published = True res = article.save() if res: returnId = { 'id': article.meta.id, } resultDict = returnMsg(returnId) return jsonify(resultDict) else: resultDict = returnNoneMsg("add failed!") return jsonify(resultDict)
def deleteArticle(id=None): if id is None: resultDict = returnNoneMsg("please article id!") return jsonify(resultDict) article_id = id article = Article() try: article.get(id=article_id).delete() resultDict = returnMsg("delete success") except Exception, e: resultDict = returnNoneMsg("id not exists,delete error")
def updateArticle(): if not request.json: resultDict = returnNoneMsg("failed!") return jsonify(resultDict) jsonData = request.get_data() dataDict = json.loads(jsonData) id = dataDict.get('id', None) dataDict.pop("id") article = Article() try: article.get(id=id).update(**dataDict) resultDict = returnMsg("update success") except Exception, e: resultDict = returnNoneMsg("id not exists,update error")
def login(): if not request.json: resultDict = returnNoneMsg("failed!") return jsonify(resultDict) jsonData = request.get_data() dataDict = json.loads(jsonData) name = dataDict.get("name", None) password = dataDict.get("password", None) if name and password: user = User.query.filter_by(user_name=name).first() if not user: resultDict = returnNoneMsg("user not exists") return jsonify(resultDict) if not user.check_pwd(password): resultDict = returnNoneMsg("Password error") return jsonify(resultDict) else: resultDict = returnNoneMsg("Bad counselorName or counselorPwd") return jsonify(resultDict) if user: user_id = user.id userAccessToken = create_access_token(identity=user_id) userRefreshToken = create_refresh_token(identity=user_id) accessJti = get_jti(userAccessToken) refreshJti = get_jti(userRefreshToken) user.user_token = accessJti user.user_reflesh_token = refreshJti db.session.add(user) # 提交修改 db.session.flush() db.session.commit() dictInfo = { "userId": user_id, "accessToken": userAccessToken, "refreshToken": userRefreshToken, } resultDict = returnMsg(dictInfo) else: resultDict = returnNoneMsg("this user not exit") return jsonify(resultDict)
def get(): """ 获取用户信息 :return: json """ adminId = get_jwt_identity() user = User.query.filter_by(id=adminId).first() if user: returnUser = { 'id': user.id, 'username': user.user_name, 'email': user.email, 'phone': user.phone, 'ip_addr': user.user_reg_ip, # 'addtime':user.addtime } resultDict = returnMsg(returnUser) else: resultDict = returnNoneMsg("user not exists") return jsonify(resultDict)
def searchArticle(k=None): if not request.json: resultDict = returnNoneMsg("failed!") return jsonify(resultDict) jsonData = request.get_data() dataDict = json.loads(jsonData) title = dataDict.get('title', None) author = dataDict.get('author', None) article = Article() a = article.search() if title: a = a.filter('term', published=True).query('match', title=title) else: a = a.filter('term', published=True).query('match', author=author) results = a.execute() if results: infoList = [] for messageTable in results: dictInfo = { "id": messageTable._id, "title": messageTable.title, "author": messageTable.author, "content": messageTable.content, "add_ip": messageTable.add_ip, "category": messageTable.category, "point_num": messageTable.point_num, "commont_num": messageTable.commont_num, "fav_num": messageTable.fav_num, } infoList.append(dictInfo) resultDict = returnMsg(infoList) else: resultDict = returnNoneMsg("the message not exit!") return jsonify(resultDict)