112 lines
3.5 KiB
Python
112 lines
3.5 KiB
Python
from flask import Flask, request, redirect, url_for, render_template_string
|
|
import os
|
|
import psycopg2
|
|
from psycopg2 import pool
|
|
|
|
app = Flask(__name__)
|
|
|
|
DB_HOST = os.getenv('DB_HOST', 'localhost')
|
|
DB_PORT = os.getenv('DB_PORT', '5432')
|
|
DB_NAME = os.getenv('DB_NAME', 'postgres')
|
|
DB_USER = os.getenv('DB_USER', 'postgres')
|
|
DB_PASS = os.getenv('DB_PASS', 'postgres')
|
|
|
|
connection_pool = psycopg2.pool.SimpleConnectionPool(
|
|
1, 10,
|
|
host=DB_HOST,
|
|
port=DB_PORT,
|
|
database=DB_NAME,
|
|
user=DB_USER,
|
|
password=DB_PASS
|
|
)
|
|
|
|
# Initialize database schema
|
|
with connection_pool.getconn() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute('''
|
|
CREATE TABLE IF NOT EXISTS shopping_items (
|
|
id SERIAL PRIMARY KEY,
|
|
item TEXT NOT NULL,
|
|
quantity INTEGER DEFAULT 1,
|
|
purchased BOOLEAN DEFAULT FALSE
|
|
);
|
|
''')
|
|
conn.commit()
|
|
|
|
HTML = """
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Shopping List</title>
|
|
<meta charset="UTF-8">
|
|
</head>
|
|
<body>
|
|
<h1>Shopping List</h1>
|
|
<form action="/add" method="post">
|
|
<input name="item" placeholder="Item" required>
|
|
<input name="quantity" type="number" value="1" min="1">
|
|
<button type="submit">Add</button>
|
|
</form>
|
|
<ul>
|
|
{% for id, name, qty, done in items %}
|
|
<li>
|
|
{{ name }} ({{ qty }})
|
|
{% if not done %}
|
|
<form action="/toggle/{{ id }}" method="post" style="display:inline">
|
|
<button type="submit">Mark Purchased</button>
|
|
</form>
|
|
{% else %}
|
|
<form action="/toggle/{{ id }}" method="post" style="display:inline">
|
|
<button type="submit">Unmark</button>
|
|
</form>
|
|
{% endif %}
|
|
<form action="/delete/{{ id }}" method="post" style="display:inline">
|
|
<button type="submit">Delete</button>
|
|
</form>
|
|
</li>
|
|
{% endfor %}
|
|
</ul>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
@app.route('/')
|
|
def index():
|
|
conn = connection_pool.getconn()
|
|
with conn.cursor() as cur:
|
|
cur.execute("SELECT id, item, quantity, purchased FROM shopping_items ORDER BY purchased, id")
|
|
items = cur.fetchall()
|
|
connection_pool.putconn(conn)
|
|
return render_template_string(HTML, items=items)
|
|
|
|
@app.route('/add', methods=['POST'])
|
|
def add_item():
|
|
item = request.form['item']
|
|
qty = int(request.form.get('quantity', 1))
|
|
conn = connection_pool.getconn()
|
|
with conn.cursor() as cur:
|
|
cur.execute("INSERT INTO shopping_items (item, quantity) VALUES (%s, %s)", (item, qty))
|
|
conn.commit()
|
|
connection_pool.putconn(conn)
|
|
return redirect(url_for('index'))
|
|
|
|
@app.route('/toggle/<int:item_id>', methods=['POST'])
|
|
def toggle_item(item_id):
|
|
conn = connection_pool.getconn()
|
|
with conn.cursor() as cur:
|
|
cur.execute("UPDATE shopping_items SET purchased=NOT purchased WHERE id=%s", (item_id,))
|
|
conn.commit()
|
|
connection_pool.putconn(conn)
|
|
return redirect(url_for('index'))
|
|
|
|
@app.route('/delete/<int:item_id>', methods=['POST'])
|
|
def delete_item(item_id):
|
|
conn = connection_pool.getconn()
|
|
with conn.cursor() as cur:
|
|
cur.execute("DELETE FROM shopping_items WHERE id=%s", (item_id,))
|
|
conn.commit()
|
|
connection_pool.putconn(conn)
|
|
return redirect(url_for('index'))
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5000) |