Files
QPP/problem_loader.py
2025-08-11 21:49:33 +02:00

38 lines
1.3 KiB
Python

import json
import os
import threading
import time
from models import db, Problem
from flask import current_app
def load_problems_from_json(json_path):
if not os.path.exists(json_path):
print(f"Problem JSON file not found: {json_path}")
return
with open(json_path, 'r') as f:
problems = json.load(f)
for p in problems:
# Check if problem already exists by title
existing = Problem.query.filter_by(title=p['title']).first()
# Load test code from solution file if provided
test_code = ''
if 'solution' in p and os.path.exists(p['solution']):
with open(p['solution'], 'r') as sf:
test_code = sf.read()
if existing:
existing.description = p['description']
existing.test_code = test_code
else:
new_problem = Problem(title=p['title'], description=p['description'], test_code=test_code)
db.session.add(new_problem)
db.session.commit()
def schedule_problem_reload(app, json_path, interval_hours=10):
def reload_loop():
while True:
with app.app_context():
load_problems_from_json(json_path)
time.sleep(interval_hours * 3600)
t = threading.Thread(target=reload_loop, daemon=True)
t.start()