<commit_before>import os.path
from flask import (Flask, url_for, render_template, jsonify,
	redirect, abort, flash, request, session)
app = Flask(__name__)

dirname = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
cfg_file_path = os.path.join(dirname, 'config.py')
app.config.from_pyfile(cfg_file_path)


# set up logging
if not app.debug:
    import logging, sys
    file_handler = logging.StreamHandler(sys.stderr)
    file_handler.setLevel(logging.WARNING)
    app.logger.addHandler(file_handler)


from qdb.models import Quote
from qdb.database import init_db, db_session
from qdb.utils import Paginator, CustomJSONEncoder
app.json_encoder = CustomJSONEncoder
from datetime import datetime
from sqlalchemy.sql.expression import func


# ROUTES
@app.route('/')
def home():
	search = request.args.get('s')
	page = int(request.args.get('p', 1))

	query = db_session.query(Quote) \
		.filter(Quote.approved == True) \
		.order_by(Quote.submitted_at.desc())

	if search:
		query = query.filter(Quote.body.like('%'+search+'%'))

	if page > 0:
		paginator = Paginator(query, page, request.url)
		quotes = paginator.items

		if request.headers.get('Accept') == 'application/json':
			return jsonify(quotes=paginator)
	else:
		paginator = None
		quotes = query.all()

		if request.headers.get('Accept') == 'application/json':
			return jsonify(quotes=quotes)

	return render_template('list.html.jinja', quotes=quotes, paginator=paginator)


@app.route('/random')
def random():
	query = db_session.query(Quote) \
		.filter(Quote.approved == True) \
		.order_by(func.random())

	quotes = query.all()

	return render_template('list.html.jinja', quotes=quotes)

@app.route('/submit', methods=['GET', 'POST'])
def submit():
	if request.method == 'GET':
		return render_template('form.html.jinja', form_action=url_for('submit'))

	body = Quote.prepare(request.form['body'])
	quote = Quote(body, datetime.now())
	db_session.add(quote)
	db_session.commit()

	flash('Quote added!')
	return redirect(url_for('home'))

@app.route('/pending')
def pending():
	if not session.get('logged_in'):
		flash('You must be logged in to do that!')
		return redirect(url_for('home'))

	query = db_session.query(Quote) \
		.filter(Quote.approved == False) \
		.order_by(Quote.submitted_at.desc())<commit_msg>limit number of random quotes<commit_after>import os.path
from flask import (Flask, url_for, render_template, jsonify,
	redirect, abort, flash, request, session)
app = Flask(__name__)

dirname = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
cfg_file_path = os.path.join(dirname, 'config.py')
app.config.from_pyfile(cfg_file_path)


# set up logging
if not app.debug:
    import logging, sys
    file_handler = logging.StreamHandler(sys.stderr)
    file_handler.setLevel(logging.WARNING)
    app.logger.addHandler(file_handler)


from qdb.models import Quote
from qdb.database import init_db, db_session
from qdb.utils import Paginator, CustomJSONEncoder
app.json_encoder = CustomJSONEncoder
from datetime import datetime
from sqlalchemy.sql.expression import func


# ROUTES
@app.route('/')
def home():
	search = request.args.get('s')
	page = int(request.args.get('p', 1))

	query = db_session.query(Quote) \
		.filter(Quote.approved == True) \
		.order_by(Quote.submitted_at.desc())

	if search:
		query = query.filter(Quote.body.like('%'+search+'%'))

	if page > 0:
		paginator = Paginator(query, page, request.url)
		quotes = paginator.items

		if request.headers.get('Accept') == 'application/json':
			return jsonify(quotes=paginator)
	else:
		paginator = None
		quotes = query.all()

		if request.headers.get('Accept') == 'application/json':
			return jsonify(quotes=quotes)

	return render_template('list.html.jinja', quotes=quotes, paginator=paginator)


@app.route('/random')
def random():
	query = db_session.query(Quote) \
		.filter(Quote.approved == True) \
		.order_by(func.random()) \
		.limit(10)

	quotes = query.all()

	return render_template('list.html.jinja', quotes=quotes)

@app.route('/submit', methods=['GET', 'POST'])
def submit():
	if request.method == 'GET':
		return render_template('form.html.jinja', form_action=url_for('submit'))

	body = Quote.prepare(request.form['body'])
	quote = Quote(body, datetime.now())
	db_session.add(quote)
	db_session.commit()

	flash('Quote added!')
	return redirect(url_for('home'))

@app.route('/pending')
def pending():
	if not session.get('logged_in'):
		flash('You must be logged in to do that!')
		return redirect(url_for('home'))

	query = db_session.query(Quote) \
		.filter(Quote.approved == False) \
		.order_by(Quote.submitted_at.desc())
{'fraction_non_alphanumeric': 0.1503791982665222, 'fraction_numerical': 0.0013001083423618636, 'mean_word_length': 5.065703022339028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '456718', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1554, 'n_words': 326}