-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
207 lines (174 loc) · 5.51 KB
/
database.py
File metadata and controls
207 lines (174 loc) · 5.51 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
import sqlite3
import json
import os
from datetime import datetime
DB_PATH = os.path.join(os.path.dirname(__file__), '..', 'database', 'edgelab.db')
if not os.path.exists(os.path.dirname(DB_PATH)):
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
def get_conn():
return sqlite3.connect(DB_PATH)
def init_db():
"""Initialize database tables"""
conn = get_conn()
c = conn.cursor()
# assignments table
c.execute('''
CREATE TABLE IF NOT EXISTS assignments (
id TEXT PRIMARY KEY,
language TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT,
data TEXT NOT NULL
)
''')
# submissions table
c.execute('''
CREATE TABLE IF NOT EXISTS submissions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
assignment_id TEXT NOT NULL,
code TEXT NOT NULL,
status TEXT DEFAULT 'pending',
results TEXT,
ai_feedback TEXT,
created_at TEXT NOT NULL,
FOREIGN KEY (assignment_id) REFERENCES assignments(id)
)
''')
conn.commit()
conn.close()
print("Database initialized successfully")
def load_assignments_from_files():
"""Load all assignment JSON files into the database"""
assignments_dir = os.path.join(os.path.dirname(__file__), '..', 'assignments')
if not os.path.exists(assignments_dir):
print(f"Warning: assignments directory not found at {assignments_dir}")
return
conn = get_conn()
c = conn.cursor()
loaded = 0
for filename in os.listdir(assignments_dir):
if not filename.endswith('.json'):
continue
filepath = os.path.join(assignments_dir, filename)
try:
with open(filepath, 'r') as f:
assignment = json.load(f)
c.execute('''
INSERT OR REPLACE INTO assignments (id, language, title, description, data)
VALUES (?, ?, ?, ?, ?)
''', (
assignment['id'],
assignment['language'],
assignment['title'],
assignment['description'],
json.dumps(assignment)
))
loaded += 1
except Exception as e:
print(f"Error loading {filename}: {e}")
conn.commit()
conn.close()
print(f"Loaded {loaded} assignments into database")
def get_all_assignments():
"""Get list of all assignments (without hidden tests)"""
conn = get_conn()
c = conn.cursor()
c.execute('SELECT id, language, title, description FROM assignments')
rows = c.fetchall()
conn.close()
return [
{
'id': row[0],
'language': row[1],
'title': row[2],
'description': row[3]
}
for row in rows
]
def get_assignment(assignment_id):
"""Get full assignment data including tests"""
conn = get_conn()
c = conn.cursor()
c.execute('SELECT data FROM assignments WHERE id = ?', (assignment_id,))
row = c.fetchone()
conn.close()
if row:
return json.loads(row[0])
return None
def get_assignment_public(assignment_id):
"""Get assignment WITHOUT hidden tests (for users)"""
assignment = get_assignment(assignment_id)
if not assignment:
return None
# remove hidden tests before sending to user
public_assignment = assignment.copy()
if 'hidden_tests' in public_assignment:
del public_assignment['hidden_tests']
return public_assignment
def create_submission(assignment_id, code):
"""Create a new submission record"""
conn = get_conn()
c = conn.cursor()
timestamp = datetime.now().isoformat()
c.execute('''
INSERT INTO submissions (assignment_id, code, status, created_at)
VALUES (?, ?, ?, ?)
''', (assignment_id, code, 'pending', timestamp))
submission_id = c.lastrowid
conn.commit()
conn.close()
return submission_id
def update_submission_results(submission_id, status, results, ai_feedback=None):
"""Update submission with test results"""
conn = get_conn()
c = conn.cursor()
c.execute('''
UPDATE submissions
SET status = ?, results = ?, ai_feedback = ?
WHERE id = ?
''', (status, json.dumps(results), ai_feedback, submission_id))
conn.commit()
conn.close()
def get_submission(submission_id):
"""Get submission by ID"""
conn = get_conn()
c = conn.cursor()
c.execute('SELECT * FROM submissions WHERE id = ?', (submission_id,))
row = c.fetchone()
conn.close()
if not row:
return None
return {
'id': row[0],
'assignment_id': row[1],
'code': row[2],
'status': row[3],
'results': json.loads(row[4]) if row[4] else None,
'ai_feedback': row[5],
'created_at': row[6]
}
def get_recent_submissions(limit=10):
"""Get most recent submissions"""
conn = get_conn()
c = conn.cursor()
c.execute('''
SELECT id, assignment_id, status, created_at
FROM submissions
ORDER BY created_at DESC
LIMIT ?
''', (limit,))
rows = c.fetchall()
conn.close()
return [
{
'id': row[0],
'assignment_id': row[1],
'status': row[2],
'created_at': row[3]
}
for row in rows
]
if __name__ == '__main__':
# setup script
init_db()
load_assignments_from_files()