import os
import sqlite3
from flask import Flask, render_template, request, g, redirect, url_for
from werkzeug.utils import secure_filename

#initialize the Flask object
app = Flask(__name__)

#change config settings from default
app.config.update(dict(
  DATABASE=os.path.join(app.root_path, 'salsa_votes.db'),
  DEBUG=True,
  SECRET_KEY='123456',
  USERNAME='admin',
  PASSWORD='default'
))

#upload settings
UPLOAD_FOLDER = '/Users/barrett/Code/salsa-vote/static/img'
ALLOWED_EXTENSIONS = set(['jpg', 'png', 'jpeg', 'gif'])

#the next few methods are for database connections
def connect_db():
  #Connects to the specific database
  #rv transforms db rows into dicts (i think)
  rv = sqlite3.connect(app.config['DATABASE'])
  rv.row_factory = sqlite3.Row
  return rv

#method to create the database
def init_db():
  #must explicitly declare app context bc there's no request yet
  with app.app_context():
    db = get_db()
    with app.open_resource('schema.sql', mode='r') as f:
      db.cursor().executescript(f.read())
    db.commit()

def get_db():
  #Opens db connection if one doesn't exist
  if not hasattr(g, 'sqlite_db'):
    g.sqlite_db = connect_db()
  return g.sqlite_db

#i think this executes at the end of every request
# (indeed, that's what the teardown marker is for)
#if process executes properly, no error occurs
#if error occurs, it's passed to this method
@app.teardown_appcontext
def close_db(error):
  if hasattr(g, 'sqlite_db'): #checks to see if db conn exists
    g.sqlite_db.close()

'''
@app.route('/salsa')
def vote_salsa():
  db = get_db()
  cur = db.execute("SELECT * FROM salsas WHERE type='salsa' ORDER BY table_no ASC")
  salsa_entries = cur.fetchall()
  return render_template('salsa_vote.html', salsa_entries = salsa_entries)
'''

if __name__ == '__main__':
  app.run()
