raj-tomar001 commited on
Commit
8e74368
·
verified ·
1 Parent(s): 386894b

Update submit_response.py

Browse files
Files changed (1) hide show
  1. submit_response.py +103 -104
submit_response.py CHANGED
@@ -1,104 +1,103 @@
1
- import json
2
- import logging
3
- from flask import jsonify, request
4
- from flask_jwt_extended import get_jwt_identity
5
- from models import get_db_connection
6
- import mysql.connector
7
- from datetime import datetime
8
-
9
- import traceback
10
- def submit_quiz():
11
- if request.method == 'POST':
12
- logging.info("Received a POST request to submit quiz.")
13
-
14
- quiz_id = request.form.get('quiz_id')
15
- user_response = request.form.getlist('user_response')
16
- theme = request.form.get('theme')
17
- start_time = request.form.get('start_time')
18
- end_time = request.form.get('end_time')
19
-
20
- logging.info(f"Received data: quiz_id={quiz_id}, user_response={user_response}, theme={theme}")
21
-
22
- if not quiz_id or not user_response or not theme or not start_time or not end_time:
23
- return jsonify({"error": "Please provide all required fields."}), 400
24
-
25
- user_id_attempt = get_jwt_identity()
26
- logging.info(f"Authenticated user ID: {user_id_attempt}")
27
-
28
- connection = get_db_connection()
29
- if not connection:
30
- return jsonify({"error": "Database connection failed."}), 500
31
-
32
- try:
33
- cursor = connection.cursor(dictionary=True)
34
- logging.info("Database connection established.")
35
-
36
-
37
- cursor.execute("SELECT theme_quiz_table FROM themes WHERE theme = %s", (theme,))
38
- theme_entry = cursor.fetchone()
39
-
40
- if not theme_entry:
41
- return jsonify({"error": "Invalid theme or theme not supported."}), 400
42
-
43
- theme_table = theme_entry['theme_quiz_table']
44
-
45
-
46
- cursor.execute(f"SELECT * FROM {theme_table} WHERE quiz_id = %s AND FIND_IN_SET(%s, user_id_attempt) > 0", (quiz_id, user_id_attempt))
47
- if cursor.fetchone():
48
- return jsonify({"error": "You have already submitted this quiz."}), 403
49
-
50
-
51
- cursor.execute(f"SELECT * FROM {theme_table} WHERE quiz_id = %s", (quiz_id,))
52
- quiz = cursor.fetchone()
53
-
54
- if not quiz:
55
- return jsonify({"error": "Quiz not found."}), 404
56
-
57
-
58
- questions_data = json.loads(quiz.get('questions_by_llm') or quiz.get('questions_by_master', '[]'))
59
- correct_options = json.loads(quiz.get('correct_options_llm') or quiz.get('correct_options_master', '[]'))
60
-
61
- if len(user_response) != len(correct_options):
62
- return jsonify({"error": "Number of responses does not match the number of questions."}), 400
63
-
64
-
65
- score = sum(1 for i in range(len(correct_options)) if user_response[i] == correct_options[i])
66
- time_taken = str(datetime.strptime(end_time, "%H:%M:%S") - datetime.strptime(start_time, "%H:%M:%S"))
67
-
68
-
69
- cursor.execute("""
70
- INSERT INTO quiz_response (quiz_id, theme, user_id_attempt, user_response, score, time_taken)
71
- VALUES (%s, %s, %s, %s, %s, %s)
72
- """, (quiz_id, theme, user_id_attempt, json.dumps(user_response), score, time_taken))
73
-
74
-
75
- cursor.execute(f"""
76
- UPDATE {theme_table}
77
- SET user_id_attempt = CONCAT(COALESCE(user_id_attempt, ''), %s, ',')
78
- WHERE quiz_id = %s
79
- """, (user_id_attempt, quiz_id))
80
-
81
-
82
- cursor.execute("""
83
- UPDATE users
84
- SET total_score = total_score + %s
85
- WHERE id = %s
86
- """, (score, user_id_attempt))
87
-
88
- connection.commit()
89
- logging.info(f"User {user_id_attempt} submitted quiz {quiz_id} with score {score} and time taken {time_taken}.")
90
-
91
- return jsonify({"quiz_id": quiz_id, "score": score, "time_taken": time_taken}), 200
92
-
93
- except mysql.connector.Error as err:
94
- logging.error(f"MySQL Error: {err}")
95
- return jsonify({"error": "Database operation failed."}), 500
96
-
97
- except Exception as e:
98
- logging.error(f"Unexpected error: {str(e)}")
99
- return jsonify({"error": "An unexpected error occurred."}), 500
100
-
101
- finally:
102
- cursor.close()
103
- connection.close()
104
- logging.info("Database connection closed.")
 
