-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
137 lines (104 loc) · 4.08 KB
/
app.py
File metadata and controls
137 lines (104 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Import required modules
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime, date, timedelta
import os
# Initialize Flask app
app = Flask(__name__)
# Configure SQLAlchemy
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{os.path.join(basedir, "instance", "habits.db")}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Habit model
class Habit(db.Model):
__tablename__ = 'habit'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
last_check_in = db.Column(db.Date)
logs = db.relationship('HabitLog', backref='habit',
lazy='select', cascade='all, delete-orphan')
def __repr__(self):
return f'<Habit {self.name}>'
def calculate_streak(self):
logs = HabitLog.query.filter_by(
habit_id=self.id).order_by(HabitLog.date.desc()).all()
if not logs:
return 0
streak = 1
today = date.today()
# If the most recent log isn't from today or yesterday, streak is just 0 or 1
if logs[0].date != today and logs[0].date != today - timedelta(days=1):
# Return 1 if checked in today, otherwise 0
return 1 if logs[0].date == today else 0
# Count consecutive days
for i in range(len(logs) - 1):
# If this log and the next one are consecutive days
if logs[i].date - logs[i+1].date == timedelta(days=1):
streak += 1
else:
# Break the streak when we find a gap
break
return streak
# HabitLog model
class HabitLog(db.Model):
__tablename__ = 'habit_log'
id = db.Column(db.Integer, primary_key=True)
habit_id = db.Column(db.Integer, db.ForeignKey(
'habit.id', ondelete='CASCADE'), nullable=False)
date = db.Column(db.Date, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return f'<HabitLog for habit_id {self.habit_id} on {self.date}>'
# Create all database tables
with app.app_context():
db.create_all()
@app.route('/')
def index():
habits = Habit.query.all()
today = date.today()
return render_template('index.html', habits=habits, today=today)
@app.route('/add_habit', methods=['POST'])
def add_habit():
name = request.form['name']
new_habit = Habit(name=name)
db.session.add(new_habit)
db.session.commit()
return redirect(url_for('index'))
@app.route('/check_in/<int:habit_id>')
def check_in(habit_id):
habit = Habit.query.get_or_404(habit_id)
today = date.today()
if habit.last_check_in != today:
# Check if this habit already has a log for today
existing_log = HabitLog.query.filter_by(
habit_id=habit.id, date=today).first()
if not existing_log:
# Create a new log entry
log = HabitLog(habit_id=habit.id, date=today)
db.session.add(log)
habit.last_check_in = today
db.session.commit()
return redirect(url_for('index'))
@app.route('/habit/<int:habit_id>')
def habit_detail(habit_id):
habit = Habit.query.get_or_404(habit_id)
logs = HabitLog.query.filter_by(
habit_id=habit.id).order_by(HabitLog.date.desc()).all()
# Get data for last 7 days for display
today = date.today()
last_7_days = []
for i in range(6, -1, -1):
day = today - timedelta(days=i)
completed = any(log.date == day for log in logs)
last_7_days.append({'date': day, 'completed': completed})
return render_template('habit_detail.html', habit=habit, logs=logs, last_7_days=last_7_days)
@app.route('/delete/<int:habit_id>')
def delete_habit(habit_id):
habit = Habit.query.get_or_404(habit_id)
db.session.delete(habit)
db.session.commit()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)