From 23748e161c34ea537ee97383953e1366deb3ef04 Mon Sep 17 00:00:00 2001 From: Nick Donathan <31076657+ndonathan@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:15:59 -0500 Subject: [PATCH] Add PointsBank model and update index to display total points - Introduced PointsBank model to track total points. - Initialized PointsBank in the database if it doesn't exist. - Updated index route to include bank total in the rendered template. - Added routes to add and remove points from the bank. --- app.py | 37 ++++++++++++++++++++++++++++++++++++- templates/index.html | 10 ++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 7b069e0..6d792b9 100644 --- a/app.py +++ b/app.py @@ -22,9 +22,18 @@ class Task(db.Model): def __repr__(self): return f"" +class PointsBank(db.Model): + id = db.Column(db.Integer, primary_key=True) + total_points = db.Column(db.Integer, default=0) + # Create the database and tables with app.app_context(): db.create_all() + # Initialize PointsBank if it doesn't exist + if not PointsBank.query.first(): + bank = PointsBank(total_points=0) + db.session.add(bank) + db.session.commit() @app.route('/') def index(): @@ -38,7 +47,9 @@ def index(): date_str = task.completed_at.split(' ')[0] # Extract MM-DD-YYYY totals_per_day[date_str] += task.points - return render_template('index.html', tasks=tasks, completed_tasks=completed_tasks, totals_per_day=totals_per_day) + bank = PointsBank.query.first() + bank_total = bank.total_points if bank else 0 + return render_template('index.html', tasks=tasks, completed_tasks=completed_tasks, totals_per_day=totals_per_day, bank_total=bank_total) @app.route('/add', methods=['POST']) def add(): @@ -77,6 +88,30 @@ def clear_tasks(): db.session.commit() return '', 204 # No Content +@app.route('/add_to_bank', methods=['POST']) +def add_to_bank(): + amount = request.form.get('amount', 0) + try: + amount = int(amount) + except ValueError: + amount = 0 + bank = PointsBank.query.first() + bank.total_points += amount + db.session.commit() + return redirect(url_for('index')) + +@app.route('/remove_from_bank', methods=['POST']) +def remove_from_bank(): + amount = request.form.get('amount', 0) + try: + amount = int(amount) + except ValueError: + amount = 0 + bank = PointsBank.query.first() + bank.total_points -= amount + db.session.commit() + return redirect(url_for('index')) + if __name__ == '__main__': import os debug_mode = os.getenv('FLASK_DEBUG', 'False').lower() in ['true', '1', 't'] diff --git a/templates/index.html b/templates/index.html index 67c98dc..edf0f99 100644 --- a/templates/index.html +++ b/templates/index.html @@ -156,6 +156,16 @@

Daily Tasks

+

Points Bank: {{ bank_total }}

+
+ + +
+
+ + +
+

Completed Tasks