t Claude (claude-opus-4-5-thinking) commited on
Commit
32f5035
·
1 Parent(s): bce2021

fix: create question row if missing when saving classification

Browse files

The update_single endpoint now checks if a questions row exists for
the image_id, and creates one if it doesn't. This fixes the issue
where manual classifications weren't being saved when the question
row was missing.

Co-Authored-By: Claude (claude-opus-4-5-thinking) <noreply@anthropic.com>

Files changed (1) hide show
  1. routes/questions.py +21 -4
routes/questions.py CHANGED
@@ -310,13 +310,30 @@ def update_question_classification_single():
310
  if not image_id: return jsonify({'error': 'Image ID is required'}), 400
311
  try:
312
  conn = get_db_connection()
313
- owner = conn.execute("SELECT s.user_id FROM images i JOIN sessions s ON i.session_id = s.id WHERE i.id = ?", (image_id,)).fetchone()
314
- if not owner or owner['user_id'] != current_user.id:
 
 
 
 
315
  conn.close(); return jsonify({'error': 'Unauthorized'}), 403
316
- conn.execute('UPDATE questions SET subject = ?, chapter = ? WHERE image_id = ?', (subject, chapter, image_id))
 
 
 
 
 
 
 
 
 
 
 
317
  conn.commit(); conn.close()
318
  return jsonify({'success': True})
319
- except Exception as e: return jsonify({'error': str(e)}), 500
 
 
320
 
321
 
322
  @main_bp.route('/question_entry_v2/<session_id>')
 
310
  if not image_id: return jsonify({'error': 'Image ID is required'}), 400
311
  try:
312
  conn = get_db_connection()
313
+ # Get image info and verify ownership
314
+ img_info = conn.execute("""
315
+ SELECT i.session_id, s.user_id FROM images i
316
+ JOIN sessions s ON i.session_id = s.id WHERE i.id = ?
317
+ """, (image_id,)).fetchone()
318
+ if not img_info or img_info['user_id'] != current_user.id:
319
  conn.close(); return jsonify({'error': 'Unauthorized'}), 403
320
+
321
+ # Check if question row exists
322
+ existing = conn.execute('SELECT id FROM questions WHERE image_id = ?', (image_id,)).fetchone()
323
+ if existing:
324
+ conn.execute('UPDATE questions SET subject = ?, chapter = ? WHERE image_id = ?', (subject, chapter, image_id))
325
+ else:
326
+ # Create question row if it doesn't exist
327
+ conn.execute('''
328
+ INSERT INTO questions (session_id, image_id, question_number, subject, chapter, status)
329
+ VALUES (?, ?, '', ?, ?, 'unattempted')
330
+ ''', (img_info['session_id'], image_id, subject, chapter))
331
+
332
  conn.commit(); conn.close()
333
  return jsonify({'success': True})
334
+ except Exception as e:
335
+ current_app.logger.error(f"Error updating classification: {e}")
336
+ return jsonify({'error': str(e)}), 500
337
 
338
 
339
  @main_bp.route('/question_entry_v2/<session_id>')