Esempio n. 1
0
def init():
    if request.method == 'GET':
        if g.blog:
            return redirect(url_for('dashboard'))
        else:
            g.blog = DEFAULT_CONFIG
            return render_template('init.html')
    elif request.method == 'POST':
        for key in request.form:
            if not request.form[key]:
                flash(MSG_SETTINGS_FILL)
                g.blog = request.form
                return render_template('init.html')

        from yak import init
        init(blog_dir, request.form)
        g.blog = read_config(blog_dir)

        hg_init(blog_dir)
        for post in publish():
            source = os.path.join(blog_dir, 'publish',  post)
            hg_add(source)
            hg_commit(source, 'new blog')

        bake_blog()
        flash(MSG_INIT_SUCCESS)
        return redirect(url_for('dashboard'))
Esempio n. 2
0
def new():
    """
        Actions are sent as forms, and redirects are made by `referer`
    """
    if request.method == 'GET':
        filename, markdown = default_post()
        return render_template('new_post.html',
                filename=filename, markdown=markdown)
    elif request.method == 'POST':
        filename = request.form['filename']
        if not filename.endswith('.md'):
            filename += '.md'
        markdown = request.form['markdown']
        action = request.form['action']
        
        valid_filename = is_valid_filename(filename)
        if not valid_filename:
            flash(MSG_POST_FILENAME_INVALID)
        elif get_location(filename):
            flash(MSG_POST_EXISTS)
        elif not is_valid_post(markdown, valid_filename['published']):
            flash(MSG_POST_CONTENT_INVALID)
        else:
            if 'Draft' in action:
                dest = 'drafts'
                flash(MSG_POST_SAVED.format(filename[:-3]))
            elif 'Publish' in action:
                dest = 'publish'
                flash(MSG_POST_PUBLISHED.format(filename[:-3]))

            with open(os.path.join(blog_dir, dest, filename),
                    'w', 'utf-8') as f:
                f.write(markdown)
            if 'Publish' in action:
                bake_blog()

            source = os.path.join(blog_dir, get_location(filename))
            hg_add(source)
            hg_commit(source, 'new post {} in {}'.format(filename, dest))
            return redirect(url_for('posts'))
        return render_template(request.form['referer'],
                filename=filename, markdown=markdown)