forked from PemaCheki19/demoapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
316 lines (264 loc) · 10.5 KB
/
app.py
File metadata and controls
316 lines (264 loc) · 10.5 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
Main Flask application entry point
Handles app initialization, configuration, and routing
"""
import os
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager, login_user, logout_user, login_required, current_user
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize Flask app
app = Flask(__name__)
# Configuration
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-secret-key')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'postgresql://todouser:todopass@localhost:5432/todoapp')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Initialize extensions
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# Import and initialize models with database instance
from models import init_models
User, Task = init_models(db)
# Initialize Flask-Migrate after models are imported
migrate = Migrate(app, db)
@login_manager.user_loader
def load_user(user_id):
"""Load user for Flask-Login"""
return User.query.get(int(user_id))
# Routes
@app.route('/health')
def health():
"""Health check endpoint for monitoring"""
return jsonify({
'status': 'healthy',
'timestamp': datetime.utcnow().isoformat(),
'version': '1.0.0'
})
@app.route('/')
def index():
"""Home page - redirect to dashboard if logged in, otherwise to login"""
if current_user.is_authenticated:
return redirect(url_for('dashboard'))
return redirect(url_for('login'))
@app.route('/register', methods=['GET', 'POST'])
def register():
"""User registration"""
if request.method == 'POST':
username = request.form.get('username', '').strip()
email = request.form.get('email', '').strip()
password = request.form.get('password', '')
# Basic validation
if not username or not email or not password:
flash('All fields are required', 'error')
return render_template('register.html')
if len(password) < 6:
flash('Password must be at least 6 characters', 'error')
return render_template('register.html')
# Check if user exists
if User.query.filter_by(username=username).first():
flash('Username already exists', 'error')
return render_template('register.html')
if User.query.filter_by(email=email).first():
flash('Email already registered', 'error')
return render_template('register.html')
# Create new user
user = User(
username=username,
email=email,
password_hash=generate_password_hash(password)
)
try:
db.session.add(user)
db.session.commit()
flash('Registration successful! Please log in.', 'success')
return redirect(url_for('login'))
except Exception as e:
db.session.rollback()
flash('Registration failed. Please try again.', 'error')
app.logger.error(f"Registration error: {e}")
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
"""User login"""
if request.method == 'POST':
username = request.form.get('username', '').strip()
password = request.form.get('password', '')
if not username or not password:
flash('Username and password are required', 'error')
return render_template('login.html')
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password_hash, password):
login_user(user)
next_page = request.args.get('next')
return redirect(next_page) if next_page else redirect(url_for('dashboard'))
else:
flash('Invalid username or password', 'error')
return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
"""User logout"""
logout_user()
flash('You have been logged out', 'info')
return redirect(url_for('login'))
@app.route('/dashboard')
@login_required
def dashboard():
"""Main dashboard - display user's tasks"""
status_filter = request.args.get('status', 'all')
# Base query for current user's tasks
query = Task.query.filter_by(user_id=current_user.id)
# Apply status filter
if status_filter != 'all':
query = query.filter_by(status=status_filter)
# Order by due date and creation date
tasks = query.order_by(Task.due_date.asc(), Task.created_at.desc()).all()
# Get task counts for filter buttons
task_counts = {
'all': Task.query.filter_by(user_id=current_user.id).count(),
'pending': Task.query.filter_by(user_id=current_user.id, status='pending').count(),
'completed': Task.query.filter_by(user_id=current_user.id, status='completed').count()
}
return render_template('dashboard.html',
tasks=tasks,
status_filter=status_filter,
task_counts=task_counts)
@app.route('/task/new', methods=['GET', 'POST'])
@login_required
def new_task():
"""Create a new task"""
if request.method == 'POST':
title = request.form.get('title', '').strip()
description = request.form.get('description', '').strip()
due_date_str = request.form.get('due_date', '')
# Validation
if not title:
flash('Task title is required', 'error')
return render_template('task_form.html', task=None)
# Parse due date
due_date = None
if due_date_str:
try:
due_date = datetime.strptime(due_date_str, '%Y-%m-%d').date()
except ValueError:
flash('Invalid due date format', 'error')
return render_template('task_form.html', task=None)
# Create task
task = Task(
title=title,
description=description,
due_date=due_date,
user_id=current_user.id
)
try:
db.session.add(task)
db.session.commit()
flash('Task created successfully!', 'success')
return redirect(url_for('dashboard'))
except Exception as e:
db.session.rollback()
flash('Failed to create task. Please try again.', 'error')
app.logger.error(f"Task creation error: {e}")
return render_template('task_form.html', task=None)
@app.route('/task/<int:task_id>/edit', methods=['GET', 'POST'])
@login_required
def edit_task(task_id):
"""Edit an existing task"""
task = Task.query.filter_by(id=task_id, user_id=current_user.id).first_or_404()
if request.method == 'POST':
title = request.form.get('title', '').strip()
description = request.form.get('description', '').strip()
due_date_str = request.form.get('due_date', '')
status = request.form.get('status', 'pending')
# Validation
if not title:
flash('Task title is required', 'error')
return render_template('task_form.html', task=task)
# Parse due date
due_date = None
if due_date_str:
try:
due_date = datetime.strptime(due_date_str, '%Y-%m-%d').date()
except ValueError:
flash('Invalid due date format', 'error')
return render_template('task_form.html', task=task)
# Update task
task.title = title
task.description = description
task.due_date = due_date
task.status = status
task.updated_at = datetime.utcnow()
try:
db.session.commit()
flash('Task updated successfully!', 'success')
return redirect(url_for('dashboard'))
except Exception as e:
db.session.rollback()
flash('Failed to update task. Please try again.', 'error')
app.logger.error(f"Task update error: {e}")
return render_template('task_form.html', task=task)
@app.route('/task/<int:task_id>/delete', methods=['POST'])
@login_required
def delete_task(task_id):
"""Delete a task"""
task = Task.query.filter_by(id=task_id, user_id=current_user.id).first_or_404()
try:
db.session.delete(task)
db.session.commit()
flash('Task deleted successfully!', 'success')
except Exception as e:
db.session.rollback()
flash('Failed to delete task. Please try again.', 'error')
app.logger.error(f"Task deletion error: {e}")
return redirect(url_for('dashboard'))
@app.route('/api/task/<int:task_id>/toggle', methods=['POST'])
@login_required
def toggle_task_status(task_id):
"""AJAX endpoint to toggle task status"""
task = Task.query.filter_by(id=task_id, user_id=current_user.id).first_or_404()
# Toggle status
task.status = 'completed' if task.status == 'pending' else 'pending'
task.updated_at = datetime.utcnow()
try:
db.session.commit()
return jsonify({
'success': True,
'status': task.status,
'message': f'Task marked as {task.status}'
})
except Exception as e:
db.session.rollback()
app.logger.error(f"Task toggle error: {e}")
return jsonify({
'success': False,
'message': 'Failed to update task status'
}), 500
# Error handlers
@app.errorhandler(404)
def not_found(error):
"""Handle 404 errors"""
return render_template('error.html',
error_code=404,
error_message="Page not found"), 404
@app.errorhandler(500)
def internal_error(error):
"""Handle 500 errors"""
db.session.rollback()
return render_template('error.html',
error_code=500,
error_message="Internal server error"), 500
if __name__ == '__main__':
# Initialize database and demo data on startup
with app.app_context():
print("📁 Creating database tables...")
db.create_all()
print("🎉 Database initialization completed!")
# Run the app on port 8000 to match docker-compose
app.run(host='0.0.0.0', port=8000, debug=True)