Example #1
0
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')
Example #2
0
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)
Example #3
0
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)
Example #4
0
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')