mirror of
https://github.com/tmdinosaurcenter/kiosk-guestbook.git
synced 2025-09-15 11:43:32 -06:00
Migrate application from Node.js to Flask, removing Node dependencies and adding Flask requirements. Foregoing the Google Form for a simpler MVP using a simple HTML form and SQLite db
This commit is contained in:
46
app.py
Normal file
46
app.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from flask import Flask, render_template, request, redirect, url_for
|
||||
import sqlite3
|
||||
|
||||
app = Flask(__name__)
|
||||
DATABASE = 'guestbook.db'
|
||||
|
||||
def init_db():
|
||||
conn = sqlite3.connect(DATABASE)
|
||||
c = conn.cursor()
|
||||
c.execute('''
|
||||
CREATE TABLE IF NOT EXISTS guests (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
location TEXT NOT NULL,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def index():
|
||||
if request.method == 'POST':
|
||||
name = request.form.get('name')
|
||||
email = request.form.get('email')
|
||||
location = request.form.get('location')
|
||||
if name and email and location:
|
||||
conn = sqlite3.connect(DATABASE)
|
||||
c = conn.cursor()
|
||||
c.execute('INSERT INTO guests (name, email, location) VALUES (?, ?, ?)',
|
||||
(name, email, location))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return redirect(url_for('index'))
|
||||
# Retrieve guest entries to display on the page.
|
||||
conn = sqlite3.connect(DATABASE)
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT name, email, location, timestamp FROM guests ORDER BY id DESC')
|
||||
guests = c.fetchall()
|
||||
conn.close()
|
||||
return render_template('index.html', guests=guests)
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_db()
|
||||
app.run(host='0.0.0.0', port=5000)
|
Reference in New Issue
Block a user