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

    if not form.validate():
        return render_template("forum/topic_new.html", form=form)

    name = form.name.data

    new_topic = Topic(name, g.area.id)
    new_topic.account_id = current_user.id

    db.session().add(new_topic)
    db.session().commit()

    message = form.message.data

    # Remove possible ghost messages because SQLAlchemy reuses ids
    Message.query.filter_by(topic_id=new_topic.id).delete()

    new_msg = Message(message, new_topic.id)
    new_msg.account_id = current_user.id

    db.session().add(new_msg)
    db.session().commit()

    return redirect(url_for("topic.topic", area_name=g.area.name, created=new_topic.created))
示例#2
0
def test_destroy_with_comment(client):
    topic = Topic.create({ 'title': 'title1', 'body': 'body1' })
    comment1 = Comment.create({ 'topic_id': topic.id(), 'body': 'body1' })
    comment2 = Comment.create({ 'topic_id': topic.id(), 'body': 'body2' })
    topic.destroy()
    assert(not Topic.is_exists(topic.id()))
    assert(not Comment.is_exists(comment1.id()))
    assert(not Comment.is_exists(comment2.id()))
示例#3
0
def new():
    if request.method == 'POST':
        Topic.create({
            'title': request.form['title'],
            'body': request.form['body']
        })
        return redirect(url_for('topic.index'))
    else:
        return render_template('topics/new.html')
示例#4
0
def test_update(client):
    topic = Topic.create({ 'title': 'title2', 'body': 'body2' })
    assert 'title2', topic.title()
    assert 'body2', topic.body()
    topic.update({ 'title': 'title3', 'body': 'body3' })
    assert 'title3', topic.title()
    assert 'body3', topic.body()
    topic = Topic.find(topic.id())
    assert 'title3', topic.title()
    assert 'body3', topic.body()
示例#5
0
def edit(id):
    topic = Topic.find(id)
    if request.method == 'POST':
        topic.update({
            'title': request.form['title'],
            'body': request.form['body']
        })
        return redirect(url_for('topic.show', id=id))
    else:
        topic = Topic.find(id)
        return render_template('topics/edit.html', topic=topic)
示例#6
0
def forum_main():
    areas = Area.query.all()
    most_active_area = Area.find_area_with_most_messages()
    message_count = Message.find_message_count()
    hot_topic = Topic.find_hot_topic()
    topic_count = Topic.find_topic_count()
    return render_template("forum.html",
                           areas=areas,
                           most_active_area=most_active_area,
                           message_count=message_count,
                           hot_topic=hot_topic,
                           topic_count=topic_count)
示例#7
0
def test_get_topics_index(client):
    expected_title = 'indexindex_title'
    topic = Topic.create({'title': expected_title, 'body': 'a'})
    response = client.get('/topics/')
    assert response.status_code == 200
    actual = response.get_data()
    assert expected_title in actual
示例#8
0
def new_comment(id):
    topic = Topic.find(id)
    if request.method == 'POST':
        Comment.create({
            'topic_id': topic.id(),
            'body': request.form['comment_body']
        })
        return redirect(url_for('topic.show', id=id))
    else:
        return render_template('comments/new.html', topic=topic)
示例#9
0
def test_all(client):
    topic1 = Topic.create({ 'title': 'title1', 'body': 'body1' })
    topic2 = Topic.create({ 'title': 'title2', 'body': 'body2' })
    topics = Topic.all()
    assert len(topics) == 2
    assert Topic.find(topic1.id()).title() == topic1.title()
    assert Topic.find(topic1.id()).body() == topic1.body()
    assert Topic.find(topic2.id()).title() == topic2.title()
    assert Topic.find(topic2.id()).body() == topic2.body()

    topics = Topic.all(limit=1)
    assert len(topics) == 1
示例#10
0
def test_get_topics_edit(client):
    title = 'test_get_topics_edit_title'
    body = 'test_get_topics_edit_body'
    topic = Topic.create({'title': title, 'body': body})

    response = client.get('/topics/%s/edit' % topic.id())
    assert response.status_code == 200
    actual = response.get_data()
    assert title in actual
    assert body in actual
示例#11
0
def test_post_topics_edit(client):
    title = 'test_get_topics_edit_title'
    body = 'test_get_topics_edit_body'
    topic = Topic.create({'title': title, 'body': body})
    response = client.post('/topics/%s/edit' % topic.id(),
                           data={
                               'title': 'titletitle',
                               'body': 'bodybody'
                           })
    assert response.status_code == 302

    response = client.get('/topics/%s' % topic.id())
    actual = response.get_data()
    assert 'titletitle' in actual
    assert 'bodybody' in actual
示例#12
0
def test_post_topics_delete(client):
    topic = Topic.create({'title': 'title', 'body': 'body'})
    response = client.post('/topics/%s/delete' % topic.id(),
                           data={'id': topic.id()})
    assert response.status_code == 302
    assert (not Topic.is_exists(topic.id()))
示例#13
0
def test_destroy(client):
    topic = Topic.create({ 'title': 'title2', 'body': 'body2' })
    topic.destroy()
    assert(not Topic.is_exists(topic.id()))
示例#14
0
def index():
    topics = Topic.all()
    return render_template('topics/index.html', topics=topics)
示例#15
0
def test_find(client):
    topic = Topic.create({ 'title': 'title2', 'body': 'body2' })
    topic = Topic.find(topic.id())
    assert 'title2', topic.title()
    assert 'body2', topic.body()
    assert type(topic) == Topic
示例#16
0
def test_new(client):
    topic = Topic({ 'title': 'fi', 'body': 'body' })
    assert 'fi', topic.title
    assert 'body', topic.body
示例#17
0
def show(id):
    topic = Topic.find(id)
    comments = topic.comments()
    return render_template('topics/show.html', topic=topic, comments=comments)
示例#18
0
def delete(id):
    topic = Topic.find(id)
    topic.destroy()
    return redirect(url_for('topic.index'))
示例#19
0
def test_destroy(client):
    topic = Topic.create({'title': 'title1', 'body': 'body1'})
    comment = Comment.create({'topic_id': topic.id(), 'body': 'body1'})
    comment.destroy()
    assert (Topic.is_exists(topic.id()))
    assert (not Comment.is_exists(comment.id()))