from flask import Flask, render_template, request, url_for
import sqlite3

foo = {'cat': 1, 'dog': 0}

with sqlite3.connect('cat_dog.db') as conn:
	c = conn.cursor()
	c.execute('CREATE TABLE IF NOT EXISTS preference(name TEXT, fav_col TEXT, cat INTEGER)')
	c.close()

app = Flask(__name__)

@app.route("/")
def index():
	return render_template("webpage.html")

@app.route("/status", methods=["POST"])
def status():
	name = request.form.get("unique_name")

	with sqlite3.connect('cat_dog.db') as conn:
		c = conn.cursor()
		c.execute("SELECT EXISTS(SELECT name FROM preference WHERE name=?)", (name,))
		(swit, ) = c.fetchone()
		if swit:
			return render_template("stat.html", outline = False, link = request.url_root)
		else:
			c.execute('INSERT INTO preference (name, fav_col, cat) VALUES(?, ?, ?)', (name, request.form.get("fav_col"), foo[request.form.get("animal")]))
			conn.commit()
			return render_template("stat.html", outline = True, link = request.url_root)
