Working simple letter assignment without updated tests
This commit is contained in:
54
web/app.py
54
web/app.py
@@ -1,11 +1,61 @@
|
||||
from flask import Flask
|
||||
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 '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__':
|
||||
|
||||
11
web/templates/generate.html
Normal file
11
web/templates/generate.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Wigan Homebrew Club Competition Entry</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Wigan Homebrew Club Competition Entry</h1>
|
||||
<p>You have been entered into the competition to brew a {{ brew_name }}, for {{ brew_month }}. Please mark all of your bottlecaps with the following identifier: <strong>{{ identifier }}</strong></p>
|
||||
</body>
|
||||
</html>
|
||||
17
web/templates/index.html
Normal file
17
web/templates/index.html
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Wigan Homebrew Club Competition Entry</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Wigan Homebrew Club Competition Entry</h1>
|
||||
<p>The next competition brew will be a <strong>{{ brew_name }}</strong>. It will be judged at the meeting in <strong>{{ brew_month }}</strong></p>
|
||||
<p>If you would like to enter the competition, please enter your name below to generate a letter/number that will be used to identify your entry</p>
|
||||
<form action="{{ url_for('generate') }}" method="POST">
|
||||
<label for="name">Please enter your name and initial (eg: Sean C):</label>
|
||||
<input name="name" type="text" id="name"><br>
|
||||
<button type="submit">Enter Competition</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user