Exemple #1
0
def user_news_release():
    from app.models import Category, News

    from app.utils.qiniu.image_storage import storage

    from app import db

    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})

    # 取参数
    user_id = session["user_id"]
    # 1. 取到请求参数
    source = "个人发布"  # 来源
    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")
    # 参数转换
    try:
        category_id = int(category_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="参数有误")

    # 上传七牛云
    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.DBERR, errmsg="参数有误")

    # 2. 判断参数
    if not all([title, category_id, digest, index_image, content]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    new = News()
    new.title = title
    new.digest = digest
    new.source = source
    new.content = content
    new.index_image_url = "http://pv875q204.bkt.clouddn.com/" + key
    new.category_id = category_id
    new.user_id = user_id
    # 1代表待审核状态
    new.status = 1

    try:
        # 插入提交数据
        db.session.add(new)
        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")
Exemple #2
0
def news_release():
    if request.method == "GET":
        categories = []
        try:
            # 获取所有的分类数据
            categories = Category.query.all()
        except Exception as e:
            current_app.logger.error(e)

        # 定义列表保存分类数据
        categories_dict_list = []

        for category in categories:
            # 获取字典
            cate_dict = category.to_dict()
            # 拼接内容
            categories_dict_list.append(cate_dict)

        # 移除"最新"分类
        categories_dict_list.pop(0)

        data = {
            "categories": categories_dict_list
        }
        # 返回内容
        return render_template('profile/user_news_release.html', data=data)

    # POST提交,执行发布新闻操作

    # 1.获取要提交的数据
    title = request.form.get("title")
    source = "个人发布"
    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, source, digest, content, index_image, category_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

    # 1.2尝试读取图片
    try:
        index_image = index_image.read()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

    # 2.将标题图片上传到七牛
    try:
        key = pic_storage(index_image)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="上传图片失败")

    # 3.初始化新闻模型,并设置相关数据
    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
    # 1代表待审核状态
    news.status = 1

    # 4.保存到数据库
    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="保存数据失败")

    # 5.返回结果
    return jsonify(errno=RET.OK, errmsg="发布成功,等待审核")