def release(): user = UserInfo.query.get(session['user_id']) category_list = NewsCategory.query.all() try: # 有带新闻参数的get方式请求,认为是修改新闻的请求 news_id = int(request.args.get('news_id')) news = NewsInfo.query.get(news_id) except: news = None if request.method == 'GET': # news为NewsInfo对象或者None,当为None时,模板中即不显示值 return render_template('news/user_news_release.html', category_list=category_list, news=news) elif request.method == 'POST': dict1 = request.form title = dict1.get('title') category_id = int(dict1.get('category')) summary = dict1.get('summary') context = dict1.get('content') try: __news_pic = request.files.get('news_pic') news_picname = pic1(__news_pic) except: # 用户没有上传图片,则存值空字符串 news_picname = '' if not all((title, category_id, summary, context)): # html中存在要用到news的html,错误信息回传时,也需要传news对象 return render_template('news/user_news_release.html', category_list=category_list, error_info='您的内容没有填写完整哦', news=news) try: if news: # 如果是修改操作,不用传用户id, news.update_time = datetime.now() else: news = NewsInfo() news.user_id = user.id # 如果获取到了上传的图片文件,就上传或者修改 if news_picname: news.pic = news_picname news.title = title news.category_id = category_id news.summary = summary news.status = 1 news.context = context db.session.add(news) db.session.commit() # 数据添加成功后默认去到新闻列表页 return redirect('/user/list') except: return render_template('news/user_news_release.html', category_list=category_list, error_info='服务器出错', news=news)
def release(): if request.method == 'GET': category_list = NewsCategory.query.all() return render_template( 'news/user_news_release.html', category_list=category_list ) # 添加新闻数据 title = request.form.get('title') category = request.form.get('category') summary = request.form.get('summary') content = request.form.get('content') # 接收图片 pic = request.files.get('pic') # 验证 if not all([title, category, summary, content, pic]): return '请填写完整数据' # 保存图片到七牛 pic_name = qiniu_upload.upload(pic) # 保存数据 news = NewsInfo() news.title = title news.category_id = int(category) news.summary = summary news.context = content news.pic = pic_name news.user_id = session.get('user_id') db.session.add(news) #修改作者的发布量 # user=g.user # user.public_count+=1 #提交到数据库 db.session.commit() # 响应 return redirect('/user/list')
def user_news_release(): if request.method == 'GET': category_list = NewsCategory().query.all() return render_template('news/user_news_release.html', list=category_list) title = request.form.get('title') category_id = request.form.get('category_id') summary = request.form.get('summary') context = request.form.get('content') pic = request.files.get('pic') news = NewsInfo() if pic: # 将图片上传到腾讯云 nowTime = lambda: int(round(time.time() * 1000)) file_name = str(random.random()) + str(nowTime()) + pic.filename qlcoud_cos.upload_img(pic, file_name) news.pic = file_name # 向数据库添加数据 news.title = title news.category_id = category_id news.summary = summary news.context = context news.user_id = g.user.id db.session.add(news) db.session.commit() return jsonify(res=1)
def release(): category_list = NewsCategory.query.all() if request.method == 'GET': return render_template('news/user_news_release.html', category_list=category_list) elif request.method == 'POST': dict1 = request.form title = dict1.get('title') category_id = int(dict1.get('category')) summary = dict1.get('summary') content = dict1.get('content') news_pic = request.files.get('news_pic') if not all([title, category_id, summary, content, news_pic]): return render_template('news/user_news_release.html', category_list=category_list, msg='数据不能为空') filename = upload_pic(news_pic) news = NewsInfo() news.title = title news.category_id = category_id news.summary = summary news.content = content news.pic = filename news.user_id = session['user_id'] db.session.add(news) db.session.commit() return redirect('/user/newsList')
def release(): # 查询新闻分类 # category = NewsCategory.query.all() if request.method == 'GET': return render_template('news/user_news_release.html', category=category) elif request.method == 'POST': dict1 = request.form title = dict1.get('title') category_id = dict1.get('category_id') summary = dict1.get('summary') content = dict1.get('content') pic = dict1.get('pic') print(dict1) if not all([title, category_id, summary, content, pic]): print(111) return jsonify(result=1, title=title, category_id=category_id, summary=summary, content=content, pic=pic) # p=pic # str1=p # us=re.split(r'\\',str1) # print(us) news = NewsInfo() print(news) news.category_id = category_id news.pic = pic str1 = news.pic use = re.split(r"\\", str1) print(use[2]) news.pic = use[2] news.title = title news.summary = summary news.content = content news.user_id = session['user_id'] db.session.add(news) db.session.commit() return jsonify(result=2)
def release(): if request.method == 'GET': category_list = NewsCategory.query.all() return render_template('news/user_news_release.html', category_list=category_list) title = request.form.get('title') category = request.form.get('category') summary = request.form.get('summary') content = request.form.get('content') if not all([title, category, summary, content]): return '请填写完整数据' news = NewsInfo() news.title = title news.category_id = category news.summary = summary news.context = content pic = request.files.get('pic') if pic: pic_name = qiniu_upload.upload(pic) news.pic = pic_name news.user_id = session.get('user_id') db.session.add(news) db.session.commit() return redirect('/user/list')
def release(): # 查询所有的分类,供编辑人员选择 category_list = NewsCategory.query.all() # 接收新闻的编号, 为了确认是创建还是修改 news_id = request.args.get('news_id') if request.method == 'GET': if news_id is None: # 展示页面 return render_template('news/user_news_release.html', category_list=category_list, news=None) else: # 如果有新闻编号存在,则进行修改操作,所以需要查询到原新闻并展示 news = NewsInfo.query.get(int(news_id)) return render_template('news/user_news_release.html', category_list=category_list, news=news) elif request.method == 'POST': # 新闻的添加处理 # 1.接收请求 dict1 = request.form title = dict1.get('title') category_id = dict1.get('category') summary = dict1.get('summary') content = dict1.get('content') # 接收新闻图片 news_pic = request.files.get('news_pic') if news_id is None: # 2.验证 if not all([title, category_id, summary, content, news_pic]): return render_template('news/user_news_release.html', category_list=category_list, msg='请将数据填写完整') else: if not all([title, category_id, summary, content]): return render_template('news/user_news_release.html', category_list=category_list, msg='请将数据填写完整') # 上传图片到七牛云, 修改文章内容的时候,默认不改图片,检查一下到底要不要改 if news_pic: from utils.qiniu_xjzx import upload_pic filename = upload_pic(news_pic) # 3.添加 if news_id is None: news = NewsInfo() else: news = NewsInfo.query.get(news_id) news.category_id = int(category_id) if news_pic: news.pic = filename news.title = title news.summary = summary news.content = content news.status = 1 news.update_time = datetime.now() news.user_id = session['user_id'] # 4.提交 db.session.add(news) db.session.commit() # 5.响应 return redirect('/user/newslist')
parser = argparse.ArgumentParser(description='data2sql') parser.add_argument('--mode', type=str, default='both', help="choose a mode: ['crisis', 'nextevent', 'both']") # parser.add_argument('--theme', default='NH', type=str, help='theme_name') args = parser.parse_args() # theme = "南海" # 更新的主题范围, 南海、朝核、台选, both mode = args.mode # 更新crisis、nextevent or both crisisNewsFunc = CrisisNewsFunc() eventPredictFunc = EventPredictFunc() # news = NewsInfo.select().where(NewsInfo.theme_label == theme) # 根据主题选取新闻 news = NewsInfo.select() # 逐条更新 (是否有批量更新方法?) for n in tqdm(news): if mode == "crisis": WJcrisis, WJWords = crisisNewsFunc.calcu_crisis_pro( n.theme_label, n.title) # 危机指数计算 n.crisis = WJcrisis n.wjwords = WJWords n.save() elif mode == "nextevent": n.nextevent = eventPredictFunc.calcu_next_event( n.theme_label, n.title) # 事件预测 n.save() elif mode == "both": WJcrisis, WJWords = crisisNewsFunc.calcu_crisis_pro(
def newscsvtosql(path, theme, datatype=1): # 从csv文件读入数据 df = pd.read_csv(path) df['time'] = pd.to_datetime(df['time']) df = df.fillna('') # 填充NA数据 # print(df.shape) # 遍历读取处理 news_data = [] for index, row in df.iterrows(): tmp = {} tmp['newsid'] = row['news_id'] tmp['title'] = clean_zh_text(row['title'], 2).replace("原创",'').replace("转帖",'').replace("参考消息",'') # 过滤title信息 tmp['time'] = datetime.strftime(row['time'],'%Y-%m-%d %H:%M:%S') # 格式化时间字符串 tmp['content'] = clean_zh_text(row['content']) # 清洗正文内容 tmp['url'] = row['url'] tmp['customer'] = row['customer'] tmp['theme_label'] = theme tmp['content_label'] = row['content_label'] tmp['country_label'] = row['country_label'] tmp['positive'] = float(row['positive']) tmp['negative'] = float(row['negative']) tmp['influence'] = float(row['influence']) # print(row['reliability']) tmp['reliability'] = float(row['reliability']) tmp['crisis'] = float(row['crisis']) tmp['persons'] = row['persons'] tmp['orgs'] = row['orgs'] tmp['wjwords'] = row['wjwords'] tmp['nextevent'] = row['nextevent'] news_data.append(tmp) # print(len(news_data)) # write data to mysql mysql_db.connect() # 插入新闻数据 if not NewsInfo.table_exists(): # 如果表不存在则创建 NewsInfo.create_table() # else: # bug调好后注释掉, 改为增量 # NewsInfo.delete().execute() # 每次重新更新之前清空数据表 # 根据切片分批次插入 slice_size = 300 # 切片大小 nslices = math.floor(len(news_data) / slice_size) ''' for i in range(0, nslices): with mysql_db.atomic(): NewsInfo.insert_many(news_data[i * slice_size: (i + 1) * slice_size]).on_conflict_ignore().execute() # 批量插入 # print(i) # 插入最后一个切片的数据 with mysql_db.atomic(): NewsInfo.insert_many(news_data[nslices*slice_size:]).on_conflict_ignore().execute() # 批量插入, 主键重复则忽略该条, https://segmentfault.com/a/1190000020265522 # print(nslices) ''' with mysql_db.atomic(): for batch in chunked(news_data, 300): # 一次300条 NewsInfo.insert_many(batch).on_conflict_ignore().execute() # 批量插入, 主键重复则忽略该条 mysql_db.close()