<commit_before>from blog import app, db
from blog.posts import Post
from flask import Blueprint, abort, render_template, request, redirect, \
    url_for, g, flash
import math

blueprint = Blueprint('posts', __name__)

@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/page-<int:page>')
def list(page):
    if page <= 0:
        abort(404)

    start = (page - 1) * app.config['POSTS_PER_PAGE']
    end = page * app.config['POSTS_PER_PAGE']

    posts = db.session.query(Post).order_by(Post.id.desc())[start:end]

    if len(posts) == 0:
        flash("We don't have that many posts here.", 'error')
        abort(404, 'woot')

    max_page = int(math.ceil(
        db.session.query(Post).count() / float(app.config['POSTS_PER_PAGE'])))

    num = app.config['NAVIGATION_PAGE_COUNT'] / 2
    pages = [p for p in xrange(page - num, page + num + 1) if 1 < p < max_page]

    return render_template('posts/list.html', posts=posts, page=page,
            pages=pages, max_page=max_page)

def get_post(post_id):
    try:
        return db.session.query(Post).filter_by(id=post_id).one()
    except db.NoResultFound:
        app.logger.warning('Requested invalid post: "%s".', post_id)
        flash('That post does not exist.', 'error')
        abort(404)

@blueprint.route('/<int:id>')
def show(id):
    post = get_post(id)
    return render_template('posts/show.html', post=post)

def handle_get_and_preview(post, edit):
    if request.method == 'GET':
        return render_template('posts/edit.html', post=post, edit=edit)

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

    return None

@blueprint.route('/<int:id>/edit', methods=('GET', 'POST'))
def edit(id):
    if not g.user:
        abort(403)<commit_msg>Bug fix to previews.<commit_after>from blog import app, db
from blog.posts import Post
from flask import Blueprint, abort, render_template, request, redirect, \
    url_for, g, flash
import math

blueprint = Blueprint('posts', __name__)

@blueprint.route('/', defaults={'page': 1})
@blueprint.route('/page-<int:page>')
def list(page):
    if page <= 0:
        abort(404)

    start = (page - 1) * app.config['POSTS_PER_PAGE']
    end = page * app.config['POSTS_PER_PAGE']

    posts = db.session.query(Post).order_by(Post.id.desc())[start:end]

    if len(posts) == 0:
        flash("We don't have that many posts here.", 'error')
        abort(404, 'woot')

    max_page = int(math.ceil(
        db.session.query(Post).count() / float(app.config['POSTS_PER_PAGE'])))

    num = app.config['NAVIGATION_PAGE_COUNT'] / 2
    pages = [p for p in xrange(page - num, page + num + 1) if 1 < p < max_page]

    return render_template('posts/list.html', posts=posts, page=page,
            pages=pages, max_page=max_page)

def get_post(post_id):
    try:
        return db.session.query(Post).filter_by(id=post_id).one()
    except db.NoResultFound:
        app.logger.warning('Requested invalid post: "%s".', post_id)
        flash('That post does not exist.', 'error')
        abort(404)

@blueprint.route('/<int:id>')
def show(id):
    post = get_post(id)
    return render_template('posts/show.html', post=post)

def handle_get_and_preview(post, edit):
    if request.method == 'GET':
        return render_template('posts/edit.html', post=post, edit=edit)

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

    return None

@blueprint.route('/<int:id>/edit', methods=('GET', 'POST'))
def edit(id):
    if not g.user:
        abort(403)
{'fraction_non_alphanumeric': 0.16063941299790357, 'fraction_numerical': 0.010482180293501049, 'mean_word_length': 3.3672768878718538, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6300824', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1277, 'n_words': 293}