コード例 #1
0
def post_edit(id=1):
    form = PostForm()
    form.category.choices = [(str(c.id), c.title)
                             for c in Category.query.all()]
    post = Post.query.get(id)

    if not post:
        abort(404)

    if form.validate_on_submit():
        post.title = form.title.data
        post.tags = conformTags(form)
        post.cat_id = form.category.data
        post.content = form.content.data.replace("$$id", str(post.id))
        db.session.commit()

        uploadFiles(form,
                    os.getcwd() + "/blog/static/posts/" + str(post.id) + "/")

        flash(f'Пост було опубліковано', 'success')
        return redirect(url_for('posts.post_edit', id=id))
    elif form.is_submitted():
        flash("Будь ласка, заповніть всі поля відповідно до вимог.", "error")
    form.title.data = post.title
    form.tags.data = ", ".join([tag.title for tag in post.tags])
    form.content.data = post.content
    return render_template(
        "add_post.html",
        title="Редагувати пост",
        form=form,
        files=listFiles(os.getcwd() + "/blog/static/posts/" + str(post.id) +
                        "/"))
コード例 #2
0
def post_add():
    form = PostForm()
    form.category.choices = [(str(c.id), c.title)
                             for c in Category.query.all()]

    if form.validate_on_submit():
        tags = conformTags(form)

        post = Post(title=form.title.data,
                    content=form.content.data,
                    tags=tags,
                    cat_id=form.category.data)
        post.content = post.content.replace("$$id", str(post.id))
        db.session.add(post)
        db.session.commit()

        uploadFiles(form,
                    os.getcwd() + "/blog/static/posts/" + str(post.id) + "/")

        flash(f'Пост було опубліковано', 'success')
        return redirect(url_for('posts.post_add'))
    elif form.is_submitted():
        flash("Користувач з таким логіном вже існує!", "error")

    return render_template("add_post.html",
                           categories=Category.query.all(),
                           title="Додати пост",
                           form=form,
                           files=[])