-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1140 lines (934 loc) · 41.3 KB
/
server.py
File metadata and controls
1140 lines (934 loc) · 41.3 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, jsonify, request, render_template, g, redirect, send_from_directory
from flask_cors import CORS
import sqlite3
import os
import json
import uuid
from datetime import datetime
DATABASE = os.getenv('DATABASE_PATH', os.path.join('data', 'fretlog.db'))
app = Flask(__name__)
CORS(app)
# Read version from file
VERSION_FILE = os.path.join(os.path.dirname(__file__), 'VERSION')
try:
with open(VERSION_FILE, 'r') as f:
APP_VERSION = f.read().strip()
except Exception as e:
print(f"Warning: Could not read VERSION file: {e}")
APP_VERSION = '0.0.0'
def get_db():
db = getattr(g, '_database', None)
if db is None:
# ensure directory exists
os.makedirs(os.path.dirname(DATABASE), exist_ok=True)
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def dict_from_row(row):
return dict(row) if row else None
def generate_id():
return str(uuid.uuid4())
def init_db():
# ensure directory exists
os.makedirs(os.path.dirname(DATABASE), exist_ok=True)
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
# Tables
cursor.execute('DROP TABLE IF EXISTS users')
cursor.execute('''CREATE TABLE IF NOT EXISTS categories (
id TEXT PRIMARY KEY, name TEXT, type TEXT, icon TEXT, color TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS instruments (
id TEXT PRIMARY KEY, name TEXT, icon TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS artists (
id TEXT PRIMARY KEY, name TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS library_items (
id TEXT PRIMARY KEY, name TEXT, category_id TEXT, artist_id TEXT,
star_rating INTEGER, notes TEXT, created_at TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY, instrument_id TEXT, status TEXT, date TEXT,
start_time TEXT, end_time TEXT, total_time INTEGER, notes TEXT, created_at TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS session_items (
id TEXT PRIMARY KEY, session_id TEXT, library_item_id TEXT, name TEXT,
category_id TEXT, time_spent INTEGER, started_at TEXT)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY, value TEXT)''')
# Default data if empty
cursor.execute('SELECT count(*) FROM instruments')
if cursor.fetchone()[0] == 0:
init_default_data(conn)
conn.commit()
conn.close()
def init_default_data(conn):
cursor = conn.cursor()
# Default Categories
cats = [
('cat-ear-training', 'Ear Training', 'Ear Training', '👂', '#f59e0b'),
('cat-lesson', 'Lesson', 'Lesson', '🎓', '#3b82f6'),
('cat-song', 'Song', 'Song', '🎵', '#4f46e5'),
('cat-technique', 'Technique', 'Technique', '💪', '#10b981'),
('cat-theory', 'Theory', 'Theory', '📚', '#8b5cf6')
]
for cat_id, name, type_val, icon, color in cats:
cursor.execute('INSERT INTO categories (id, name, type, icon, color) VALUES (?, ?, ?, ?, ?)',
(cat_id, name, type_val, icon, color))
# Default Instruments
insts = [
('inst-bass', 'Bass', '🎸'),
('inst-drums', 'Drums', '🥁'),
('inst-guitar', 'Guitar', '🎸'),
('inst-piano', 'Piano', '🎹')
]
for inst_id, name, icon in insts:
cursor.execute('INSERT INTO instruments (id, name, icon) VALUES (?, ?, ?)',
(inst_id, name, icon))
# Set default instrument
cursor.execute("INSERT OR REPLACE INTO settings (key, value) VALUES ('default_instrument_id', ?)", ('inst-guitar',))
conn.commit()
@app.context_processor
def inject_user():
try:
conn = get_db()
cursor = conn.cursor()
# Get Current Instrument ID from settings
cursor.execute("SELECT value FROM settings WHERE key='default_instrument_id'")
row = cursor.fetchone()
instrument_id = row[0] if row else 'inst-guitar'
# Get Current Instrument
instrument = None
cursor.execute('SELECT * FROM instruments WHERE id=?', (instrument_id,))
inst_row = cursor.fetchone()
if inst_row:
instrument = dict(inst_row)
# current_user is now a dummy object for template compatibility
user = {'id': 'local-user', 'name': 'Musician'}
return dict(current_user=user, current_instrument=instrument, APP_VERSION=APP_VERSION)
except Exception as e:
print(f"Error injecting user context: {e}")
return dict(current_user=None, current_instrument=None, APP_VERSION=APP_VERSION)
# ==========================================
# Page Routes
# ==========================================
@app.route('/')
def index():
return render_template('index.html', active_page='dashboard')
@app.route('/sessions')
def sessions():
return render_template('sessions.html', active_page='sessions')
@app.route('/library')
def library():
return render_template('library.html', active_page='library')
@app.route('/statistics')
def statistics():
return render_template('statistics.html', active_page='statistics')
@app.route('/settings')
def settings():
return render_template('settings.html', active_page='settings')
@app.route('/static/<path:filename>')
def serve_static_assets(filename):
return send_from_directory('static', filename)
@app.route('/version.js')
def serve_version():
"""Serve the application version as a global JS constant"""
return f"const APP_VERSION = '{APP_VERSION}';", 200, {'Content-Type': 'application/javascript'}
@app.route('/<path:filename>')
def serve_legacy_html(filename):
# Support legacy .html links if any, redirect to clean routes
if filename.endswith('.html'):
base = filename[:-5]
if base == 'index': return redirect('/')
return redirect('/' + base)
return send_from_directory('.', filename)
# ==========================================
# Init API - Single endpoint for all startup data
# ==========================================
@app.route('/api/init', methods=['GET'])
def get_init_data():
"""Return all initialization data in a single request for faster page loads"""
conn = get_db()
cursor = conn.cursor()
# User (Dummy for compatibility)
user = {'id': 'local-user', 'name': 'Musician'}
cursor.execute("SELECT value FROM settings WHERE key='default_instrument_id'")
inst_row = cursor.fetchone()
user['default_instrument_id'] = inst_row[0] if inst_row else 'inst-guitar'
# Categories
cursor.execute('SELECT * FROM categories')
categories = [dict_from_row(row) for row in cursor.fetchall()]
# Instruments
cursor.execute('SELECT * FROM instruments')
instruments = [dict_from_row(row) for row in cursor.fetchall()]
# Artists
cursor.execute('SELECT * FROM artists')
artists = [dict_from_row(row) for row in cursor.fetchall()]
# Library items
cursor.execute('SELECT * FROM library_items ORDER BY created_at DESC')
library = [dict_from_row(row) for row in cursor.fetchall()]
# Sessions (completed only)
cursor.execute('SELECT * FROM sessions WHERE status="completed" ORDER BY date DESC')
sessions = []
for row in cursor.fetchall():
session = dict_from_row(row)
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (session['id'],))
session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
sessions.append(session)
# Current session (running)
cursor.execute('SELECT * FROM sessions WHERE status="running" ORDER BY created_at DESC LIMIT 1')
current_session_row = cursor.fetchone()
current_session = None
if current_session_row:
current_session = dict_from_row(current_session_row)
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (current_session['id'],))
current_session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
# Theme
cursor.execute("SELECT value FROM settings WHERE key='theme'")
theme_row = cursor.fetchone()
theme = theme_row[0] if theme_row else 'dark'
return jsonify({
'user': user,
'categories': categories,
'instruments': instruments,
'artists': artists,
'library': library,
'sessions': sessions,
'currentSession': current_session,
'theme': theme
})
# ==========================================
# User API
# ==========================================
@app.route('/api/user', methods=['GET'])
def get_user():
user = {'id': 'local-user', 'name': 'Musician'}
return jsonify(user)
@app.route('/api/user', methods=['POST', 'PUT'])
def update_user():
# Users table is gone, just return dummy
return jsonify({'id': 'local-user', 'name': 'Musician'})
# ==========================================
# Categories API
# ==========================================
@app.route('/api/categories', methods=['GET'])
def get_categories():
conn = get_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM categories')
categories = [dict_from_row(row) for row in cursor.fetchall()]
conn.close()
return jsonify(categories)
@app.route('/api/categories', methods=['POST'])
def add_category():
data = request.json
conn = get_db()
cursor = conn.cursor()
cat_id = data.get('id') or generate_id()
# Check if exists (for restore/import scenarios)
cursor.execute('SELECT * FROM categories WHERE id=?', (cat_id,))
if cursor.fetchone():
# Update if exists? Or skip? For now let's update or just return existing
pass
# We'll just try insert and let it fail or use INSERT OR REPLACE if we wanted
# But simpler:
# Use INSERT OR REPLACE to handle re-seeding same IDs
cursor.execute('''
INSERT OR REPLACE INTO categories (id, name, type, icon, color) VALUES (?, ?, ?, ?, ?)
''', (cat_id, data.get('name'), data.get('type'), data.get('icon', '🎵'), data.get('color')))
conn.commit()
cursor.execute('SELECT * FROM categories WHERE id=?', (cat_id,))
category = dict_from_row(cursor.fetchone())
conn.close()
return jsonify(category), 201
@app.route('/api/categories/<cat_id>', methods=['PUT'])
def update_category(cat_id):
data = request.json
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
UPDATE categories SET name=?, type=?, icon=?, color=? WHERE id=?
''', (data.get('name'), data.get('type'), data.get('icon'), data.get('color'), cat_id))
conn.commit()
cursor.execute('SELECT * FROM categories WHERE id=?', (cat_id,))
category = dict_from_row(cursor.fetchone())
conn.close()
return jsonify(category)
@app.route('/api/categories/<cat_id>', methods=['DELETE'])
def delete_category(cat_id):
conn = get_db()
cursor = conn.cursor()
cursor.execute('DELETE FROM categories WHERE id=?', (cat_id,))
conn.commit()
conn.close()
return '', 204
# ==========================================
# Instruments API
# ==========================================
@app.route('/api/instruments', methods=['GET'])
def get_instruments():
conn = get_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM instruments')
instruments = [dict_from_row(row) for row in cursor.fetchall()]
conn.close()
return jsonify(instruments)
@app.route('/api/instruments', methods=['POST'])
def add_instrument():
data = request.json
conn = get_db()
cursor = conn.cursor()
inst_id = data.get('id') or generate_id()
cursor.execute('''
INSERT OR REPLACE INTO instruments (id, name, icon) VALUES (?, ?, ?)
''', (inst_id, data.get('name'), data.get('icon', '🎸')))
conn.commit()
cursor.execute('SELECT * FROM instruments WHERE id=?', (inst_id,))
instrument = dict_from_row(cursor.fetchone())
conn.close()
return jsonify(instrument), 201
@app.route('/api/instruments/<inst_id>', methods=['PUT'])
def update_instrument(inst_id):
data = request.json
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
UPDATE instruments SET name=?, icon=? WHERE id=?
''', (data.get('name'), data.get('icon'), inst_id))
conn.commit()
cursor.execute('SELECT * FROM instruments WHERE id=?', (inst_id,))
instrument = dict_from_row(cursor.fetchone())
conn.close()
return jsonify(instrument)
@app.route('/api/instruments/<inst_id>', methods=['DELETE'])
def delete_instrument(inst_id):
conn = get_db()
cursor = conn.cursor()
cursor.execute('DELETE FROM instruments WHERE id=?', (inst_id,))
conn.commit()
conn.close()
return '', 204
# ==========================================
# Artists API
# ==========================================
@app.route('/api/artists', methods=['GET'])
def get_artists():
conn = get_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM artists')
artists = [dict_from_row(row) for row in cursor.fetchall()]
conn.close()
return jsonify(artists)
@app.route('/api/artists', methods=['POST'])
def add_artist():
data = request.json
conn = get_db()
cursor = conn.cursor()
# Check if artist exists
cursor.execute('SELECT * FROM artists WHERE LOWER(name)=LOWER(?)', (data.get('name'),))
existing = cursor.fetchone()
if existing:
conn.close()
return jsonify(dict_from_row(existing))
artist_id = generate_id()
cursor.execute('INSERT INTO artists (id, name) VALUES (?, ?)',
(artist_id, data.get('name')))
conn.commit()
cursor.execute('SELECT * FROM artists WHERE id=?', (artist_id,))
artist = dict_from_row(cursor.fetchone())
conn.close()
return jsonify(artist), 201
@app.route('/api/artists/<artist_id>', methods=['DELETE'])
def delete_artist(artist_id):
conn = get_db()
cursor = conn.cursor()
cursor.execute('DELETE FROM artists WHERE id=?', (artist_id,))
conn.commit()
conn.close()
return '', 204
@app.route('/api/artists/<artist_id>', methods=['PUT', 'POST'])
def update_artist(artist_id):
data = request.json
conn = get_db()
cursor = conn.cursor()
cursor.execute('UPDATE artists SET name=? WHERE id=?', (data.get('name'), artist_id))
conn.commit()
cursor.execute('SELECT * FROM artists WHERE id=?', (artist_id,))
artist = dict_from_row(cursor.fetchone())
conn.close()
return jsonify(artist)
# ==========================================
# Library API
# ==========================================
@app.route('/api/library', methods=['GET'])
def get_library():
conn = get_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM library_items ORDER BY created_at DESC')
items = [dict_from_row(row) for row in cursor.fetchall()]
conn.close()
return jsonify(items)
@app.route('/api/library', methods=['POST'])
def add_library_item():
data = request.json
name = data.get('name')
category_id = data.get('categoryId')
artist_id = data.get('artistId')
conn = get_db()
cursor = conn.cursor()
# Check for duplicates (case-insensitive)
cursor.execute('''
SELECT id FROM library_items
WHERE LOWER(name) = LOWER(?) AND category_id = ? AND (artist_id = ? OR (artist_id IS NULL AND ? IS NULL))
''', (name, category_id, artist_id, artist_id))
if cursor.fetchone():
conn.close()
return jsonify({'error': 'An item with this name already exists in this category.'}), 409
item_id = generate_id()
cursor.execute('''
INSERT INTO library_items (id, name, category_id, artist_id, star_rating, notes, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (item_id, name, category_id, artist_id,
data.get('starRating', 0), data.get('notes', ''), datetime.now().isoformat()))
conn.commit()
cursor.execute('SELECT * FROM library_items WHERE id=?', (item_id,))
item = dict_from_row(cursor.fetchone())
conn.close()
return jsonify(item), 201
@app.route('/api/library/<item_id>', methods=['PUT'])
def update_library_item(item_id):
data = request.json
conn = get_db()
cursor = conn.cursor()
# Check for duplicates if name/category/artist are being changed
if any(k in data for k in ['name', 'categoryId', 'artistId']):
cursor.execute('SELECT name, category_id, artist_id FROM library_items WHERE id=?', (item_id,))
current = cursor.fetchone()
if not current:
conn.close()
return jsonify({'error': 'Item not found'}), 404
new_name = data.get('name', current[0])
new_cat = data.get('categoryId', current[1])
new_art = data.get('artistId', current[2]) if 'artistId' in data else current[2]
cursor.execute('''
SELECT id FROM library_items
WHERE LOWER(name) = LOWER(?) AND category_id = ? AND (artist_id = ? OR (artist_id IS NULL AND ? IS NULL))
AND id != ?
''', (new_name, new_cat, new_art, new_art, item_id))
if cursor.fetchone():
conn.close()
return jsonify({'error': 'An item with this name already exists in this category.'}), 409
# Map frontend camelCase to backend snake_case
field_map = {
'name': 'name',
'categoryId': 'category_id',
'artistId': 'artist_id',
'starRating': 'star_rating',
'notes': 'notes'
}
update_fields = []
values = []
for key, col in field_map.items():
if key in data:
update_fields.append(f"{col}=?")
values.append(data[key])
if update_fields:
values.append(item_id)
query = f"UPDATE library_items SET {', '.join(update_fields)} WHERE id=?"
cursor.execute(query, tuple(values))
conn.commit()
cursor.execute('SELECT * FROM library_items WHERE id=?', (item_id,))
item = dict_from_row(cursor.fetchone())
conn.close()
return jsonify(item)
@app.route('/api/library/<item_id>', methods=['DELETE'])
def delete_library_item(item_id):
conn = get_db()
cursor = conn.cursor()
cursor.execute('DELETE FROM library_items WHERE id=?', (item_id,))
conn.commit()
conn.close()
return '', 204
# ==========================================
# Sessions API
# ==========================================
@app.route('/api/sessions', methods=['GET'])
def get_sessions():
conn = get_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM sessions WHERE status="completed" ORDER BY date DESC')
sessions = []
for row in cursor.fetchall():
session = dict_from_row(row)
# Get session items
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (session['id'],))
session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
sessions.append(session)
conn.close()
return jsonify(sessions)
@app.route('/api/sessions', methods=['POST'])
def add_session():
data = request.json
conn = get_db()
cursor = conn.cursor()
session_id = data.get('id') or generate_id()
# Check if session already exists
cursor.execute('SELECT id FROM sessions WHERE id=?', (session_id,))
exists = cursor.fetchone()
if exists:
print(f"DEBUG: Session {session_id} exists. Updating...")
# Update existing session
cursor.execute('''
UPDATE sessions SET instrument_id=?, status=?, date=?, start_time=?, end_time=?,
total_time=?, notes=? WHERE id=?
''', (data.get('instrumentId'), data.get('status', 'completed'),
data.get('date', datetime.now().isoformat()), data.get('startTime'),
data.get('endTime'), data.get('totalTime', 0), data.get('notes', ''), session_id))
# Delete old items and re-insert
print(f"DEBUG: Deleting items for session {session_id}")
cursor.execute('DELETE FROM session_items WHERE session_id=?', (session_id,))
print(f"DEBUG: Deleted {cursor.rowcount} items")
else:
print(f"DEBUG: Creating new session {session_id}")
# Insert new session
cursor.execute('''
INSERT INTO sessions (id, instrument_id, status, date, start_time, end_time, total_time, notes, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (session_id, data.get('instrumentId'), data.get('status', 'completed'),
data.get('date', datetime.now().isoformat()), data.get('startTime'),
data.get('endTime'), data.get('totalTime', 0), data.get('notes', ''),
datetime.now().isoformat()))
# Add session items
items = data.get('items', [])
for item in items:
item_id = item.get('id') or generate_id()
# Handle both camelCase and snake_case keys
lib_id = item.get('libraryItemId') or item.get('library_item_id')
cat_id = item.get('categoryId') or item.get('category_id')
started_at = item.get('startedAt') or item.get('started_at')
time_spent = item.get('timeSpent', 0) if item.get('timeSpent') is not None else item.get('time_spent', 0)
# DEBUG: Check for collision
cursor.execute("SELECT session_id FROM session_items WHERE id=?", (item_id,))
existing_collision = cursor.fetchone()
if existing_collision:
print(f"DEBUG: Item {item_id} ALREADY EXISTS in session {existing_collision[0]}")
# If it belongs to a different session, this is a logic error in our app
# But the UNIQUE constraint will fail regardless.
# We must DELETE it by ID to proceed safely, implying session_id mismatch
cursor.execute("DELETE FROM session_items WHERE id=?", (item_id,))
print(f"DEBUG: Force deleted item {item_id} to resolve collision")
cursor.execute('''
INSERT INTO session_items (id, session_id, library_item_id, name, category_id, time_spent, started_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (item_id, session_id, lib_id, item.get('name'),
cat_id, time_spent, started_at))
conn.commit()
cursor.execute('SELECT * FROM sessions WHERE id=?', (session_id,))
session = dict_from_row(cursor.fetchone())
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (session_id,))
session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
conn.close()
return jsonify(session), 201
@app.route('/api/sessions/<session_id>', methods=['PUT'])
def update_session(session_id):
data = request.json
conn = get_db()
cursor = conn.cursor()
# Update session record
cursor.execute('''
UPDATE sessions SET instrument_id=?, status=?, date=?, total_time=?, notes=?, end_time=?
WHERE id=?
''', (data.get('instrumentId'), data.get('status'), data.get('date'),
data.get('totalTime'), data.get('notes'), data.get('endTime'), session_id))
# Delete old items and re-insert new ones
cursor.execute('DELETE FROM session_items WHERE session_id=?', (session_id,))
# Add session items
items = data.get('items', [])
for item in items:
item_id = item.get('id') or generate_id()
# Handle both camelCase and snake_case keys
lib_id = item.get('libraryItemId') or item.get('library_item_id')
cat_id = item.get('categoryId') or item.get('category_id')
started_at = item.get('startedAt') or item.get('started_at')
time_spent = item.get('timeSpent', 0) if item.get('timeSpent') is not None else item.get('time_spent', 0)
cursor.execute('''
INSERT INTO session_items (id, session_id, library_item_id, name, category_id, time_spent, started_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (item_id, session_id, lib_id, item.get('name'),
cat_id, time_spent, started_at))
conn.commit()
cursor.execute('SELECT * FROM sessions WHERE id=?', (session_id,))
session = dict_from_row(cursor.fetchone())
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (session_id,))
session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
conn.close()
return jsonify(session)
@app.route('/api/sessions/<session_id>', methods=['DELETE'])
def delete_session(session_id):
conn = get_db()
cursor = conn.cursor()
cursor.execute('DELETE FROM session_items WHERE session_id=?', (session_id,))
cursor.execute('DELETE FROM sessions WHERE id=?', (session_id,))
conn.commit()
conn.close()
return '', 204
# ==========================================
# Current Session API
# ==========================================
@app.route('/api/sessions/current', methods=['GET'])
def get_current_session():
conn = get_db()
cursor = conn.cursor()
cursor.execute("SELECT value FROM settings WHERE key='current_session'")
result = cursor.fetchone()
if not result or not result[0]:
conn.close()
return jsonify(None)
session_id = result[0]
cursor.execute('SELECT * FROM sessions WHERE id=?', (session_id,))
session = cursor.fetchone()
if not session:
conn.close()
return jsonify(None)
session = dict_from_row(session)
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (session_id,))
session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
conn.close()
return jsonify(session)
@app.route('/api/sessions/current', methods=['POST'])
def save_current_session():
data = request.json
conn = get_db()
cursor = conn.cursor()
session_id = data.get('id') or generate_id()
# Check if session already exists
cursor.execute('SELECT id FROM sessions WHERE id=?', (session_id,))
exists = cursor.fetchone()
if exists:
# Update existing session
cursor.execute('''
UPDATE sessions SET instrument_id=?, status=?, date=?, start_time=?,
total_time=?, notes=? WHERE id=?
''', (data.get('instrumentId'), data.get('status', 'running'),
data.get('date'), data.get('startTime'), data.get('totalTime', 0),
data.get('notes', ''), session_id))
# Delete old items and re-insert
cursor.execute('DELETE FROM session_items WHERE session_id=?', (session_id,))
else:
# Create new session
cursor.execute('''
INSERT INTO sessions (id, instrument_id, status, date, start_time, total_time, notes, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (session_id, data.get('instrumentId'), data.get('status', 'running'),
data.get('date', datetime.now().isoformat()), data.get('startTime'),
data.get('totalTime', 0), data.get('notes', ''), datetime.now().isoformat()))
# Insert session items
items = data.get('items', [])
for item in items:
item_id = item.get('id') or generate_id()
# Handle both camelCase and snake_case keys
lib_id = item.get('libraryItemId') or item.get('library_item_id')
cat_id = item.get('categoryId') or item.get('category_id')
started_at = item.get('startedAt') or item.get('started_at')
time_spent = item.get('timeSpent', 0) if item.get('timeSpent') is not None else item.get('time_spent', 0)
cursor.execute('''
INSERT INTO session_items (id, session_id, library_item_id, name, category_id, time_spent, started_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (item_id, session_id, lib_id, item.get('name'),
cat_id, time_spent, started_at))
# Store current session reference
cursor.execute('''
INSERT OR REPLACE INTO settings (key, value) VALUES ('current_session', ?)
''', (session_id,))
conn.commit()
cursor.execute('SELECT * FROM sessions WHERE id=?', (session_id,))
session = dict_from_row(cursor.fetchone())
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (session_id,))
session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
conn.close()
return jsonify(session)
@app.route('/api/sessions/current', methods=['DELETE'])
def clear_current_session():
conn = get_db()
cursor = conn.cursor()
# Get current session ID
cursor.execute("SELECT value FROM settings WHERE key='current_session'")
result = cursor.fetchone()
if result and result[0]:
session_id = result[0]
# Check status first - only delete if it's still running (cancelling)
# If it's already 'completed', it means we successfully saved it, so DON'T delete the data
cursor.execute("SELECT status FROM sessions WHERE id=?", (session_id,))
row = cursor.fetchone()
if row and row[0] == 'running':
cursor.execute('DELETE FROM session_items WHERE session_id=?', (session_id,))
cursor.execute("DELETE FROM sessions WHERE id=?", (session_id,))
# Clear current session reference
cursor.execute("DELETE FROM settings WHERE key='current_session'")
conn.commit()
conn.close()
return '', 204
@app.route('/api/sessions/current/items', methods=['POST'])
def add_item_to_current_session():
data = request.json
conn = get_db()
cursor = conn.cursor()
# Get current session
cursor.execute("SELECT value FROM settings WHERE key='current_session'")
result = cursor.fetchone()
if not result or not result[0]:
conn.close()
return jsonify({'error': 'No current session'}), 400
session_id = result[0]
# Get library item info
cursor.execute('SELECT * FROM library_items WHERE id=?', (data.get('libraryItemId'),))
library_item = cursor.fetchone()
if not library_item:
conn.close()
return jsonify({'error': 'Library item not found'}), 404
library_item = dict_from_row(library_item)
item_id = generate_id()
cursor.execute('''
INSERT INTO session_items (id, session_id, library_item_id, name, category_id, time_spent, started_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (item_id, session_id, library_item['id'], library_item['name'],
library_item['category_id'], data.get('timeSpent', 0),
data.get('startedAt') or int(datetime.now().timestamp() * 1000)))
conn.commit()
# Return updated session
cursor.execute('SELECT * FROM sessions WHERE id=?', (session_id,))
session = dict_from_row(cursor.fetchone())
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (session_id,))
session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
conn.close()
return jsonify(session)
@app.route('/api/sessions/current/items/<item_id>', methods=['PUT'])
def update_session_item_time(item_id):
data = request.json
conn = get_db()
cursor = conn.cursor()
cursor.execute('UPDATE session_items SET time_spent=? WHERE id=?',
(data.get('timeSpent', 0), item_id))
conn.commit()
# Return updated current session
cursor.execute("SELECT value FROM settings WHERE key='current_session'")
result = cursor.fetchone()
if result and result[0]:
session_id = result[0]
cursor.execute('SELECT * FROM sessions WHERE id=?', (session_id,))
session = dict_from_row(cursor.fetchone())
cursor.execute('SELECT * FROM session_items WHERE session_id=?', (session_id,))
session['items'] = [dict_from_row(item) for item in cursor.fetchall()]
conn.close()
return jsonify(session)
conn.close()
return jsonify(None)
# ==========================================
# Theme API
# ==========================================
@app.route('/api/theme', methods=['GET'])
def get_theme():
conn = get_db()
cursor = conn.cursor()
cursor.execute("SELECT value FROM settings WHERE key='theme'")
result = cursor.fetchone()
conn.close()
return jsonify({'theme': result[0] if result else 'light'})
@app.route('/api/theme', methods=['POST'])
def set_theme():
data = request.json
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO settings (key, value) VALUES ('theme', ?)
''', (data.get('theme', 'light'),))
conn.commit()
conn.close()
return jsonify({'theme': data.get('theme')})
@app.route('/api/settings', methods=['POST'])
def update_setting():
data = request.json
key = data.get('key')
value = data.get('value')
if not key:
return jsonify({'error': 'Missing key'}), 400
conn = get_db()
cursor = conn.cursor()
cursor.execute('''
INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)
''', (key, value))
conn.commit()
conn.close()
return jsonify({'status': 'success', 'key': key, 'value': value})
# ==========================================
# Statistics API
# ==========================================
@app.route('/api/statistics/summary', methods=['GET'])
def get_statistics_summary():
conn = get_db()
cursor = conn.cursor()
now = datetime.now()
today_start = now.replace(hour=0, minute=0, second=0, microsecond=0).isoformat()
# Calculate week start (Sunday)
days_since_sunday = now.weekday() + 1 if now.weekday() != 6 else 0
week_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
from datetime import timedelta
week_start = (week_start - timedelta(days=days_since_sunday)).isoformat()
month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0).isoformat()
year_start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0).isoformat()
def get_time_in_range(start):
cursor.execute('''
SELECT COALESCE(SUM(total_time), 0) FROM sessions
WHERE status='completed' AND date >= ?
''', (start,))
return cursor.fetchone()[0]
cursor.execute('SELECT COALESCE(SUM(total_time), 0) FROM sessions WHERE status="completed"')
all_time = cursor.fetchone()[0]
summary = {
'today': get_time_in_range(today_start),
'week': get_time_in_range(week_start),
'month': get_time_in_range(month_start),
'year': get_time_in_range(year_start),
'allTime': all_time
}
conn.close()
return jsonify(summary)
# = = = = = = = = = = = = = = = = = = = = = =
# Data Management API
# = = = = = = = = = = = = = = = = = = = = = =
@app.route('/api/export', methods=['GET'])
def export_data():
"""Export all database tables to JSON"""
conn = get_db()
cursor = conn.cursor()
tables = ['categories', 'instruments', 'artists', 'library_items', 'sessions', 'session_items', 'settings']
export = {}
for table in tables:
cursor.execute(f'SELECT * FROM {table}')
export[table] = [dict_from_row(row) for row in cursor.fetchall()]
conn.close()
return jsonify(export)
@app.route('/api/import', methods=['POST'])
def import_data():
"""Import data from JSON with duplicate prevention"""
data = request.json
if not data:
return jsonify({'error': 'No data provided'}), 400
conn = get_db()
cursor = conn.cursor()