# find the project root directory
import os.path
root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

# initiate the flask app
from flask import Flask
app = Flask(__name__, static_folder=os.path.join(root, 'public'))

# runtime/local configuration via config.py
cfg_file_path = os.path.join(root, 'config.py')
app.config.from_pyfile(cfg_file_path)

if not app.debug:
	# enable bytecode caching for templates
	from jinja2 import FileSystemBytecodeCache
	from werkzeug.datastructures import ImmutableDict
	j2cachedir = os.path.join(root, 'tmp', 'j2cache')
	app.jinja_options = ImmutableDict(
		extensions = ['jinja2.ext.with_'],
		bytecode_cache = FileSystemBytecodeCache(j2cachedir, '%s.cache'),
	)

	# set up logging to stderr
	import logging, sys
	file_handler = logging.StreamHandler(sys.stderr)
	file_handler.setLevel(logging.WARNING)
	app.logger.addHandler(file_handler)

# replace the json encoder
from qdb.utils import CustomJSONEncoder
app.json_encoder = CustomJSONEncoder

# register routes
import qdb.routes

# initialize the database session
from qdb.database import init_db, db_session
init_db()

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

# inject the pending_count variable as a jinja global
from qdb.models import Quote
from flask import session
@app.context_processor
def inject_pending_count():
	if not session.get('logged_in'):
		return dict()

	query = db_session.query(Quote) \
		.filter(Quote.approved == False)
	count = query.count()
	return {'pending_count': count}

@@ -1215,52 +1215,30 @@
 ect 
-the pending_count variable as a jinja global
+global jinja variables
 %0Afro
@@ -1320,40 +1320,48 @@
 def 
+j
 inj
-ect_pending_count():
+a_globals():%0A%09context = dict()%0A
 %0A%09if 
-not 
 sess
@@ -1388,29 +1388,32 @@
 :%0A%09%09
-return dict()%0A%0A%09query
+context%5B'pending_count'%5D
  = d
@@ -1439,16 +1439,17 @@
 te) %5C%0A%09%09
+%09
 .filter(
@@ -1476,23 +1476,14 @@
 lse)
-%0A%09count = query
+ %5C%0A%09%09%09
 .cou
@@ -1491,16 +1491,17 @@
 t()%0A
+%0A
 %09return 
 %7B'pe
@@ -1500,29 +1500,12 @@
 urn 
-%7B'pending_
 co
-u
 nt
-': count%7D
+ext
 %0A

make global jinja context handler more extendable