Skip to content

Commit

Permalink
Merge pull request #17 from NetworkRehab/points_bank
Browse files Browse the repository at this point in the history
Add PointsBank model and update index to display total points
  • Loading branch information
ndonathan authored Nov 1, 2024
2 parents 985e682 + b75a279 commit 5571798
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
41 changes: 39 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Task(db.Model):
def __repr__(self):
return f"<Task {self.name}>"


class PointsBank(db.Model):
id = db.Column(db.Integer, primary_key=True)
total_points = db.Column(db.Integer, default=0)

class CompletedTask(db.Model):
id = db.Column(db.Integer, primary_key=True)
task_id = db.Column(db.Integer, nullable=False)
Expand All @@ -47,9 +52,15 @@ class CompletedTask(db.Model):
def __repr__(self):
return f"<CompletedTask {self.name} at {self.completed_at}>"


# 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():
Expand All @@ -63,8 +74,10 @@ def index():
if task.completed_at:
date_str = task.completed_at.split(' ')[0]
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():
Expand Down Expand Up @@ -113,6 +126,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'))

@app.route('/delete_completed_task/<int:task_id>', methods=['POST'])
def delete_completed_task(task_id):
completed_task = CompletedTask.query.get_or_404(task_id)
Expand Down
10 changes: 10 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ <h1>Daily Tasks</h1>
<button onclick="resetSliders()">Reset</button>
<button onclick="clearTasks()">Clear</button>

<h2>Points Bank: {{ bank_total }}</h2>
<form action="/add_to_bank" method="post" style="display:inline-block;">
<input type="number" name="amount" placeholder="Amount to Add">
<input type="submit" value="Add to Bank">
</form>
<form action="/remove_from_bank" method="post" style="display:inline-block;">
<input type="number" name="amount" placeholder="Amount to Remove">
<input type="submit" value="Remove from Bank">
</form>

<h2>Completed Tasks</h2>
<div class="completed-tasks-container">
<table class="completed-tasks">
Expand Down

0 comments on commit 5571798

Please sign in to comment.