Beispiel #1
0
def create_topic():
    form = TopicForm(request.form)

    form.tags.choices = [form.tags.data]

    if not form.validate():
        return render_template("topics/new.html", form=form)

    subject = form.subject.data
    body = form.body.data
    author_id = current_user.id

    initial_post = Post(body)
    initial_post.author_id = author_id
    topic = Topic(subject, author_id)
    topic.posts.append(initial_post)

    tag_names = form.tags.data[0].split(",")
    for tag_name in tag_names:
        tag = Tag.query.filter_by(name=tag_name).first()
        if not tag:
            tag = Tag(tag_name)
        topic.tags.append(tag)

    db.session().add(topic)
    db.session().commit()

    return redirect(url_for("topics_index"))
Beispiel #2
0
def create_post(topic_id):
    topic = Topic.query.get(topic_id)

    if topic is None:
        return redirect(url_for("topics_index"))

    form = PostForm(request.form)

    if not form.validate():
        return render_template("topics/single.html", topic=topic, form=form)

    body = request.form.get("body")
    post = Post(body)
    post.topic = topic
    post.author_id = current_user.id

    db.session().add(post)
    db.session().commit()

    return redirect(url_for("view_topic", topic_id=topic_id))