Exemple #1
0
def preprocess(post, edit):
    if request.method == 'GET':
        session['token'] = create_token()
        return render_template('posts/edit.html', post=post, edit=edit), None

    req_tags = request.form['tags']
    req_tags = map(prepare_tag_name, req_tags.split())
    tags = get_tags(req_tags)
    req_tags = ' '.join(req_tags)

    if request.form['action'] == 'preview':
        p = Post(request.form['title'], request.form['content'], g.user)
        p.id = post.id if post else -1
        return render_template('posts/edit.html', post=p, preview=True,
                edit=edit, tags=req_tags), None

    if tags is None:
        return render_template('posts/edit.html', post=post, edit=edit,
                tags=req_tags), None

    if not validate_token():
        flash('Tokens did not match. Try again.', 'error')

        if edit:
            return redirect(url_for('posts.edit', id=post.id)), None
        else:
            return redirect(url_for('posts.create')), None

    return None, tags
Exemple #2
0
def create():
    if not g.user:
        abort(403)

    res, tags = preprocess(None, False)
    if res:
        return res

    post = Post(request.form['title'], request.form['content'], g.user)
    post.tags = tags
    db.session.add(post)
    db.session.commit()
    app.logger.info('Created post %d', post.id)
    flash('Post created.', 'success')
    return redirect(url_for('posts.show', id=post.id))