ngwakomadikwe commited on
Commit
cb049d7
·
verified ·
1 Parent(s): 31ac6a0

Create models.py (Database Models)

Browse files
Files changed (1) hide show
  1. models.py (Database Models) +324 -0
models.py (Database Models) ADDED
@@ -0,0 +1,324 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Database models for ThutoAI School Integration
3
+ """
4
+ from datetime import datetime
5
+ import sqlite3
6
+ import os
7
+
8
+ class DatabaseManager:
9
+ def __init__(self, db_path="thutoai_school.db"):
10
+ self.db_path = db_path
11
+ self.init_database()
12
+
13
+ def get_connection(self):
14
+ return sqlite3.connect(self.db_path)
15
+
16
+ def init_database(self):
17
+ """Initialize the database with all required tables"""
18
+ conn = self.get_connection()
19
+ cursor = conn.cursor()
20
+
21
+ # Students table
22
+ cursor.execute('''
23
+ CREATE TABLE IF NOT EXISTS students (
24
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
25
+ student_id TEXT UNIQUE NOT NULL,
26
+ name TEXT NOT NULL,
27
+ email TEXT,
28
+ grade_level TEXT,
29
+ class_section TEXT,
30
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
31
+ )
32
+ ''')
33
+
34
+ # Announcements table
35
+ cursor.execute('''
36
+ CREATE TABLE IF NOT EXISTS announcements (
37
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
38
+ title TEXT NOT NULL,
39
+ content TEXT NOT NULL,
40
+ priority TEXT DEFAULT 'normal',
41
+ target_audience TEXT DEFAULT 'all',
42
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
43
+ expires_at TIMESTAMP,
44
+ is_active BOOLEAN DEFAULT 1
45
+ )
46
+ ''')
47
+
48
+ # Examinations table
49
+ cursor.execute('''
50
+ CREATE TABLE IF NOT EXISTS examinations (
51
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
52
+ exam_name TEXT NOT NULL,
53
+ subject TEXT NOT NULL,
54
+ exam_date DATE NOT NULL,
55
+ exam_time TIME,
56
+ duration_minutes INTEGER,
57
+ location TEXT,
58
+ grade_level TEXT,
59
+ instructions TEXT,
60
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
61
+ )
62
+ ''')
63
+
64
+ # Tests/Assignments table
65
+ cursor.execute('''
66
+ CREATE TABLE IF NOT EXISTS tests_assignments (
67
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
68
+ title TEXT NOT NULL,
69
+ subject TEXT NOT NULL,
70
+ type TEXT NOT NULL, -- 'test', 'assignment', 'quiz', 'project'
71
+ due_date DATE NOT NULL,
72
+ due_time TIME,
73
+ description TEXT,
74
+ grade_level TEXT,
75
+ total_marks INTEGER,
76
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
77
+ )
78
+ ''')
79
+
80
+ # Grades table
81
+ cursor.execute('''
82
+ CREATE TABLE IF NOT EXISTS grades (
83
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
84
+ student_id TEXT NOT NULL,
85
+ subject TEXT NOT NULL,
86
+ assessment_type TEXT NOT NULL, -- 'test', 'exam', 'assignment', 'quiz'
87
+ assessment_name TEXT NOT NULL,
88
+ marks_obtained REAL NOT NULL,
89
+ total_marks REAL NOT NULL,
90
+ percentage REAL NOT NULL,
91
+ grade_letter TEXT,
92
+ date_recorded DATE NOT NULL,
93
+ teacher_comments TEXT,
94
+ FOREIGN KEY (student_id) REFERENCES students (student_id)
95
+ )
96
+ ''')
97
+
98
+ # School Events table
99
+ cursor.execute('''
100
+ CREATE TABLE IF NOT EXISTS school_events (
101
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
102
+ event_name TEXT NOT NULL,
103
+ event_date DATE NOT NULL,
104
+ event_time TIME,
105
+ location TEXT,
106
+ description TEXT,
107
+ event_type TEXT, -- 'holiday', 'sports', 'cultural', 'academic'
108
+ is_holiday BOOLEAN DEFAULT 0,
109
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
110
+ )
111
+ ''')
112
+
113
+ # Admin Users table
114
+ cursor.execute('''
115
+ CREATE TABLE IF NOT EXISTS admin_users (
116
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
117
+ username TEXT UNIQUE NOT NULL,
118
+ password_hash TEXT NOT NULL,
119
+ full_name TEXT NOT NULL,
120
+ email TEXT,
121
+ role TEXT DEFAULT 'admin', -- 'admin', 'teacher', 'principal'
122
+ is_active BOOLEAN DEFAULT 1,
123
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
124
+ last_login TIMESTAMP
125
+ )
126
+ ''')
127
+
128
+ # Syllabus table
129
+ cursor.execute('''
130
+ CREATE TABLE IF NOT EXISTS syllabus (
131
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
132
+ subject TEXT NOT NULL,
133
+ grade_level TEXT NOT NULL,
134
+ chapter_number INTEGER,
135
+ chapter_title TEXT NOT NULL,
136
+ topics TEXT NOT NULL, -- JSON array of topics
137
+ learning_objectives TEXT,
138
+ duration_weeks INTEGER,
139
+ resources TEXT, -- JSON array of resources/books
140
+ assessment_methods TEXT,
141
+ created_by TEXT NOT NULL,
142
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
143
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
144
+ is_active BOOLEAN DEFAULT 1
145
+ )
146
+ ''')
147
+
148
+ # Timetable table
149
+ cursor.execute('''
150
+ CREATE TABLE IF NOT EXISTS timetable (
151
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
152
+ class_section TEXT NOT NULL, -- e.g., "10-A", "11-B"
153
+ day_of_week TEXT NOT NULL, -- 'Monday', 'Tuesday', etc.
154
+ period_number INTEGER NOT NULL,
155
+ start_time TIME NOT NULL,
156
+ end_time TIME NOT NULL,
157
+ subject TEXT NOT NULL,
158
+ teacher_name TEXT,
159
+ room_number TEXT,
160
+ created_by TEXT NOT NULL,
161
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
162
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
163
+ is_active BOOLEAN DEFAULT 1
164
+ )
165
+ ''')
166
+
167
+ # File Uploads table (for syllabus documents, etc.)
168
+ cursor.execute('''
169
+ CREATE TABLE IF NOT EXISTS file_uploads (
170
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
171
+ filename TEXT NOT NULL,
172
+ original_filename TEXT NOT NULL,
173
+ file_path TEXT NOT NULL,
174
+ file_type TEXT NOT NULL, -- 'syllabus', 'timetable', 'announcement'
175
+ file_size INTEGER,
176
+ uploaded_by TEXT NOT NULL,
177
+ related_id INTEGER, -- ID of related record (syllabus, announcement, etc.)
178
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
179
+ )
180
+ ''')
181
+
182
+ conn.commit()
183
+ conn.close()
184
+ print("Database initialized successfully!")
185
+
186
+ def add_sample_data(self):
187
+ """Add sample data for testing"""
188
+ conn = self.get_connection()
189
+ cursor = conn.cursor()
190
+
191
+ # Sample announcements
192
+ announcements = [
193
+ ("Important: Mid-term Exam Schedule Released",
194
+ "The mid-term examination schedule has been posted. Please check your exam dates and prepare accordingly. All exams will be held in the main examination hall.",
195
+ "high", "all"),
196
+ ("Library Hours Extended",
197
+ "Due to upcoming exams, library hours have been extended until 8 PM on weekdays.",
198
+ "normal", "all"),
199
+ ("Sports Day Registration Open",
200
+ "Registration for annual sports day is now open. Last date for registration: March 15th.",
201
+ "normal", "all")
202
+ ]
203
+
204
+ cursor.executemany('''
205
+ INSERT OR IGNORE INTO announcements (title, content, priority, target_audience)
206
+ VALUES (?, ?, ?, ?)
207
+ ''', announcements)
208
+
209
+ # Sample examinations
210
+ examinations = [
211
+ ("Mid-term Mathematics", "Mathematics", "2024-03-20", "09:00", 180, "Hall A", "Grade 10", "Bring calculator and geometry box"),
212
+ ("Mid-term Physics", "Physics", "2024-03-22", "09:00", 180, "Hall B", "Grade 10", "Formula sheet will be provided"),
213
+ ("Mid-term Chemistry", "Chemistry", "2024-03-25", "09:00", 180, "Hall A", "Grade 10", "Periodic table will be provided")
214
+ ]
215
+
216
+ cursor.executemany('''
217
+ INSERT OR IGNORE INTO examinations (exam_name, subject, exam_date, exam_time, duration_minutes, location, grade_level, instructions)
218
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
219
+ ''', examinations)
220
+
221
+ # Sample tests/assignments
222
+ tests = [
223
+ ("Chapter 5 Quiz", "Mathematics", "quiz", "2024-03-15", "10:00", "Quadratic equations quiz", "Grade 10", 20),
224
+ ("Physics Lab Report", "Physics", "assignment", "2024-03-18", "23:59", "Submit pendulum experiment report", "Grade 10", 25),
225
+ ("Chemistry Unit Test", "Chemistry", "test", "2024-03-16", "11:00", "Atomic structure and bonding", "Grade 10", 50)
226
+ ]
227
+
228
+ cursor.executemany('''
229
+ INSERT OR IGNORE INTO tests_assignments (title, subject, type, due_date, due_time, description, grade_level, total_marks)
230
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
231
+ ''', tests)
232
+
233
+ # Sample student
234
+ cursor.execute('''
235
+ INSERT OR IGNORE INTO students (student_id, name, email, grade_level, class_section)
236
+ VALUES (?, ?, ?, ?, ?)
237
+ ''', ("STU001", "John Doe", "[email protected]", "Grade 10", "10-A"))
238
+
239
+ # Sample grades
240
+ grades = [
241
+ ("STU001", "Mathematics", "test", "Algebra Test", 85, 100, 85.0, "A", "2024-02-15", "Excellent work!"),
242
+ ("STU001", "Physics", "quiz", "Motion Quiz", 18, 20, 90.0, "A+", "2024-02-20", "Great understanding"),
243
+ ("STU001", "Chemistry", "assignment", "Lab Report 1", 22, 25, 88.0, "A", "2024-02-25", "Good analysis")
244
+ ]
245
+
246
+ cursor.executemany('''
247
+ INSERT OR IGNORE INTO grades (student_id, subject, assessment_type, assessment_name, marks_obtained, total_marks, percentage, grade_letter, date_recorded, teacher_comments)
248
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
249
+ ''', grades)
250
+
251
+ # Sample events
252
+ events = [
253
+ ("Spring Break", "2024-03-30", "00:00", "School", "Spring holidays begin", "holiday", 1),
254
+ ("Science Fair", "2024-04-15", "09:00", "School Auditorium", "Annual science exhibition", "academic", 0),
255
+ ("Sports Day", "2024-04-20", "08:00", "School Ground", "Annual sports competition", "sports", 0)
256
+ ]
257
+
258
+ cursor.executemany('''
259
+ INSERT OR IGNORE INTO school_events (event_name, event_date, event_time, location, description, event_type, is_holiday)
260
+ VALUES (?, ?, ?, ?, ?, ?, ?)
261
+ ''', events)
262
+
263
+ # Sample admin user (password: admin123)
264
+ import hashlib
265
+ password_hash = hashlib.sha256("admin123".encode()).hexdigest()
266
+ cursor.execute('''
267
+ INSERT OR IGNORE INTO admin_users (username, password_hash, full_name, email, role)
268
+ VALUES (?, ?, ?, ?, ?)
269
+ ''', ("admin", password_hash, "School Administrator", "[email protected]", "admin"))
270
+
271
+ # Sample syllabus data
272
+ syllabus_data = [
273
+ ("Mathematics", "Grade 10", 1, "Algebra Fundamentals",
274
+ '["Linear equations", "Quadratic equations", "Polynomials", "Factoring"]',
275
+ "Students will understand and solve algebraic equations", 4,
276
+ '["NCERT Mathematics Grade 10", "RD Sharma", "Online Khan Academy"]',
277
+ "Tests, Assignments, Projects", "admin"),
278
+ ("Physics", "Grade 10", 1, "Light and Reflection",
279
+ '["Laws of reflection", "Mirrors", "Refraction", "Lenses"]',
280
+ "Understanding light behavior and optical instruments", 3,
281
+ '["NCERT Physics Grade 10", "HC Verma", "Lab experiments"]',
282
+ "Practical exams, Theory tests", "admin"),
283
+ ("Chemistry", "Grade 10", 1, "Acids, Bases and Salts",
284
+ '["Properties of acids", "Properties of bases", "pH scale", "Salt formation"]',
285
+ "Chemical properties and reactions of acids and bases", 3,
286
+ '["NCERT Chemistry Grade 10", "Lab manual"]',
287
+ "Lab reports, Unit tests", "admin")
288
+ ]
289
+
290
+ cursor.executemany('''
291
+ INSERT OR IGNORE INTO syllabus (subject, grade_level, chapter_number, chapter_title, topics, learning_objectives, duration_weeks, resources, assessment_methods, created_by)
292
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
293
+ ''', syllabus_data)
294
+
295
+ # Sample timetable data for Grade 10-A
296
+ timetable_data = [
297
+ ("10-A", "Monday", 1, "09:00", "09:45", "Mathematics", "Mr. Smith", "Room 101", "admin"),
298
+ ("10-A", "Monday", 2, "09:45", "10:30", "Physics", "Ms. Johnson", "Room 201", "admin"),
299
+ ("10-A", "Monday", 3, "10:45", "11:30", "Chemistry", "Dr. Brown", "Lab 1", "admin"),
300
+ ("10-A", "Monday", 4, "11:30", "12:15", "English", "Mrs. Davis", "Room 102", "admin"),
301
+ ("10-A", "Monday", 5, "13:00", "13:45", "History", "Mr. Wilson", "Room 103", "admin"),
302
+ ("10-A", "Monday", 6, "13:45", "14:30", "Physical Education", "Coach Miller", "Gym", "admin"),
303
+
304
+ ("10-A", "Tuesday", 1, "09:00", "09:45", "Physics", "Ms. Johnson", "Room 201", "admin"),
305
+ ("10-A", "Tuesday", 2, "09:45", "10:30", "Mathematics", "Mr. Smith", "Room 101", "admin"),
306
+ ("10-A", "Tuesday", 3, "10:45", "11:30", "English", "Mrs. Davis", "Room 102", "admin"),
307
+ ("10-A", "Tuesday", 4, "11:30", "12:15", "Chemistry", "Dr. Brown", "Lab 1", "admin"),
308
+ ("10-A", "Tuesday", 5, "13:00", "13:45", "Computer Science", "Mr. Tech", "Computer Lab", "admin"),
309
+ ("10-A", "Tuesday", 6, "13:45", "14:30", "Art", "Ms. Creative", "Art Room", "admin"),
310
+ ]
311
+
312
+ cursor.executemany('''
313
+ INSERT OR IGNORE INTO timetable (class_section, day_of_week, period_number, start_time, end_time, subject, teacher_name, room_number, created_by)
314
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
315
+ ''', timetable_data)
316
+
317
+ conn.commit()
318
+ conn.close()
319
+ print("Sample data added successfully!")
320
+
321
+ # Initialize database when module is imported
322
+ if __name__ == "__main__":
323
+ db = DatabaseManager()
324
+ db.add_sample_data()