Created database class.
Tidied app.py so that it only contains controller logic. Added error handling for if number of entrants exceeds number of possible identifiers Updated test coverage to test for all identifiers, as well as error case
This commit is contained in:
60
web/app.py
60
web/app.py
@@ -1,52 +1,16 @@
|
||||
from flask import Flask, render_template, request
|
||||
from flask_bootstrap import Bootstrap
|
||||
import sqlite3
|
||||
import os
|
||||
import string
|
||||
|
||||
import db
|
||||
import config
|
||||
import pprint
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(config.BaseConfig)
|
||||
Bootstrap(app)
|
||||
my_db = db.Database(app.config['DB_PATH'])
|
||||
|
||||
|
||||
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:
|
||||
identifier = str(identifier)
|
||||
c.execute('''SELECT identifier FROM brewers WHERE identifier=?''', (identifier,))
|
||||
data = c.fetchone()
|
||||
if data is None:
|
||||
c.execute("INSERT INTO brewers (name,identifier) VALUES(?, ?)", (name, identifier))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return identifier
|
||||
|
||||
|
||||
@app.route('/')
|
||||
@@ -56,9 +20,19 @@ def hello_world():
|
||||
|
||||
@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)
|
||||
try:
|
||||
error = None
|
||||
identifier = my_db.get_identifier(request.form['name'])
|
||||
pass
|
||||
except StopIteration:
|
||||
identifier = ''
|
||||
error = 'Maximum entry limit reached - please contact Sean or Joe'
|
||||
pass
|
||||
return render_template('generate.html', brew_name=brew_name, brew_month=brew_month, identifier=identifier, error=error)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0')
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user