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)


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 = 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+'%'))

	paginator = Paginator(query, page, request.url)
	quotes = paginator.items

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

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

@app.route('/random')
def random():
	page = request.args.get('p', 1)

	query = db_session.query(Quote) \
		.filter(Quote.approved == True) \
		.order_by(func.random())

	paginator = Paginator(query, page, request.url)
	quotes = paginator.items

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

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

	quote = Quote(request.form['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())

	quotes = query.all()

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

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

@app.route('/login')
def login():
	if request.args.get('password') == app.config.get('PASSWORD'):
		session['logged_in'] = True
		flash('You are now logged in!')
	else:
		flash('Incorrect password!')

	return redirect(url_for('home'))

@app.route('/<int:quote_id>', methods=['GET', 'POST', 'DELETE'])
def show(quote_id):
	quote = db_session.query(Quote) \
		.filter(Quote.id == quote_id) \
		.first()

	if not quote:
		return abort(404)

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

		return render_template('show.html.jinja', quote=quote)
	elif request.method == 'DELETE':
		db_session.delete(quote)
		db_session.commit()
		return 'OK'
	else:
		return 'not yet implemented'

@app.route('/<int:quote_id>/vote', methods=['POST'])
def vote(quote_id):
	quote = db_session.query(Quote) \
		.filter(Quote.id == quote_id) \
		.first()

	if not quote:
		return abort(404)

	return 'not yet implemented'

@app.route('/<int:quote_id>/approve', methods=['POST'])
def approve(quote_id):
	quote = db_session.query(Quote) \
		.filter(Quote.id == quote_id) \
		.first()

	if not quote:
		return abort(404)

	quote.approved = True
	db_session.add(quote)
	db_session.commit()
	return 'OK'


# EXTRA
init_db()

@app.teardown_appcontext
def shutdown_session(exception=None):
	db_session.remove()

@@ -603,32 +603,36 @@
 et('s')%0A%09page = 
+int(
 request.args.get
@@ -631,32 +631,33 @@
 args.get('p', 1)
+)
 %0A%0A%09query = db_se
@@ -818,24 +818,39 @@
 arch+'%25'))%0A%0A
+%09if page %3E 0:%0A%09
 %09paginator =
@@ -878,32 +878,33 @@
 e, request.url)%0A
+%09
 %09quotes = pagina
@@ -914,16 +914,17 @@
 .items%0A%0A
+%09
 %09if requ
@@ -964,32 +964,33 @@
 lication/json':%0A
+%09
 %09%09return jsonify
@@ -1007,16 +1007,157 @@
 ginator)
+%0A%09else:%0A%09%09paginator = None%0A%09%09quotes = query.all()%0A%0A%09%09if request.headers.get('Accept') == 'application/json':%0A%09%09%09return jsonify(quotes=quotes)
 %0A%0A%09retur
@@ -1222,32 +1222,33 @@
 tor=paginator)%0A%0A
+%0A
 @app.route('/ran

make it possible to list all quotes without paginating