def register(): try: result = {'response': 'success', 'info': ''} name = request.json.get("name", None) password = request.json.get("password", None) email = request.json.get("email", None) if None in [name, password, email]: result.update({"response": "fail", "info": "请求参数错误"}) return jsonify(result) en_password = AESCipher.encrypt(password), with get_session() as db_session: u = db_session.query(User).filter( or_(User.name == name, User.email == email)).first() if u: result.update({"response": "fail", "info": "当前用户已存在"}) return jsonify(result) else: u = User() u.name = name u.password = en_password u.email = email db_session.add(u) db_session.commit() return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def upload_image(cls, img_obj): with get_session() as db_session: img_id = str(IdGenerator.gen()) path_src = datetime.datetime.now().strftime('%Y-%m-%d') upload_src = os.path.join(UPLOAD_SRC, path_src) if not os.path.exists(upload_src): os.makedirs(upload_src) im = Img.open(img_obj) width, height = im.size file_format = im.format file_name = '.'.join([img_id, file_format.lower()]) file_full_path = os.path.join(upload_src, file_name).replace('\\', '/') im.save(file_full_path) # 数据库存储 img = Image() img.id = img_id img.name = file_name img.width = width img.height = height img.path = path_src img.status = cls.NORMAL db_session.add(img) img_dict = img.to_dict() img_dict["src"] = os.path.join(path_src, file_name).replace('\\', '/') db_session.commit() return img_dict
def login(): try: result = {'response': 'success', "data": "", 'info': ''} token_id = request.json.get("tokenId", None) password = request.json.get("password", None) email = request.json.get("email", None) captcha = request.json.get("captcha", None) if None in [password, email, captcha]: result.update({"response": "fail", "info": "请求参数错误"}) return jsonify(result) store_captcha = redis_db.get(get_token_id_key(token_id)) if not store_captcha: result.update({"response": "fail", "info": "验证码超时,请重新获取验证码"}) return jsonify(result) en_store_captcha = str(store_captcha, "utf-8") if en_store_captcha != captcha.upper(): result.update({"response": "fail", "info": "验证码输入错误"}) return jsonify(result) with get_session() as db_session: u = db_session.query(User).filter(User.email == email).first() if u: de_password = AESCipher.decrypt(u.password) if de_password != password: result.update({"response": "fail", "info": "密码输入错误"}) return jsonify(result) else: login_user(u, remember=True) result["data"] = u.to_dict() return jsonify(result) else: result.update({"response": "fail", "info": "当前用户不存在"}) return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def check_user_email_valid(): try: result = {'response': 'success', 'info': ''} email = request.json.get("email", None) with get_session() as db_session: u = db_session.query(User).filter(User.email == email).first() if u: result.update({"response": "fail", "info": "当前用户邮箱已存在"}) return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def submit_post(): try: result = {'response': 'success', "info": ""} post_id = request.json.get("post_id", None) sub_forum_id = request.json.get("sub_forum_id", None) title = request.json.get("title", None) content = request.json.get("content", None) hidden_content = request.json.get("hidden_content", None) cover_image_id = request.json.get("cover_image_id", None) cost = request.json.get("cost", 0) status = request.json.get("status", 0) if status not in [Post.DRAFT, Post.CHECKING]: result.update({'response': 'fail', "info": "当前提交帖子状态错误"}) return jsonify(result) with get_session() as db_session: sub_forum = db_session.query(SubForum).filter( SubForum.id == sub_forum_id).first() if sub_forum: if post_id: post = db_session.query(Post).filter( Post.user_id == current_user.id, Post.id == post_id).first() if post: post.title = title post.status = status post.content = content post.cover_image_id = cover_image_id post.hidden_content = hidden_content post.cost = cost else: result.update({'response': 'fail', "info": "当前提交不存在"}) return jsonify(result) else: post = Post() post.sub_forum_id = sub_forum_id post.user_id = current_user.id, post.title = title post.status = status post.content = content post.cover_image_id = cover_image_id post.hidden_content = hidden_content post.cost = cost db_session.add(post) db_session.commit() post_data = post.to_dict() result.update({"data": post_data}) else: result.update({'response': 'fail', "info": "当前论坛不存在"}) return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def forum_info(): try: result = { 'response': 'success', "category_list": list(), "post_list": list(), "info": "" } sub_forum_id = request.args.get("sub_forum_id", None) with get_session() as db_session: sub_forum = db_session.query(SubForum).filter( SubForum.id == sub_forum_id).first() if sub_forum: forum_categories = db_session.query(ForumCategory).filter( ForumCategory.sub_forum_id == sub_forum_id).all() if len(forum_categories): for forum_category in forum_categories: forum_category_dict = forum_category.to_dict() forum_sub_categories = db_session.query( ForumSubCategory).filter( ForumSubCategory.category_id == forum_category.id).all() if len(forum_sub_categories): forum_category_dict["sub_categories"] = list() for forum_sub_category in forum_sub_categories: forum_category_dict["sub_categories"].append( forum_sub_category.to_dict()) result["category_list"].append(forum_category_dict) all_post = db_session.query(Post, User, Image).join( User, User.id == Post.user_id).join( Image, Image.id == Post.cover_image_id).filter( Post.status == Post.PASS, Post.sub_forum_id == sub_forum_id).order_by(-Post.created_date).all() if len(all_post) > 0: for post, user, image in all_post: post_dict = post.to_dict() post_dict["user"] = user.name post_dict["cover_image_url"] = image.get_image_url() result["post_list"].append(post_dict) else: result.update({'response': 'fail', "info": "当前论坛不存在"}) return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def pay_post(): try: result = {"response": "success", "data": "", "info": ""} post_id = request.json.get("post_id", None) with get_session() as db_session: post = db_session.query(Post).get(post_id) if post: if post.user_id != current_user.id: user_pay_record = db_session.query(UserPayRecord).filter( UserPayRecord.post_id == post_id, UserPayRecord.user_id == current_user.id).first() if user_pay_record: result.update({ "response": "fail", "info": "当前资源已购买,请勿重复购买" }) else: if post.cost <= current_user.coin: user_pay_record = UserPayRecord() user_pay_record.user_id = current_user.id user_pay_record.post_id = post.id user_pay_record.cost = post.cost current_user.coin -= post.cost db_session.add(user_pay_record) db_session.commit() result.update({"data": post.get_hidden_content()}) else: result.update({ "response": "fail", "info": "您的金币不足" }) else: result.update({ "response": "fail", "info": "当前帖子由您创建,无需购买" }) else: result.update({"response": "fail", "info": "当前帖子不存在"}) return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def forum_list(): try: result = {'response': 'success', "forum_list": list(), "info": ""} with get_session() as db_session: forums = db_session.query(Forum).all() if len(forums): for forum in forums: forum_dict = forum.to_dict() sub_forum_list = db_session.query(SubForum).filter( SubForum.forum_id == forum.id).all() forum_dict["sub_forums"] = list() if len(sub_forum_list): for sub_forum in sub_forum_list: sub_forum_dict = sub_forum.to_dict() forum_dict["sub_forums"].append(sub_forum_dict) result["forum_list"].append(forum_dict) return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def edit_post(): try: result = {"response": "success", "info": ""} sub_forum_id = request.args.get("sub_forum_id", None) with get_session() as db_session: post = db_session.query(Post).filter( Post.user_id == current_user.id, Post.sub_forum_id == sub_forum_id, Post.status == Post.DRAFT).first() if post: image = db_session.query(Image).get(post.cover_image_id) post_data = post.to_dict() post_data["hidden_content"] = post.get_hidden_content() post_data["cover_image_url"] = image.get_image_url( ) if image else "" post_data["content"] = post.get_content() result.update({"data": post_data}) return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def get_post(): try: result = {"response": "success", "data": "", "info": ""} post_id = request.args.get("post_id", None) with get_session() as db_session: post, image, sub_forum, user = db_session.query( Post, Image, SubForum, User).join(Image, Post.cover_image_id == Image.id).join( User, Post.user_id == User.id).join( SubForum, Post.sub_forum_id == SubForum.id).filter( Post.id == post_id).first() if post: post_dict = post.to_dict() post_dict["content"] = post.get_content() user_pay_record = db_session.query(UserPayRecord).filter( UserPayRecord.user_id == current_user.id, UserPayRecord.post_id == post_id).first() if user_pay_record or post.user_id == current_user.id: result.update({ "data": { "post": post_dict, "hidden_content": post.get_hidden_content(), "cover_image_url": image.get_image_url(), "sub_forum": sub_forum.to_dict(), "user": user.to_dict() } }) else: result.update({ "data": { "post": post_dict, "cover_image_url": image.get_image_url(), "sub_forum": sub_forum.to_dict(), "user": user.to_dict() } }) return jsonify(result) except Exception as e: print(traceback.format_exc(e)) abort(500)
def user_loader(user_id): with get_session() as db_session: u = db_session.query(User).get(user_id) return u