-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathskala_admin_api.py
More file actions
756 lines (630 loc) · 26.4 KB
/
skala_admin_api.py
File metadata and controls
756 lines (630 loc) · 26.4 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
# Copyright (C) 2023 David Mossakowski
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import logging
from datetime import datetime
from functools import wraps
from flask import Blueprint, jsonify, session, request, send_file
from werkzeug.utils import secure_filename
import competitionsEngine
import skala_db
# Import the admin_required decorator and session recreation from skala_api
from skala_api import admin_required, recreate_session_from_jwt
# Get data directory from environment
DATA_DIRECTORY = os.getenv('DATA_DIRECTORY')
if DATA_DIRECTORY is None:
DATA_DIRECTORY = os.getcwd()
COMPETITIONS_DB = DATA_DIRECTORY + "/db/competitions.sqlite"
UPLOAD_FOLDER = os.path.join(DATA_DIRECTORY, 'uploads')
ALLOWED_DB_EXTENSIONS = {'sqlite', 'db', 'sqlite3'}
ALLOWED_IMAGE_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'svg', 'webp'}
# Create admin blueprint with /api1/admin prefix
skala_admin_api = Blueprint('skala_admin', __name__, url_prefix='/api1/admin')
@skala_admin_api.before_request
def before_admin_request():
"""Ensure session is recreated from JWT before each admin request."""
recreate_session_from_jwt()
def allowed_file(filename):
"""Check if file has allowed extension for database files."""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_DB_EXTENSIONS
def allowed_image(filename):
"""Check if file has allowed extension for image files."""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_IMAGE_EXTENSIONS
# ============= Database Management Endpoints =============
@skala_admin_api.route('/database/download', methods=['GET'])
@admin_required
def download_database():
"""Download the SQLite database file.
Returns the competitions.sqlite database as a downloadable file.
Requires admin permissions.
"""
try:
if not os.path.exists(COMPETITIONS_DB):
return jsonify({
'success': False,
'error': 'database_not_found',
'message': 'Database file does not exist'
}), 404
# Get file stats for metadata
file_stats = os.stat(COMPETITIONS_DB)
file_size = file_stats.st_size
modified_time = datetime.fromtimestamp(file_stats.st_mtime).isoformat()
# Generate filename with timestamp
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
download_name = f'skala3ma_competitions_{timestamp}.sqlite'
logging.info(f"Admin {session.get('email')} downloading database: {download_name} ({file_size} bytes)")
return send_file(
COMPETITIONS_DB,
as_attachment=True,
download_name=download_name,
mimetype='application/x-sqlite3'
)
except Exception as e:
logging.error(f"Error downloading database: {e}")
return jsonify({
'success': False,
'error': 'download_failed',
'message': str(e)
}), 500
@skala_admin_api.route('/database/upload', methods=['POST'])
@admin_required
def upload_database():
"""Upload and replace the SQLite database file.
Accepts a multipart/form-data upload with 'file' field.
Creates a backup of the existing database before replacing.
Requires admin permissions.
Form data:
- file: SQLite database file
- create_backup: (optional) 'true' to create backup (default: true)
"""
try:
# Check if file was uploaded
if 'file' not in request.files:
return jsonify({
'success': False,
'error': 'no_file',
'message': 'No file provided in request'
}), 400
file = request.files['file']
# Check if file was selected
if file.filename == '':
return jsonify({
'success': False,
'error': 'empty_filename',
'message': 'No file selected'
}), 400
# Validate file extension
if not allowed_file(file.filename):
return jsonify({
'success': False,
'error': 'invalid_file_type',
'message': f'File must have one of these extensions: {", ".join(ALLOWED_DB_EXTENSIONS)}'
}), 400
# Get backup preference (default to true)
create_backup = request.form.get('create_backup', 'true').lower() != 'false'
# Create backup of existing database if requested
backup_path = None
if create_backup and os.path.exists(COMPETITIONS_DB):
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
backup_filename = f'competitions_backup_{timestamp}.sqlite'
backup_path = os.path.join(DATA_DIRECTORY, 'db', backup_filename)
try:
import shutil
shutil.copy2(COMPETITIONS_DB, backup_path)
logging.info(f"Created database backup: {backup_path}")
except Exception as backup_error:
logging.error(f"Failed to create backup: {backup_error}")
return jsonify({
'success': False,
'error': 'backup_failed',
'message': f'Failed to create backup: {str(backup_error)}'
}), 500
# Save uploaded file
try:
file.save(COMPETITIONS_DB)
file_size = os.path.getsize(COMPETITIONS_DB)
logging.info(f"Admin {session.get('email')} uploaded new database: {file.filename} ({file_size} bytes)")
# Try to verify it's a valid SQLite database
try:
import sqlite3
conn = sqlite3.connect(COMPETITIONS_DB)
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [row[0] for row in cursor.fetchall()]
conn.close()
return jsonify({
'success': True,
'message': 'Database uploaded successfully',
'backup_created': backup_path is not None,
'backup_path': os.path.basename(backup_path) if backup_path else None,
'file_size': file_size,
'tables_found': len(tables),
'tables': tables
})
except sqlite3.Error as db_error:
logging.error(f"Uploaded file is not a valid SQLite database: {db_error}")
# Restore backup if validation fails
if backup_path and os.path.exists(backup_path):
import shutil
shutil.copy2(backup_path, COMPETITIONS_DB)
logging.info("Restored database from backup after failed validation")
return jsonify({
'success': False,
'error': 'invalid_database',
'message': 'Uploaded file is not a valid SQLite database',
'database_restored': backup_path is not None
}), 400
except Exception as save_error:
logging.error(f"Failed to save uploaded database: {save_error}")
return jsonify({
'success': False,
'error': 'save_failed',
'message': f'Failed to save database: {str(save_error)}'
}), 500
except Exception as e:
logging.error(f"Error uploading database: {e}")
return jsonify({
'success': False,
'error': 'upload_failed',
'message': str(e)
}), 500
@skala_admin_api.route('/database/info', methods=['GET'])
@admin_required
def database_info():
"""Get information about the current database.
Returns metadata including file size, modification time, and table list.
Requires admin permissions.
"""
try:
if not os.path.exists(COMPETITIONS_DB):
return jsonify({
'success': False,
'error': 'database_not_found',
'message': 'Database file does not exist'
}), 404
# Get file stats
file_stats = os.stat(COMPETITIONS_DB)
file_size = file_stats.st_size
modified_time = datetime.fromtimestamp(file_stats.st_mtime).isoformat()
created_time = datetime.fromtimestamp(file_stats.st_ctime).isoformat()
# Get database schema information
import sqlite3
conn = sqlite3.connect(COMPETITIONS_DB)
cursor = conn.cursor()
# Get table list
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;")
tables = [row[0] for row in cursor.fetchall()]
# Get row counts for each table
table_info = {}
for table in tables:
try:
cursor.execute(f"SELECT COUNT(*) FROM {table};")
count = cursor.fetchone()[0]
table_info[table] = {'row_count': count}
except Exception:
table_info[table] = {'row_count': 'unknown'}
conn.close()
return jsonify({
'success': True,
'database': {
'path': COMPETITIONS_DB,
'file_size': file_size,
'file_size_mb': round(file_size / (1024 * 1024), 2),
'modified': modified_time,
'created': created_time,
'table_count': len(tables),
'tables': table_info
}
})
except Exception as e:
logging.error(f"Error getting database info: {e}")
return jsonify({
'success': False,
'error': 'info_failed',
'message': str(e)
}), 500
@skala_admin_api.route('/database/backups', methods=['GET'])
@admin_required
def list_backups():
"""List all available database backup files.
Returns list of backup files in the database directory.
Requires admin permissions.
"""
try:
db_dir = os.path.join(DATA_DIRECTORY, 'db')
if not os.path.exists(db_dir):
return jsonify({
'success': True,
'backups': []
})
# Find all backup files
import glob
backup_pattern = os.path.join(db_dir, 'competitions_backup_*.sqlite')
backup_files = glob.glob(backup_pattern)
backups = []
for backup_file in backup_files:
file_stats = os.stat(backup_file)
backups.append({
'filename': os.path.basename(backup_file),
'path': backup_file,
'size': file_stats.st_size,
'size_mb': round(file_stats.st_size / (1024 * 1024), 2),
'created': datetime.fromtimestamp(file_stats.st_ctime).isoformat(),
'modified': datetime.fromtimestamp(file_stats.st_mtime).isoformat()
})
# Sort by creation time, newest first
backups.sort(key=lambda x: x['created'], reverse=True)
return jsonify({
'success': True,
'backup_count': len(backups),
'backups': backups
})
except Exception as e:
logging.error(f"Error listing backups: {e}")
return jsonify({
'success': False,
'error': 'list_failed',
'message': str(e)
}), 500
# ============= Image Management Endpoints =============
@skala_admin_api.route('/images/list', methods=['GET'])
@admin_required
def list_images():
"""List all uploaded images in the uploads directory.
Returns list of all image files with metadata.
Requires admin permissions.
"""
try:
if not os.path.exists(UPLOAD_FOLDER):
return jsonify({
'success': True,
'image_count': 0,
'images': [],
'total_size': 0
})
images = []
total_size = 0
# Walk through uploads directory
for root, dirs, files in os.walk(UPLOAD_FOLDER):
for filename in files:
filepath = os.path.join(root, filename)
rel_path = os.path.relpath(filepath, UPLOAD_FOLDER)
try:
file_stats = os.stat(filepath)
file_size = file_stats.st_size
total_size += file_size
images.append({
'filename': filename,
'path': rel_path,
'full_path': filepath,
'size': file_size,
'size_kb': round(file_size / 1024, 2),
'modified': datetime.fromtimestamp(file_stats.st_mtime).isoformat(),
'is_image': allowed_image(filename)
})
except Exception as e:
logging.warning(f"Error reading file {filepath}: {e}")
# Sort by modification time, newest first
images.sort(key=lambda x: x['modified'], reverse=True)
return jsonify({
'success': True,
'image_count': len(images),
'images': images,
'total_size': total_size,
'total_size_mb': round(total_size / (1024 * 1024), 2),
'upload_folder': UPLOAD_FOLDER
})
except Exception as e:
logging.error(f"Error listing images: {e}")
return jsonify({
'success': False,
'error': 'list_failed',
'message': str(e)
}), 500
@skala_admin_api.route('/images/download-all', methods=['GET'])
@admin_required
def download_all_images():
"""Download all images as a zip archive.
Creates a zip file containing all images from the uploads directory.
Requires admin permissions.
"""
try:
if not os.path.exists(UPLOAD_FOLDER):
return jsonify({
'success': False,
'error': 'folder_not_found',
'message': 'Uploads folder does not exist'
}), 404
import zipfile
import io
# Create zip file in memory
memory_file = io.BytesIO()
with zipfile.ZipFile(memory_file, 'w', zipfile.ZIP_DEFLATED) as zf:
# Walk through uploads directory
for root, dirs, files in os.walk(UPLOAD_FOLDER):
for filename in files:
filepath = os.path.join(root, filename)
arcname = os.path.relpath(filepath, UPLOAD_FOLDER)
try:
zf.write(filepath, arcname)
except Exception as e:
logging.warning(f"Error adding {filepath} to zip: {e}")
memory_file.seek(0)
# Generate filename with timestamp
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
download_name = f'skala3ma_images_{timestamp}.zip'
logging.info(f"Admin {session.get('email')} downloading all images: {download_name}")
return send_file(
memory_file,
as_attachment=True,
download_name=download_name,
mimetype='application/zip'
)
except Exception as e:
logging.error(f"Error downloading images: {e}")
return jsonify({
'success': False,
'error': 'download_failed',
'message': str(e)
}), 500
@skala_admin_api.route('/images/upload-all', methods=['POST'])
@admin_required
def upload_all_images():
"""Upload a zip archive of images and extract to uploads directory.
Accepts a zip file and extracts all contents to the uploads folder.
Can optionally clear existing images before upload.
Requires admin permissions.
Form data:
- file: Zip archive containing images
- clear_existing: (optional) 'true' to delete all existing images first
- create_backup: (optional) 'true' to create backup before replacing (default: true)
"""
try:
# Check if file was uploaded
if 'file' not in request.files:
return jsonify({
'success': False,
'error': 'no_file',
'message': 'No file provided in request'
}), 400
file = request.files['file']
# Check if file was selected
if file.filename == '':
return jsonify({
'success': False,
'error': 'empty_filename',
'message': 'No file selected'
}), 400
# Validate file is a zip
if not file.filename.lower().endswith('.zip'):
return jsonify({
'success': False,
'error': 'invalid_file_type',
'message': 'File must be a zip archive'
}), 400
clear_existing = request.form.get('clear_existing', 'false').lower() == 'true'
create_backup = request.form.get('create_backup', 'true').lower() != 'false'
# Create backup if requested
backup_path = None
if create_backup and os.path.exists(UPLOAD_FOLDER):
import shutil
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
backup_filename = f'uploads_backup_{timestamp}.zip'
backup_path = os.path.join(DATA_DIRECTORY, backup_filename)
try:
import zipfile
with zipfile.ZipFile(backup_path, 'w', zipfile.ZIP_DEFLATED) as zf:
for root, dirs, files in os.walk(UPLOAD_FOLDER):
for filename in files:
filepath = os.path.join(root, filename)
arcname = os.path.relpath(filepath, UPLOAD_FOLDER)
zf.write(filepath, arcname)
logging.info(f"Created images backup: {backup_path}")
except Exception as backup_error:
logging.error(f"Failed to create backup: {backup_error}")
return jsonify({
'success': False,
'error': 'backup_failed',
'message': f'Failed to create backup: {str(backup_error)}'
}), 500
# Clear existing images if requested
if clear_existing and os.path.exists(UPLOAD_FOLDER):
import shutil
try:
shutil.rmtree(UPLOAD_FOLDER)
os.makedirs(UPLOAD_FOLDER)
logging.info(f"Cleared existing uploads folder")
except Exception as clear_error:
logging.error(f"Failed to clear uploads folder: {clear_error}")
return jsonify({
'success': False,
'error': 'clear_failed',
'message': f'Failed to clear existing images: {str(clear_error)}'
}), 500
# Ensure uploads folder exists
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
# Extract zip file
import zipfile
import io
try:
zip_data = io.BytesIO(file.read())
extracted_count = 0
skipped_count = 0
with zipfile.ZipFile(zip_data, 'r') as zip_ref:
file_list = zip_ref.namelist()
for file_info in zip_ref.filelist:
# Skip directories and system files
if file_info.is_dir() or file_info.filename.startswith('__MACOSX'):
continue
# Extract file
try:
zip_ref.extract(file_info, UPLOAD_FOLDER)
extracted_count += 1
except Exception as extract_error:
logging.warning(f"Failed to extract {file_info.filename}: {extract_error}")
skipped_count += 1
logging.info(f"Admin {session.get('email')} uploaded images zip: {extracted_count} files extracted, {skipped_count} skipped")
return jsonify({
'success': True,
'message': 'Images uploaded successfully',
'extracted_count': extracted_count,
'skipped_count': skipped_count,
'backup_created': backup_path is not None,
'backup_path': os.path.basename(backup_path) if backup_path else None,
'cleared_existing': clear_existing
})
except zipfile.BadZipFile:
return jsonify({
'success': False,
'error': 'invalid_zip',
'message': 'Uploaded file is not a valid zip archive'
}), 400
except Exception as extract_error:
logging.error(f"Failed to extract images: {extract_error}")
return jsonify({
'success': False,
'error': 'extract_failed',
'message': f'Failed to extract images: {str(extract_error)}'
}), 500
except Exception as e:
logging.error(f"Error uploading images: {e}")
return jsonify({
'success': False,
'error': 'upload_failed',
'message': str(e)
}), 500
@skala_admin_api.route('/images/info', methods=['GET'])
@admin_required
def images_info():
"""Get information about the uploads directory.
Returns metadata including folder size, file count, and image count.
Requires admin permissions.
"""
try:
if not os.path.exists(UPLOAD_FOLDER):
return jsonify({
'success': True,
'uploads_folder': UPLOAD_FOLDER,
'exists': False,
'total_files': 0,
'image_files': 0,
'total_size': 0
})
total_files = 0
image_files = 0
total_size = 0
for root, dirs, files in os.walk(UPLOAD_FOLDER):
for filename in files:
filepath = os.path.join(root, filename)
try:
file_stats = os.stat(filepath)
total_size += file_stats.st_size
total_files += 1
if allowed_image(filename):
image_files += 1
except Exception:
pass
return jsonify({
'success': True,
'uploads_folder': UPLOAD_FOLDER,
'exists': True,
'total_files': total_files,
'image_files': image_files,
'other_files': total_files - image_files,
'total_size': total_size,
'total_size_mb': round(total_size / (1024 * 1024), 2)
})
except Exception as e:
logging.error(f"Error getting images info: {e}")
return jsonify({
'success': False,
'error': 'info_failed',
'message': str(e)
}), 500
@skala_admin_api.route('/images/delete', methods=['POST'])
@admin_required
def delete_image():
"""Delete a specific image file.
Accepts JSON with the relative path of the image to delete.
Requires admin permissions.
JSON body:
- path: Relative path to the image file (from uploads folder)
"""
try:
data = request.get_json(silent=True) or {}
rel_path = data.get('path')
if not rel_path:
return jsonify({
'success': False,
'error': 'missing_path',
'message': 'Image path is required'
}), 400
# Construct full path
full_path = os.path.join(UPLOAD_FOLDER, rel_path)
# Security check: ensure path is within uploads folder
full_path = os.path.abspath(full_path)
uploads_abs = os.path.abspath(UPLOAD_FOLDER)
if not full_path.startswith(uploads_abs):
return jsonify({
'success': False,
'error': 'invalid_path',
'message': 'Invalid file path'
}), 400
# Check if file exists
if not os.path.exists(full_path):
return jsonify({
'success': False,
'error': 'file_not_found',
'message': 'Image file not found'
}), 404
# Delete the file
try:
os.remove(full_path)
logging.info(f"Admin {session.get('email')} deleted image: {rel_path}")
return jsonify({
'success': True,
'message': 'Image deleted successfully',
'path': rel_path
})
except Exception as delete_error:
logging.error(f"Failed to delete image {rel_path}: {delete_error}")
return jsonify({
'success': False,
'error': 'delete_failed',
'message': f'Failed to delete image: {str(delete_error)}'
}), 500
except Exception as e:
logging.error(f"Error deleting image: {e}")
return jsonify({
'success': False,
'error': 'delete_failed',
'message': str(e)
}), 500
# ============= Health Check =============
@skala_admin_api.route('/health', methods=['GET'])
@admin_required
def admin_health():
"""Health check endpoint for admin API.
Returns basic status information.
Requires admin permissions.
"""
return jsonify({
'success': True,
'service': 'skala_admin_api',
'version': '1.0.0',
'timestamp': datetime.now().isoformat(),
'admin_user': session.get('email')
})