def pic_info(): # 判断用户是否登录 user = g.user if not user: return redirect("/") if request.method == "GET": return render_template("user/user_pic_info.html", user=user.to_dict()) try: img_bytes = request.files.get("avatar").read() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) try: file_name = upload_img(img_bytes) print(file_name) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) user.avatar_url = file_name return jsonify(errno=RET.OK, errmsg=error_map[RET.OK], data=user.to_dict())
def pic_info(): # 判断是否登录 user = g.user if not user: return abort(404) if request.method == 'GET': return render_template('news/user_pic_info.html', user=user.to_dict()) else: # 获取图片 try: img_bytes = request.files.get('avatar').read() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 上传至第三方服务器 try: file_name = upload_img(img_bytes) print(file_name) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) # 修改用户的头像url user.avatar_url = file_name return jsonify(errno=RET.OK, errmsg=error_map[RET.OK], data=user.to_dict())
def news_release(): # 判断用户是否登录 user = g.user if not user: return redirect("/") # 获得新闻的所有分类 categories = [] if request.method == "GET": try: categories = Category.query.all() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) return render_template("user/user_news_release.html", categories=categories) title = request.form.get("title") category_id = request.form.get("category_id") digest = request.form.get("digest") content = request.form.get("content") try: img_bytes = request.files.get("index_image").read() except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) if not all([title, category_id, digest, img_bytes, content]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) try: file_name = upload_img(img_bytes) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) try: category_id = int(category_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) news = News() news.title = title news.digest = digest news.category_id = category_id news.content = content news.index_image_url = constants.QINIU_DOMIN_PREFIX + file_name # 设置其他属性 news.user_id = user.id news.status = 1 news.source = "个人发布" db.session.add(news) return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def news_release(): user = g.user if not user: return abort(404) if request.method == "GET": # 查询所有的分类, 传到模板中 categories = [] try: categories = Category.query.all() except BaseException as e: current_app.logger.error(e) if len(categories): categories.pop(0) return render_template("news/user_news_release.html", categories=categories) # POST处理 title = request.form.get("title") category_id = request.form.get("category_id") digest = request.form.get("digest") index_image = request.files.get("index_image") content = request.form.get("content") if not all([title, category_id, digest, index_image, content]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) try: category_id = int(category_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 创建新闻模型 news = News() news.title = title news.category_id = category_id news.digest = digest news.content = content try: img_bytes = index_image.read() file_name = upload_img(img_bytes) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) news.index_image_url = QINIU_DOMIN_PREFIX + file_name # 设置其他属性 news.user_id = user.id news.status = 1 news.source = "个人发布" # 添加到数据库中 db.session.add(news) return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def news_edit_submit(): # 获取参数 news_id = request.form.get('news_id') title = request.form.get('title') category_id = request.form.get('category_id') digest = request.form.get('digest') index_image = request.files.get('index_image') content = request.form.get('content') # 校验参数 if not all([title, category_id, digest, index_image, content]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) try: news_id = int(news_id) category_id = int(category_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 从数据库获取新闻 try: news = News.query.get(news_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 判断新闻是否存在 if not news or not category_id: return jsonify(errno=RET.NODATA, errmsg=error_map[RET.NODATA]) # 修改新闻 news.title = title news.category_id = category_id news.digest = digest # 上传图片到第三方 try: img_bytes = index_image.read() file_name = upload_img(img_bytes) news.index_image_url = QINIU_DOMIN_PREFIX + file_name except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) news.content = content # 设置过修改自动提交 db.session.add(news) return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def pic_info(): user = g.user if not user: return abort(404) if request.method == "GET": return render_template("news/user_pic_info.html", user=user.to_dict()) # POST处理 try: img_bytes = request.files.get("avatar").read() except BaseException as e: return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 上传文件 try: file_name = upload_img(img_bytes) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) # 修改用户的头像URL user.avatar_url = file_name return jsonify(errno=RET.OK, errmsg=error_map[RET.OK], data=user.to_dict())
def news_edit_detail(): if request.method == 'GET': # 获取参数 news_id = request.args.get("news_id") # 校验参数 try: news_id = int(news_id) except BaseException as e: current_app.logger.error(e) return abort(404) # 查询新闻模型 try: news = News.query.get(news_id) except BaseException as e: current_app.logger.error(e) return abort(404) # 将所有的分类传到模板中 categories = [] try: categories = Category.query.all() except BaseException as e: current_app.logger.error(e) return abort(404) # 标记新闻对应的当前分类 category_list = [] for category in categories: is_selected = False category_dict = category.to_dict() if category.id == news.category_id: is_selected = True category_dict["is_selected"] = is_selected category_list.append(category_dict) if len(category_list): category_list.pop(0) # 将模型数据传到模板中 return render_template("admin/news_edit_detail.html", news=news.to_dict(), category_list=category_list) # POST处理 news_id = request.form.get("news_id") title = request.form.get("title") category_id = request.form.get("category_id") digest = request.form.get("digest") index_image = request.files.get("index_image") content = request.form.get("content") if not all([news_id, title, category_id, digest, content]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) try: news_id = int(news_id) category_id = int(category_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 取出新闻模型 try: news = News.query.get(news_id) category = Category.query.get(category_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) if not news or not category: return jsonify(errno=RET.NODATA, errmsg=error_map[RET.NODATA]) # 修改新闻模型 news.title = title news.category_id = category_id news.digest = digest news.content = content if index_image: try: img_bytes = index_image.read() file_name = upload_img(img_bytes) news.index_image_url = QINIU_DOMIN_PREFIX + file_name except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])
def news_release(): # 判断用户是否登录 user = g.user if not user: return abort(404) if request.method == 'GET': categories = list() try: categories = Category.query.all() # type:Category categories.pop(0) except BaseException as e: current_app.logger.error(e) return render_template('news/user_news_release.html', categories=categories) # # POST else: # 获取参数 title = request.form.get('title') digest = request.form.get('digest') category_id = request.form.get('category_id') index_image = request.files.get('index_image') # type:FileStorages content = request.form.get('content') # 校验参数 if not all([title, digest, content, category_id]): return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) try: category_id = int(category_id) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg=error_map[RET.PARAMERR]) # 将数据存入数据库 news = News() news.title = title news.digest = digest news.source = '个人发布' news.category_id = category_id news.create_time = datetime.now() news.status = 1 news.content = content news.user_id = user.id if index_image: # 将照片存入第三方平台 index_image = index_image.read() # try: files_name = upload_img(index_image) except BaseException as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg=error_map[RET.THIRDERR]) news.index_image_url = QINIU_DOMIN_PREFIX + files_name try: db.session.add(news) db.session.commit() except BaseException as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) # 返回json return jsonify(errno=RET.OK, errmsg=error_map[RET.OK])