-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
386 lines (309 loc) · 11.1 KB
/
app.py
File metadata and controls
386 lines (309 loc) · 11.1 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import warnings
warnings.filterwarnings("ignore")
import logging
logging.getLogger('werkzeug').setLevel(logging.ERROR)
from flask import Flask, render_template, request, jsonify
import sqlite3
import json
from datetime import datetime
import os
import threading
import webview
app = Flask(__name__)
# Database initialization
def init_db():
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
# Create folders table
cursor.execute('''
CREATE TABLE IF NOT EXISTS folders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
color TEXT DEFAULT '#667eea',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Create todos table with folder_id
cursor.execute('''
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT,
completed BOOLEAN DEFAULT FALSE,
priority TEXT DEFAULT 'medium',
category TEXT DEFAULT 'general',
folder_id INTEGER DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (folder_id) REFERENCES folders (id) ON DELETE SET NULL
)
''')
# Insert default folder if none exists
cursor.execute('SELECT COUNT(*) FROM folders')
if cursor.fetchone()[0] == 0:
cursor.execute('INSERT INTO folders (name, color) VALUES (?, ?)', ('General', '#667eea'))
conn.commit()
conn.close()
# Initialize database on startup
init_db()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/favicon.ico')
def favicon():
return '', 204
# API Routes for Folders
@app.route('/api/folders', methods=['GET'])
def get_folders():
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
cursor.execute('''
SELECT f.id, f.name, f.color, f.created_at, COUNT(t.id) as todo_count
FROM folders f
LEFT JOIN todos t ON f.id = t.folder_id
GROUP BY f.id
ORDER BY f.created_at ASC
''')
folders = []
for row in cursor.fetchall():
folder_id = row[0]
# Obtener cantidad de tareas completadas para este folder
cursor2 = conn.cursor()
cursor2.execute('SELECT COUNT(*) FROM todos WHERE folder_id = ? AND completed = 1', (folder_id,))
completed_count = cursor2.fetchone()[0]
folders.append({
'id': row[0],
'name': row[1],
'color': row[2],
'created_at': row[3],
'todo_count': row[4],
'completed_count': completed_count
})
conn.close()
return jsonify(folders)
@app.route('/api/folders', methods=['POST'])
def create_folder():
data = request.get_json()
if not data or 'name' not in data:
return jsonify({'error': 'Folder name is required'}), 400
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
cursor.execute('''
INSERT INTO folders (name, color)
VALUES (?, ?)
''', (
data['name'],
data.get('color', '#667eea')
))
folder_id = cursor.lastrowid
conn.commit()
conn.close()
return jsonify({'id': folder_id, 'message': 'Folder created successfully'}), 201
@app.route('/api/folders/<int:folder_id>', methods=['DELETE'])
def delete_folder(folder_id):
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
# Check if folder exists
cursor.execute('SELECT * FROM folders WHERE id = ?', (folder_id,))
if not cursor.fetchone():
conn.close()
return jsonify({'error': 'Folder not found'}), 404
# Delete all todos in this folder first
cursor.execute('DELETE FROM todos WHERE folder_id = ?', (folder_id,))
# Delete the folder
cursor.execute('DELETE FROM folders WHERE id = ?', (folder_id,))
conn.commit()
conn.close()
return jsonify({'message': 'Folder and all its tasks deleted successfully'})
# API Routes for Todos
@app.route('/api/todos', methods=['GET'])
def get_todos():
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
# Get query parameters
filter_status = request.args.get('status', 'all')
search_query = request.args.get('search', '')
category_filter = request.args.get('category', 'all')
folder_filter = request.args.get('folder', 'all')
# Build query with JOIN to get folder information
query = """
SELECT t.id, t.title, t.description, t.completed, t.priority,
t.category, t.folder_id, t.created_at, t.updated_at,
f.name as folder_name, f.color as folder_color
FROM todos t
LEFT JOIN folders f ON t.folder_id = f.id
WHERE 1=1
"""
params = []
if filter_status == 'completed':
query += " AND t.completed = 1"
elif filter_status == 'pending':
query += " AND t.completed = 0"
if search_query:
query += " AND (t.title LIKE ? OR t.description LIKE ?)"
params.extend([f'%{search_query}%', f'%{search_query}%'])
if category_filter != 'all':
query += " AND t.category = ?"
params.append(category_filter)
if folder_filter != 'all':
query += " AND t.folder_id = ?"
params.append(folder_filter)
query += " ORDER BY t.created_at DESC"
cursor.execute(query, params)
todos = cursor.fetchall()
# Convert to list of dictionaries
todos_list = []
for todo in todos:
todos_list.append({
'id': todo[0],
'title': todo[1],
'description': todo[2],
'completed': bool(todo[3]),
'priority': todo[4],
'category': todo[5],
'folder_id': todo[6],
'created_at': todo[7],
'updated_at': todo[8],
'folder_name': todo[9] or 'General',
'folder_color': todo[10] or '#667eea'
})
conn.close()
return jsonify(todos_list)
@app.route('/api/todos', methods=['POST'])
def create_todo():
data = request.get_json()
if not data or 'title' not in data:
return jsonify({'error': 'Title is required'}), 400
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
cursor.execute('''
INSERT INTO todos (title, description, priority, category, folder_id)
VALUES (?, ?, ?, ?, ?)
''', (
data['title'],
data.get('description', ''),
data.get('priority', 'medium'),
data.get('category', 'general'),
data.get('folder_id', 1) # Default to General folder
))
todo_id = cursor.lastrowid
conn.commit()
conn.close()
return jsonify({'id': todo_id, 'message': 'Todo created successfully'}), 201
@app.route('/api/todos/<int:todo_id>', methods=['PUT'])
def update_todo(todo_id):
data = request.get_json()
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
# Check if todo exists
cursor.execute('SELECT * FROM todos WHERE id = ?', (todo_id,))
if not cursor.fetchone():
conn.close()
return jsonify({'error': 'Todo not found'}), 404
# Update todo
update_fields = []
params = []
if 'title' in data:
update_fields.append('title = ?')
params.append(data['title'])
if 'description' in data:
update_fields.append('description = ?')
params.append(data['description'])
if 'completed' in data:
update_fields.append('completed = ?')
params.append(data['completed'])
if 'priority' in data:
update_fields.append('priority = ?')
params.append(data['priority'])
if 'category' in data:
update_fields.append('category = ?')
params.append(data['category'])
if 'folder_id' in data:
update_fields.append('folder_id = ?')
params.append(data['folder_id'])
update_fields.append('updated_at = CURRENT_TIMESTAMP')
params.append(todo_id)
query = f"UPDATE todos SET {', '.join(update_fields)} WHERE id = ?"
cursor.execute(query, params)
conn.commit()
conn.close()
return jsonify({'message': 'Todo updated successfully'})
@app.route('/api/todos/<int:todo_id>', methods=['DELETE'])
def delete_todo(todo_id):
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
# Check if todo exists
cursor.execute('SELECT * FROM todos WHERE id = ?', (todo_id,))
if not cursor.fetchone():
conn.close()
return jsonify({'error': 'Todo not found'}), 404
cursor.execute('DELETE FROM todos WHERE id = ?', (todo_id,))
conn.commit()
conn.close()
return jsonify({'message': 'Todo deleted successfully'})
@app.route('/api/todos/<int:todo_id>/toggle', methods=['PUT'])
def toggle_todo(todo_id):
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
# Check if todo exists
cursor.execute('SELECT completed FROM todos WHERE id = ?', (todo_id,))
result = cursor.fetchone()
if not result:
conn.close()
return jsonify({'error': 'Todo not found'}), 404
# Toggle completed status
new_status = not result[0]
cursor.execute('''
UPDATE todos
SET completed = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
''', (new_status, todo_id))
conn.commit()
conn.close()
return jsonify({'completed': new_status, 'message': 'Todo toggled successfully'})
@app.route('/api/stats')
def get_stats():
conn = sqlite3.connect('todos.db')
cursor = conn.cursor()
# Get total todos
cursor.execute('SELECT COUNT(*) FROM todos')
total = cursor.fetchone()[0]
# Get completed todos
cursor.execute('SELECT COUNT(*) FROM todos WHERE completed = 1')
completed = cursor.fetchone()[0]
# Get pending todos
cursor.execute('SELECT COUNT(*) FROM todos WHERE completed = 0')
pending = cursor.fetchone()[0]
# Get todos by priority
cursor.execute('SELECT priority, COUNT(*) FROM todos GROUP BY priority')
priority_stats = dict(cursor.fetchall())
# Get todos by category
cursor.execute('SELECT category, COUNT(*) FROM todos GROUP BY category')
category_stats = dict(cursor.fetchall())
# Get todos by folder
cursor.execute('''
SELECT f.name, COUNT(t.id)
FROM folders f
LEFT JOIN todos t ON f.id = t.folder_id
GROUP BY f.id
''')
folder_stats = dict(cursor.fetchall())
conn.close()
return jsonify({
'total': total,
'completed': completed,
'pending': pending,
'priority_stats': priority_stats,
'category_stats': category_stats,
'folder_stats': folder_stats
})
def start_flask():
app.run(debug=False, host='127.0.0.1', port=5000, use_reloader=False)
if __name__ == '__main__':
flask_thread = threading.Thread(target=start_flask)
flask_thread.daemon = True
flask_thread.start()
import time
time.sleep(1)
webview.create_window("TaskMaster", "http://127.0.0.1:5000", width=1100, height=800)
webview.start(gui='edgechromium')