def news_release(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR,errmsg='请登录') if request.method == 'GET': try: categories = Category.query.all() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='查询分类数据异常') if not categories: return jsonify(errno=RET.NODATA, errmsg='无分类数据') category_list =[] for category in categories: category_list.append(category.to_dict()) category_list.pop(0) data ={ 'categories':category_list } return render_template('news/user_news_release.html',data=data) title = request.form.get('title') digest = request.form.get('digest') category_id = request.form.get('category_id') content = request.form.get('content') index_image = request.files.get('index_image') if not all([title, digest, category_id, content, index_image]): return jsonify(errno=RET.PARAMERR,errmsg='参数缺失') try: category_id = int(category_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR,errmsg='参数格式错误') try: image_data= index_image.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR,errmsg='图片读取错误') try: image_name = storage(image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR,errmsg='上传图片异常') news = News() news.title = title news.digest = digest news.category_id = category_id news.content = content news.index_image_url = constants.QINIU_DOMIN_PREFIX + image_name news.source = '个人发布' news.status = 1 news.user_id = user.id 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='保存数据失败') return jsonify(errno=RET.OK,errmsg='OK')
def save_avatar(): user = g.user if not user: return redirect('/static/html/index.html') if request.method == 'GET': data = { 'avatar_url': user.avatar_url } return jsonify(errno=RET.OK,errmsg='OK',data=data) avatar = request.files.get('avatar') if not avatar: return jsonify(errno=RET.PARAMERR,errmsg='参数错误') image_data = avatar.read() try: image_name = storage(image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR,errmsg='上传图片失败') user.avatar_url = image_name try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR,errmsg='保存图片数据失败') avatar_url = constants.QINIU_DOMIN_PREFIX + user.avatar_url if user.avatar_url else "" data = { 'avatar_url':avatar_url } return jsonify(errno=RET.OK,errmsg='OK',data=data)
def user_pic_info(): if request.method == 'GET': data = {'user': g.user.to_login_dict()} return render_template('news/user_pic_info.html', data=data) # 修改 # 1.接收:图片文件 avatar = request.files.get('avatar') # avatar.save('') # 2.验证 if not avatar: return jsonify(errno=RET.NODATA, errmsg='请选择头像图片') # 3.处理 # 3.1.将图片保存到七牛 try: avatar_url = storage(avatar.read()) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='头像图片上传失败') # 3.2.修改用户的头像属性 user = g.user user.avatar_url = avatar_url try: db.session.commit() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='头像修改失败') # 4.响应 return jsonify(errno=RET.OK, errmsg='', data=user.to_login_dict())
def save_user_avatar(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR,errmsg='没登录') if request.method == 'GET': data = { 'user': user.to_dict() } return render_template('news/user_pic_info.html', data=data) avatar = request.files.get('avatar') if not avatar: return jsonify(errno=RET.PARAMERR, errmsg='参数缺少') try: image_data = avatar.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.IOERR, errmsg='读取文件错误') try: image_name = storage(image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='上传图片错误') user.avatar_url = image_name try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存数据失败') image_url = constants.QINIU_DOMIN_PREFIX + image_name return jsonify(errno=RET.OK, errmsg="ok", data={'avatar_url':image_url})
def user_avatar(): ''' 用户详情页设置头像 :return: ''' user = g.user if request.method == 'GET': return render_template('/news/user_pic_info.html', user=user) elif request.method == 'POST': file = request.files.get('avatar') if not file: return jsonify(errno=RET.PARAMERR, errmsg='文件缺失') try: key = storage(file.read()) 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: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='数据库操作失败') avatar_url = constants.QINIU_DOMIN_PREFIX + user.avatar_url return jsonify(errno=RET.OK, errmsg='操作成功', avatar_url=avatar_url)
def pic_info(): user = g.user # 当请求为get请求时直接展示页面 if request.method == "GET": data = {"user_info": user.to_dict()} return render_template("news/user_pic_info.html", data=data) # 读取页面上传的图片资源 try: avatar_file = request.files.get("avatar").read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="图片读取失败") try: # 通过七牛云上传图片资源,storage()函数会返回一个key,用七牛云的网址加后缀key,也就是下面的url就可以获取该上传的图片 url = storage(avatar_file) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="图片上传失败") # 将图片的url地址保存到对应的user的属性里,点击提交后会拼接网址到url中, user.avatar_url = url try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="数据保存失败") return jsonify(errno=RET.OK, errmsg="图片上传成功", data={"avatar_url": constants.QINIU_DOMIN_PREFIX + url})
def pic_info(): user = g.user if request.method == 'GET': data = {'user_info': user.to_dict()} return render_template('news/user_pic_info.html', data=data) try: # params avatar_file = request.files.get('avatar').read() except Exception as e: current_app.logger.error(e) return jsonify(error=RET.PARAMERR, errmsg='文件读取失败') # upload file to qiniu stack try: url = storage(avatar_file) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.THIRDERR, errmsg='文件上传失败') # syschronize image to web client user.avatar_url = url try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(error=RET.DBERR, errmsg='保存数据错误') data = {'avatar_url': constants.QINIU_DOMIN_PREFIX + url} return jsonify(error=RET.OK, errmsg='ok', data=data)
def news_release(): if request.method == "GET": category_list = Category.query.all() categories = [] for category in category_list: categories.append(category.to_dict()) # 最新的分类是按照时间进行排序,所有不需要添加到分类列表当中 categories.pop(0) data = {"categories": categories} 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="参数输入错误") index_image = index_image.read() key = storage(index_image) user = g.user 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 # 1表示当前新闻在审核中 news.status = 1 db.session.add(news) db.session.commit() return jsonify(errno=RET.OK, errmsg="发布成功")
def pic_info(): user = g.user if request.method == "GET": data = {"user_info": user.to_dict()} return render_template("news/user_pic_info.html", data=data) # 1. 获取到上传的文件 try: avater_file = request.files.get("avatar").read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="读取文件出错") # 2. 再将文件上传到七牛云 try: url = storage(avater_file) print(url) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传图片错误") # 3. 设置用户模型相关数据 try: user.avatar_url = url db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存用户数据错误") # 4. 返回上传的结果<avatar_url> data = {"avatar_url": constants.QINIU_DOMIN_PREFIX + url} return jsonify(errno=RET.OK, errmsg="OK", data=data)
def pic_info(): user = g.user if request.method == "GET": return render_template('news/user_pic_info.html', data={"user": user.to_dict()}) # 1. 获取到上传的文件 try: avatar_file = request.files.get("avatar").read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="读取文件出错") # 2. 再将文件上传到七牛云 try: key = storage(avatar_file) 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", avatar_url=constants.QINIU_DOMIN_PREFIX)
def pic_info(): user = g.user if request.method == 'GET': data = {'user': user.to_dict()} return render_template('news/user_pic_info.html', data=data) avatar = request.files.get('avatar') if not avatar: return jsonify(errno=RET.PARAMERR, errmsg='没有获取到参数') try: image_data = avatar.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='数据读取错误') #调取七牛云,获取七牛云返回的图片名称 try: image_name = storage(image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='上传图片异常') user.avatar_url = image_name try: db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存图片数据失败') avatar_url = constants.QINIU_DOMIN_PREFIX + image_name data = {'avatar_url': avatar_url} #返回结果 return jsonify(errno=RET.OK, errmsg='OK', data=data)
def user_news_release(): user = g.user if request.method != 'POST': category = None try: category = Category.query.all() except Exception as e: current_app.logger.debug(e) if not category: return jsonify(errno=RET.DATAERR, errmsg="参数错误或者新闻不存在") category_list = list() category.pop(0) for temp in category: category_list.append(temp.to_dict()) data = {'category_list': category_list} return render_template('news/user_news_release.html', data=data) title = request.form.get('title') pic = request.files.get('index_image') content = request.form.get('content') digest = request.form.get('digest') category = request.form.get('category_id') iscategory = None try: iscategory = Category.query.filter(Category.id == category) except Exception as e: current_app.logger.debug(e) if not all([title, pic, content, digest, category]): return jsonify(errno=RET.DATAERR, errmsg="参数错误") # 非空校验 if not iscategory: return jsonify(errno=RET.DATAERR, errmsg="参数错误") # 上传用户图片 pic_url = None try: pic_url = constants.QINIU_DOMIN_PREFIX + storage(pic.read()) print(pic_url) except Exception as e: current_app.logger.debug(e) try: news = News() news.user_id = user.id news.category_id = category news.digest = digest news.source = user.nick_name news.content = content news.title = title news.status = 1 news.index_image_url = pic_url db.session.add(news) db.session.commit() except Exception as e: current_app.logger.debug(e) return jsonify(errno=RET.OK, errmsg="OK")
def user_pic_info(): user = g.user if request.method == "GET": data = {"user_info": user.to_dict() if user else None} return render_template("news/user_pic_info.html", data=data) #获取要上传的文件 try: avatar = request.files.get("avatar").read() except Exception as e: return jsonify(errno=RET.PARAMERR, errmsg="读取文件出错") #将文件上传到七牛云 try: url = storage(avatar) except Exception as e: return jsonify(errno=RET.THIRDERR, errmsg="上传图片错误") #将头像更新到用户模型中 user.avatar_url = url try: db.session.commit() except Exception as e: db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存用户数据错误") print(constants.QINIU_DOMIN_PREFIX + url) return jsonify(errno=RET.OK, errmsg="OK", data={"avatar_url": constants.QINIU_DOMIN_PREFIX + url})
def pic_info(): user = g.user if request.method == "GET": data = {"user_info": user.to_dict()} return render_template('news/user_pic_info.html', data=data) # 1.获取上传的文件 try: avatar_file = request.files.get("avatar").read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='读取文件错误') #上传七牛云 try: url = storage(avatar_file) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.THIRDERR, errmsg='上传图片错误') #将头像信息更新到当前用户 print("url测试") print(url) user.avatar_url = url try: # 数据保存在数据库 db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.OK, errmsg='ok') data = {"avatar_url": constants.QINIU_DOMIN_PREFIX + url} return jsonify(errno=RET.OK, errmsg="OK", data=data)
def pic_info(): user = g.user if request.method == "GET": return render_template('news/user_pic_info.html', data={"user_info": user.to_dict()}) # 1. 获取到上传的文件 try: avatar_file = request.files.get('avatar').read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="读取文件出错!") # 2. 将文件上传到七牛云 try: url = storage(avatar_file) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传图片错误!") # 3. 将头像信息更新到当前用户的模型类中 user.avatar_url = url try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存用户数据错误!") # 4. 返回上传的结果<avatar_url> return jsonify(errno=RET.OK, errmsg="OK!", data={"avatar_url": constants.QINIU_DOMIN_PREFIX + url})
def pic_info(): user = g.user if request.method == "GET": return render_template('news/user_pic_info.html', data={"user": user.to_dict()}) # TODO 如果是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 user_pic(): """用户头像设置""" user = g.user if not user: return redirect('/') # get请求显示默认头像 if request.method == "GET": data = { "user": user.to_dict() } return render_template('news/user_pic_info.html', data=data) # post请求设置头像 if request.method == "POST": try: avatar = request.files.get('avatar').read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 七牛云存储图片 try: # 封装的七牛云模块 key = storage(avatar) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="头像上传失败") # 保存头像地址 user.avatar_url = key return jsonify(errno=RET.OK, errmsg="上传成功", data={"avatar_url": constants.QINIU_DOMIN_PREFIX + key})
def pic_info(): user = g.user data = { 'user_info': user.to_dict(), } if request.method == 'GET': return render_template('news/user_pic_info.html', data=data) # 获取上传文件 try: avatar_file = request.files.get("avatar").read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='读取文件错误') try: url = storage(avatar_file) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='上传图片失败') # 更新图片信息到当前用户 user.avatar_url = url try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='数据提交失败') return jsonify(errno=RET.OK, data={'avatar_url': constants.QINIU_DOMIN_PREFIX + url})
def pic_info(): ''' 用户上传头像 :return: ''' 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(error=RET.PARAMERR, errmsg='参数错误') # 2.上传头像 try: key = storage(avatar) print(key) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.THIRDERR, errmsg='上传头像失败') # 3.保存头像地址 user.avatar_url = key return jsonify(error=RET.OK, errmsg='OK', data={'user': user.to_dict() if user else None})
def user_pic_info(): if request.method == "GET": data = {"user": g.user.to_login_dict} return render_template("news/user_pic_info.html", data=data) avatar = request.files.get("avatar") print(avatar, "vvvvvvvv") if not avatar: return jsonify(errno=RET.NODATA, errmsg="没有数据") try: file_name = storage(avatar.read()) print(file_name, "dddddddddddddddddd") except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DATAERR, ermsg="数据错误") user = g.user user.avatar_url = file_name try: db.session.commit() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库链接失败") return jsonify(errno=RET.OK, errmsg="", data=user.to_login_dict())
def pic_info(): user = g.user if request.method == "GET": data = {'user': user.to_dict()} return render_template("user/user_pic_info.html", data=data) avatar = request.files.get("avatar") if not avatar: return jsonify(error=RET.PARAMERR, errmsg="图片为空") avatar_data = avatar.read() try: image_name = storage(avatar_data) except Exception as e: return jsonify(error=RET.THIRDERR, errmsg="七牛云") user.avatar_url = image_name try: db.session.commit() except Exception as e: return jsonify(error=RET.THIRDERR, errmsg="mysql错误") # 拼接完整图片: avatar_url = QINIU_DOMIN_PREFIX + user.avatar_url data = {"avatar_url": avatar_url} return jsonify(error=RET.OK, errmsg="OK", data=data)
def new_edit_detail(): if request.method=="GET": news_id = request.args.get("news_id") # global news # news = news_id print("ssnngggg",news_id) try: global news news = News.query.get(news_id) category = Category.query.all() data = { "news":news, "category_name":[i.name for i in category] } except Exception as e: current_app.logger.error(e) return redirect("admin/new_edit_detail") return render_template("admin/news_edit_detail.html", data = data) print(news,"fffffff") # new = News.query.get(news) new = news print(new,"ssss") title = request.form.get("title") print(title,"ttttttttt") print(new.title,"dddddddd") # category_add = request.form.get("category") digest = request.form.get("digest") url = request.files.get("url") content = request.form.get("content") if content != new.content: new.content = content if title != new.title: new.title = title # if category_add != new.category.name: # new.category_id = new.category.id if digest != new.digest: new.digest = digest if url: file_name = storage(url.read()) if new.index_image_url != file_name: new.index_image_url = file_name try: db.session.add(new) db.session.commit() except Exception as e: return jsonify(errno=RET.DBERR, errmsg="数据库链接失败") return jsonify(errno = RET.OK, errmsg="")
def news_edit_detail(): if request.method == 'GET': news_id = request.args.get('news_id') news = News.query.get(news_id) category_list = [] categories = Category.query.filter(Category.id != 1).all() for category in categories: category_list.append(category.to_dict()) data = { 'news': news.to_dict(), 'category_list': category_list, } return render_template('admin/news_edit_detail.html', data=data) news_id = request.form.get('news_id') try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) 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') print(news_id) print(title) print(digest) print(content) print(index_image) print(category_id) if not all([news_id, title, digest, content, category_id]): return jsonify(errno=RET.PARAMERR, errmsg='参数不全') if not news: return jsonify(errno=RET.NODATA, errmsg='未查询到新闻数据') if index_image: index_image = index_image.read() try: key = storage(index_image) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="第三方系统错误") index_image_url = constants.QINIU_DOMIN_PREFIX + key if index_image: news.index_image_url = index_image_url news.title = title news.digest = digest news.content = content news.category_id = category_id db.session.commit() return jsonify(errno=RET.OK, errmsg='操作成功')
def user_news_release(): """ 新闻发布: 如果是get请求,加载新闻分类,需要移除'最新'分类,传给模板 :return: """ user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg='用户未登录') if request.method == 'GET': categories = Category.query.filter(Category.id > 1).all() category_list = [] for category in categories: category_list.append(category.to_dict()) data = {'category': category_list} return render_template('news/user_news_release.html', data=data) pic_url = request.files.get('pic_url') digest = request.form.get('digest') content = request.form.get('content') title = request.form.get('title') category_id = request.form.get('category_id') if not all([pic_url, digest, content, title, category_id]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') try: category_id = int(category_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='数据类型转换异常') # 存储图片 try: image_data = pic_url.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='图片读取异常') # 使用七牛云存储图片 try: image_name = storage(image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='图片存储异常') new = News() new.category_id = category_id new.index_image_url = constants.QINIU_DOMIN_PREFIX + image_name new.content = content # new.create_time = datetime.datetime.now() new.user_id = user.id new.digest = digest new.title = title new.status = 1 new.source = '个人发布' try: db.session.add(new) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='数据存储异常') return jsonify(errno=RET.OK, errmsg='新闻发布成功')
def news_release(): user = g.user if request.method == "GET": # 查询所有的分类 categories = None # 指定查询结果为默认none,避免跑异常 try: categories = Category.query.all() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.NODATA, errmsg="数据库查询失败") category_list = [] for category in categories: category_list.append(category.to_dict()) # 删除默认的"最新"这个分类,这个分类是不存在的额 category_list.pop(0) return render_template("news/user_news_release.html", data={"categories": category_list}) # 当请求为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="参数错误") # 读取图片,将图片转化为二进制 try: index_image = index_image.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="图片读取失败") # 将图片上传到七牛云,返回值是获得图片的key,网址拼接即可拿到图片 try: key = storage(index_image) 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: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="新闻数据提交失败") 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: return render_template("admin/news_edit_detail.html", data={"errmsg": "未查询到此新闻"}) news = News.query.get(news_id) 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") if not all([title, digest, content, category_id]): return jsonify(errno=RET.PARAMERR, errmsg="参数有误") news = News.query.get(news_id) if not news: return jsonify(errno=RET.PARAMERR, errmsg="未查询到新闻数据") if index_image: try: index_image = index_image.read() except Exception as e: return jsonify(errno=RET.PARAMERR, errmsg="参数有误") try: key = storage(index_image) except Exception as 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.commit() except Exception as e: db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存数据失败") return jsonify(errno=RET.OK, errmsg="编辑成功")
def news_edit_detail(): if request.method == "GET": news_id = request.args.get("news_id") news = News.query.get(news_id) categorys = Category.query.all() categorys_list = [] for category in categorys: categorys_list.append(category.to_dict()) categorys_list.pop(0) data = {"news": news.to_dict(), "categories": categorys_list} 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 index_image: try: index_image = index_image.read() except Exception as e: return jsonify(errno=RET.PARAMERR, errmsg="参数有误") key = storage(index_image) # 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 user_news_release(): user = g.user if not user: return redirect('/') try: cates = Category.query.filter(Category.id != 1).all() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询新闻分类错误") categary_list = [] if cates: for cate in cates: categary_list.append(cate.to_dict()) if request.method == 'GET': return render_template('news/user_news_release.html', categary_list=categary_list) if request.method == "POST": title = request.form.get('title') categery = request.form.get('categary') digest = request.form.get('digest') image_file = request.files.get('image') content = request.form.get('content') print(content) if not all([title, categery, digest, image_file, content]): return jsonify(errno=RET.PARAMERR, errmsg="参数不全") image_data = image_file.read() try: image_name = image_storage.storage(image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg="上传图片到期牛云错误") new_release = News() new_release.title = title new_release.digest = digest new_release.index_image_url = constants.QINIU_DOMIN_PREFIX + image_name new_release.content = content new_release.category_id = categery new_release.status = 1 new_release.user_id = user.id new_release.source = user.nick_name new_release.create_time = datetime.now() try: db.session.add(new_release) 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")
def push_images(house_id): ''' 判断是否登录 判断房屋id 获取参数,调用第三方软件,返回图片地址给前端 :return: ''' # user = g.user image_file = request.files.get('house_image') # 检查参数 if not all([house_id, image_file]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺少') try: house = House.query.get(house_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据库失败") if house is None: return jsonify(errno=RET.DBERR, errmsg="没有房屋信息") # 读取图片数据 try: house_image_data = image_file.read() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DATAERR, errmsg='数据读取错误') # 调用七牛云,获取七牛云返回的图片名称 try: house_image_url = storage(house_image_data) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='上传图片异常') if not house.index_image_url: house.index_image_url = house_image_url db.session.add(house) # # 构造模型类对象,保存数据 house_image = HouseImage() house_image.url = house_image_url house_image.house_id = house_id # # 保存图片地址到mysql数据库 try: db.session.add(house_image) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存图片数据失败') # 拼接字符串 返回给前端 url = constants.QINIU_DOMIN_PREFIX + house_image_url # 定义字典数据 data = {'url': url} # 返回结果 return jsonify(errno=RET.OK, errmsg='OK', data=data)
def user_news_release(): """新闻发布""" if request.method == 'GET': # 获取新闻分类信息 try: categories = Category.query.all() except Exception as e: current_app.logger.error(e) abort(500) # 去除最新分类 categories.pop(0) return render_template('news/user_news_release.html', categories=categories) else: # 新闻发布 # 1. 接收参数并进行参数校验 title = request.form.get('title') category_id = request.form.get('category_id') digest = request.form.get('digest') content = request.form.get('content') file = request.files.get('index_image') if not all([title, category_id, digest, content, file]): return jsonify(errno=RET.PARAMERR, errmsg='参数不完整') # 2. 将新闻的索引图片上传至七牛云 try: key = storage(file.read()) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.THIRDERR, errmsg='保存新闻索引图片失败') # 3. 创建News对象并保存发布新闻信息 news = News() news.title = title news.source = '个人发布' news.category_id = category_id news.digest = digest news.content = content news.index_image_url = QINIU_DOMIN_PREFIX + key news.user_id = g.user.id news.status = 1 # 4. 将新闻信息添加进数据库 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='保存发布新闻信息失败') # 5. 返回应答,新闻发布成功 return jsonify(errno=RET.OK, errmsg='新闻发布成功')