from flask import Flask, render_template, request import sqlite3 import os import string app = Flask(__name__) def get_db_connection(): db_path = os.environ.get('HBC_DB_PATH') if not db_path: raise Exception("DB Path not defined") return sqlite3.connect(db_path + '/hbc.db') def db_setup(): conn = get_db_connection() sql_create_table = """ CREATE TABLE IF NOT EXISTS brewers ( id integer PRIMARY KEY, name text NOT NULL, identifier text NOT NULL );""" c = conn.cursor() c.execute(sql_create_table) conn.close() db_setup() brew_name = "Fruit Beer" brew_month = "October" identifiers = list(string.ascii_uppercase) + list(range(1, 100)) def get_identifier(name): conn = get_db_connection() c = conn.cursor() for identifier in identifiers: c.execute('''SELECT identifier FROM brewers WHERE identifier=?''', (str(identifier),)) data = c.fetchone() if data is None: print("Assigning " + identifier + " to " + name) c.execute("INSERT INTO brewers (name,identifier) VALUES(?, ?)",(name, identifier) ) conn.commit() conn.close() return identifier @app.route('/') def hello_world(): return render_template('index.html', brew_name=brew_name, brew_month=brew_month) @app.route('/generate', methods=["POST"]) def generate(): identifier = get_identifier(request.form['name']) return render_template('generate.html', brew_name=brew_name, brew_month=brew_month, identifier=identifier) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')