1
+ import json
2
+ import logging
3
+ from flask import jsonify, request
4
+ from flask_jwt_extended import get_jwt_identity
5
+ from models import get_db_connection
6
+ import mysql.connector
7
+ from datetime import datetime
8
+
9
+ import traceback
10
+ def submit_quiz():
11
+ if request.method == 'POST':
12
+ logging.info("Received a POST request to submit quiz.")
13
+
14
+ quiz_id = request.form.get('quiz_id')
15
+ user_response = request.form.getlist('user_response')
16
+ theme = request.form.get('theme')
17
+ start_time = request.form.get('start_time')
18
+ end_time = request.form.get('end_time')
19
+
20
+ logging.info(f"Received data: quiz_id={quiz_id}, user_response={user_response}, theme={theme}")
21
+
22
+ if not quiz_id or not user_response or not theme or not start_time or not end_time:
23
+ return jsonify({"error": "Please provide all required fields."}), 400
24
+
25
+ user_id_attempt = get_jwt_identity()
26
+ logging.info(f"Authenticated user ID: {user_id_attempt}")
27
+
28
+ connection = get_db_connection()
29
+ if not connection:
30
+ return jsonify({"error": "Database connection failed."}), 500
31
+
32
+ cursor = connection.cursor()
33
+ logging.info("Database connection established.")
34
+
35
+ try:
36
+ cursor.execute("SELECT theme_quiz_table FROM themes WHERE theme = %s", (theme,))
37
+ theme_entry = cursor.fetchone()
38
+
39
+ if not theme_entry:
40
+ return jsonify({"error": "Invalid theme or theme not supported."}), 400
41
+
42
+ theme_table = theme_entry['theme_quiz_table']
43
+
44
+
45
+ cursor.execute(f"SELECT * FROM {theme_table} WHERE quiz_id = %s AND FIND_IN_SET(%s, user_id_attempt) > 0", (quiz_id, user_id_attempt))
46
+ if cursor.fetchone():
47
+ return jsonify({"error": "You have already submitted this quiz."}), 403
48
+
49
+
50
+ cursor.execute(f"SELECT * FROM {theme_table} WHERE quiz_id = %s", (quiz_id,))
51
+ quiz = cursor.fetchone()
52
+
53
+ if not quiz:
54
+ return jsonify({"error": "Quiz not found."}), 404
55
+
56
+
57
+ questions_data = json.loads(quiz.get('questions_by_llm') or quiz.get('questions_by_master', '[]'))
58
+ correct_options = json.loads(quiz.get('correct_options_llm') or quiz.get('correct_options_master', '[]'))
59
+
60
+ if len(user_response) != len(correct_options):
61
+ return jsonify({"error": "Number of responses does not match the number of questions."}), 400
62
+
63
+
64
+ score = sum(1 for i in range(len(correct_options)) if user_response[i] == correct_options[i])
65
+ time_taken = str(datetime.strptime(end_time, "%H:%M:%S") - datetime.strptime(start_time, "%H:%M:%S"))
66
+
67
+
68
+ cursor.execute("""
69
+ INSERT INTO quiz_response (quiz_id, theme, user_id_attempt, user_response, score, time_taken)
70
+ VALUES (%s, %s, %s, %s, %s, %s)
71
+ """, (quiz_id, theme, user_id_attempt, json.dumps(user_response), score, time_taken))
72
+
73
+
74
+ cursor.execute(f"""
75
+ UPDATE {theme_table}
76
+ SET user_id_attempt = CONCAT(COALESCE(user_id_attempt, ''), %s, ',')
77
+ WHERE quiz_id = %s
78
+ """, (user_id_attempt, quiz_id))
79
+
80
+
81
+ cursor.execute("""
82
+ UPDATE users
83
+ SET total_score = total_score + %s
84
+ WHERE id = %s
85
+ """, (score, user_id_attempt))
86
+
87
+ connection.commit()
88
+ logging.info(f"User {user_id_attempt} submitted quiz {quiz_id} with score {score} and time taken {time_taken}.")
89
+
90
+ return jsonify({"quiz_id": quiz_id, "score": score, "time_taken": time_taken}), 200
91
+
92
+ except mysql.connector.Error as err:
93
+ logging.error(f"MySQL Error: {err}")
94
+ return jsonify({"error": "Database operation failed."}), 500
95
+
96
+ except Exception as e:
97
+ logging.error(f"Unexpected error: {str(e)}")
98
+ return jsonify({"error": "An unexpected error occurred."}), 500
99
+
100
+ finally:
101
+ cursor.close()
102
+ connection.close()
103
+ logging.info("Database connection closed.")