def user_pic_info(): """ 用户头像设置 :return: """ user = g.user if request.method == "GET": data = {"user_info": user.to_dict()} return render_template("news/user_pic_info.html", data=data) try: image_data = request.files.get("avatar").read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 将用户上传的图像保存在七牛云上 try: key = storage(image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传头像失败") user.avatar_url = key try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库保存失败") return jsonify(errno=RET.OK, errmsg="上传头像成功", data=constants.QINIU_DOMIN_PREFIX + key)
def user_news_release(): """ 发布新闻 :return: """ user = g.user if request.method == "GET": categorys = Category.query.all() # [obj] categorys_dict_li = [category.to_dict() for category in categorys] categorys_dict_li.pop(0) data = {"categorys_dict_li": categorys_dict_li} return render_template("news/user_news_release.html", data=data) 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="参数错误") try: category_id = int(category_id) image_data = index_image.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数类型不正确") try: key = storage(image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="第三上传失败") # 执行业务逻辑 news = News() news.title = title news.source = "个人发布" news.digest = digest news.content = content news.index_image_url = constants.QINIU_DOMIN_PREFIX + key news.category_id = category_id news.user_id = user.id news.status = 1 try: db.session.add(news) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据保存失败") return jsonify(errno=RET.OK, errmsg="OK")
def pic_info(): user = g.user if request.method == "GET": return render_template('news/user_pic_info.html', data={"user": user.to_dict()}) # 如果是POST表示修改头像 # 1. 取到上传的图片 try: avatar = request.files.get("avatar").read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 2. 上传头像 try: # 使用自已封装的storage方法去进行图片上传 key = storage(avatar) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传头像失败") # 3. 保存头像地址 user.avatar_url = key return jsonify(errno=RET.OK, errmsg="OK", data={"avatar_url": constants.QINIU_DOMIN_PREFIX + key})
def news_edit_detail(): """新闻编辑详情""" if request.method == "GET": # 获取参数 news_id = request.args.get("news_id") if not news_id: return render_template('admin/news_edit_detail.html', data={"errmsg": "未查询到此新闻"}) # 查询新闻 news = None try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) if not news: return render_template('admin/news_edit_detail.html', data={"errmsg": "未查询到此新闻"}) # 查询分类的数据 categories = Category.query.all() categories_li = [] for category in categories: c_dict = category.to_dict() c_dict["is_selected"] = False if category.id == news.category_id: c_dict["is_selected"] = True categories_li.append(c_dict) # 移除`最新`分类 categories_li.pop(0) data = {"news": news.to_dict(), "categories": categories_li} return render_template('admin/news_edit_detail.html', data=data) news_id = request.form.get("news_id") title = request.form.get("title") digest = request.form.get("digest") content = request.form.get("content") index_image = request.files.get("index_image") category_id = request.form.get("category_id") # 1.1 判断数据是否有值 if not all([title, digest, content, category_id]): return jsonify(errno=RET.PARAMERR, errmsg="参数有误") news = None try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) if not news: return jsonify(errno=RET.NODATA, errmsg="未查询到新闻数据") # 1.2 尝试读取图片 if index_image: try: index_image = index_image.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数有误") # 2. 将标题图片上传到七牛 try: key = storage(index_image) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传图片错误") news.index_image_url = constants.QINIU_DOMIN_PREFIX + key # 3. 设置相关数据 news.title = title news.digest = digest news.content = content news.category_id = category_id # 4. 保存到数据库 try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存数据失败") # 5. 返回结果 return jsonify(errno=RET.OK, errmsg="编辑成功")
def news_edit_detail(): """新闻编辑详情""" if request.method == "GET": news_id = request.args.get("news_id") if not news_id: abort(404) try: news_id = int(news_id) except Exception as e: current_app.logger.error(e) return render_template("admin/news_edit_detail.html", errmsg="参数错误") try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) return render_template("admin/news_edit_detail.html", errmsg="数据查询错误") if not news: return render_template('admin/news_edit_detail.html', errmsg="未查询到数据") # 查询分类数据 try: categories = Category.query.all() except Exception as e: current_app.logger.error(e) return render_template('admin/news_edit_detail.html', errmsg="查询数据错误") categories_dict_li = [] for category in categories: # 取到分类字典 cate_dict = category.to_dict() # 判断当前遍历到的分类是否是当前新闻的分类,如果是,则添加is_selected为true # 如果当前遍历出来的分类的id 和 该条新闻的分类id一样,那么is_selected = True if category.id == news.category_id: cate_dict["is_selected"] = True categories_dict_li.append(cate_dict) # 移除最新分类 categories_dict_li.pop(0) data = {"news": news.to_dict(), "categories": categories_dict_li} return render_template("admin/news_edit_detail.html", data=data) else: # 以下是POST请求 news_id = request.form.get("news_id") title = request.form.get("title") digest = request.form.get("digest") content = request.form.get("content") index_image = request.files.get("index_image") category_id = request.form.get("category_id") if not all([news_id, title, digest, content, index_image, category_id ]): return jsonify(errno=RET.PARAMERR, errmsg="参数有错误") try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据查询失败") if not news: return jsonify(errno=RET.NODATA, errmsg="未查询到新闻") # 尝试读取图片 if index_image: try: index_image = index_image.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 将图片上传到七牛 try: key = storage(index_image) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传失败") news.index_image_url = constants.QINIU_DOMIN_PREFIX + key # 设置相关数据 news.title = title news.digest = digest news.content = content news.category_id = category_id return jsonify(errno=RET.OK, errmsg="OK")
def news_release(): '''新闻发布''' if not g.user: # 未登录重定向到首页 return redirect("/") if request.method == "GET": # 动态加载新闻分类数据 categories = [] try: categories = Category.query.all() except Exception as e: current_app.logger.error(e) category_dict_li = [] for category in categories: category_dict_li.append(category.to_dict()) # 移除最新分类 category_dict_li.pop(0) return render_template('news/user_news_release.html', data={"categories": category_dict_li}) # 获取要提交的数据 # 标题 title = request.form.get('title') # 新闻来源 source = "个人发布" # 摘要 digest = request.form.get("digest") # 新闻内容 content = request.form.get("content") # 索引图片 index_image = request.files.get('index_image') # 分类id category_id = request.form.get("category_id") # 校验参数 if not all([title, digest, content, index_image, category_id]): return jsonify(errno=RET.PARAMERR, errmsg="PARAMERR error!") try: category_id = int(category_id) except Exception as e: return jsonify(errno=RET.PARAMERR, errmsg="PARAMERR error!") # 取到图片,将图片上传到七牛云 try: index_image_data = index_image.read() # 上传到七牛云 key = storage(index_image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="PARAMERR error!") # 保存入库 news = News() news.title = title news.digest = digest news.source = source news.content = content news.index_image_url = constants.QINIU_DOMIN_PREFIX + key news.category_id = category_id news.user_id = g.user.id # 代表审核状态 news.status = 1 try: db.session.add(news) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="News data saving failed!") return jsonify(errno=RET.OK, errmsg="OK")
def news_edit_detail(): """新闻编辑详情""" if request.method == "GET": news_id = request.args.get("news_id") if not news_id: abort(404) try: news_id = int(news_id) except Exception as e: current_app.logger.error(e) return render_template("admin/news_edit_detail.html", errmsg="参数错误") try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) return render_template("admin/news_edit_detail.html", errmsg="数据查询失败") if not news: return render_template("admin/news_edit_detail.html", errmsg="未查询到该新闻") # 查询分类数据 try: categories = Category.query.all() except Exception as e: current_app.logger.error(e) return render_template("admin/news_edit_detail.html", errmsg="查询数据错误") category_dict_li = [] for category in categories: category_dict = category.to_dict() if category.id == news.category_id: # 判断当前遍历到的分类是否是当前新闻的分类,如果是,则添加is_selected为true category_dict["is_selected"] = True category_dict_li.append(category_dict) category_dict_li.pop(0) data = {"news": news.to_dict(), "category_dict_li": category_dict_li} return render_template("admin/news_edit_detail.html", data=data) # POST请求的数据 news_id = request.form.get("news_id") title = request.form.get("title") digest = request.form.get("title") content = request.form.get("content") index_image = request.files.get("index_image") category_id = request.form.get("category_id") if not all([title, digest, content, category_id]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库查询失败") if not news: return jsonify(errno=RET.NODATA, errmsg="数据不存在") # 读取图片 if index_image: try: index_image = index_image.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 将标题图片上传到七牛云 try: key = storage(index_image) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传图片失败") news.index_image_url = constants.QINIU_DOMIN_PREFIX + key # 设置相关数据 news.title = title news.digest = digest news.content = content news.category_id = category_id # try: # db.session.add(news) # db.session.commit() # except Exception as e: # current_app.logger.error(e) # return jsonify(errno=RET.DBERR, errmsg="数据库保存失败") return jsonify(errno=RET.OK, errmsg="OK")