Beispiel #1
0
def scenic_edit(id=None):
    """
    编辑Subsection页面
    """
    form = ScenicForm()  # 实例化ScenicForm类
    form.area_id.choices = [(v.id, v.name)
                            for v in Area.query.all()]  # 为area_id添加属性
    form.submit.label.text = "Modify"  # 修改提交按钮的文字
    form.logo.validators = []  # 初始化为空
    scenic = Scenic.query.get_or_404(int(id))  # 根据ID查找Subsection是否存在
    if request.method == "GET":  # 如果以GET方式提交,获取所有Subsection信息
        form.is_recommended.data = scenic.is_recommended
        form.is_hot.data = scenic.is_hot
        form.area_id.data = scenic.area_id
        form.star.data = scenic.star
        form.content.data = scenic.content
        form.introduction.data = scenic.introduction
    if form.validate_on_submit():  # 如果提交表单
        data = form.data  # 获取表单数据
        scenic_count = Scenic.query.filter_by(
            title=data["title"]).count()  # 判断标题是否重复
        # 判断是否有重复数据
        if scenic_count == 1 and scenic.title != data["title"]:
            flash("Subsection already exists!", "err")
            return redirect(url_for('admin.scenic_edit', id=id))
        if not os.path.exists(current_app.config["UP_DIR"]):  # 判断目录是否存在
            os.makedirs(current_app.config["UP_DIR"])  # 创建目录
            os.chmod(current_app.config["UP_DIR"], "rw")  # 设置读写权限
        # 上传图片
        if form.logo.data != "":
            file_logo = secure_filename(form.logo.data.filename)  # 确保文件名安全
            scenic.logo = change_filename(file_logo)  #  更改文件名
            form.logo.data.save(current_app.config["UP_DIR"] +
                                scenic.logo)  # 保存文件

        # 属性赋值
        scenic.title = data["title"]
        scenic.address = data["address"]
        scenic.area_id = data["area_id"]
        scenic.star = int(data["star"])
        scenic.is_hot = int(data["is_hot"])
        scenic.is_recommended = int(data["is_recommended"])
        scenic.introduction = data["introduction"]
        scenic.content = data["content"]

        db.session.add(scenic)  # 添加数据
        db.session.commit()  # 提交数据
        flash("Successful modificaiton of subsection!", "ok")
        return redirect(url_for('admin.scenic_edit', id=id))  # 跳转到编辑页面
    return render_template("admin/subsection_edit.html",
                           form=form,
                           scenic=scenic)  # 渲染模板,传递变量
Beispiel #2
0
def scenic_edit(id=None):
    form = ScenicForm()
    form.area_id.choices = [(v.id, v.name) for v in Area.query.all()]
    form.submit.label.text = "修改"
    form.logo.validators = []
    scenic = Scenic.query.get_or_404(int(id))
    if request.method == "GET":
        form.is_recommended.data = scenic.is_recommended
        form.is_hot.data = scenic.is_hot
        form.area_id.data = scenic.area_id
        form.star.data = scenic.star
        form.content.data = scenic.content
        form.introduction.data = scenic.introduction
        form.title.data = scenic.title
        form.address.data = scenic.address
    if form.validate_on_submit():
        data = form.data
        scenic_count = Scenic.query.filter_by(title=data["title"]).count()

        if scenic_count == 1 and scenic.title != data["title"]:
            flash("景点已存在!", "err")
            return redirect(url_for("admin.scenic_edit", id=id))
        if not os.path.exists(current_app.config["UP_DIR"]):
            os.makedirs(current_app.config["UP_DIR"])
            os.chmod(current_app.config["UP_DIR"], "rw")
        if form.logo.data != "":
            file_logo = secure_filename(form.logo.data.filename)
            scenic.logo = file_logo
            form.logo.data.save(current_app.config["UP_DIR"] + scenic.logo)

        scenic.title = data["title"]
        scenic.address = data["address"]
        scenic.area_id = data["area_id"]
        scenic.star = int(data["star"])
        scenic.is_hot = int(data["is_hot"])
        scenic.is_recommended = int(data["is_recommended"])
        scenic.introduction = data["introduction"]
        scenic.content = data["content"]

        db.session.add(scenic)
        db.session.commit()
        flash("修改景区成功!", "ok")
        addOplog("修改景区" + scenic.title)
        return redirect(url_for("admin.scenic_edit", id=id))
    return render_template("admin/scenic_edit.html", form=form, scenic=scenic)
Beispiel #3
0
def scenic_add():
    """添加景区页面

    Args:

    Returns:

    Raise:

    """
    form = ScenicForm()
    form.area_id.choices = [(v.id, v.name) for v in Area.query.all()]
    if form.validate_on_submit():
        data = form.data
        scenic_count = Scenic.query.filter_by(title=data["title"]).count()
        if scenic_count == 1:
            flash("景点已存在!", "err")
            return redirect(url_for("admin.scenic_add"))
        # 生成唯一的文件名称
        file_logo = secure_filename(form.logo.data.filename)

        if not os.path.exists(current_app.config["UP_DIR"]):
            os.makedirs(current_app.config["UP_DIR"])
            os.chmod(current_app.config["UP_DIR"], "rw")
        # logo = change_filename(file_logo)
        logo = file_logo
        form.logo.data.save(current_app.config["UP_DIR"] + logo)
        scenic = Scenic(title=data["title"],
                        logo=logo,
                        star=int(data["star"]),
                        address=data["address"],
                        is_hot=int(data["is_hot"]),
                        is_recommended=int(data["is_recommended"]),
                        introduction=data["introduction"],
                        content=data["content"],
                        area_id=data["area_id"])
        db.session.add(scenic)
        db.session.commit()
        flash("添加成功!", "ok")
        return redirect(url_for("admin.scenic_add"))
    return render_template("admin/scenic_add.html", form=form)
Beispiel #4
0
def scenic_add():
    """
    添加Subsection页面
    """
    form = ScenicForm()  # 实例化form表单
    form.area_id.choices = [(v.id, v.name)
                            for v in Area.query.all()]  # 为area_id添加属性
    if form.validate_on_submit():
        data = form.data
        # 判断Subsection是否存在
        scenic_count = Scenic.query.filter_by(title=data["title"]).count()
        # 判断是否有重复数据。
        if scenic_count == 1:
            flash("Subsection already exists!", "err")
            return redirect(url_for('admin.scenic_add'))

        file_logo = secure_filename(form.logo.data.filename)  # 确保文件名
        if not os.path.exists(current_app.config["UP_DIR"]):
            # 创建一个多级目录
            os.makedirs(current_app.config["UP_DIR"])  # 创建文件夹
            os.chmod(current_app.config["UP_DIR"], "rw")  # 设置权限
        logo = change_filename(file_logo)  # 更改名称
        form.logo.data.save(current_app.config["UP_DIR"] + logo)  # 保存文件
        # 为Scenic类属性赋值
        scenic = Scenic(
            title=data["title"],
            logo=logo,
            star=int(data["star"]),
            address=data["address"],
            is_hot=int(data["is_hot"]),
            is_recommended=int(data["is_recommended"]),
            area_id=data["area_id"],
            introduction=data["introduction"],
            content=data["content"],
        )
        db.session.add(scenic)  # 添加数据
        db.session.commit()  # 提交数据
        addOplog("Add subsection" + data["title"])  # 添加日志
        flash("Successful addition of subsection!", "ok")  # 使用flash保存添加成功信息
        return redirect(url_for('admin.scenic_add'))  # 页面跳转
    return render_template("admin/subsection_add.html", form=form)  # 渲染模板