File size: 4,029 Bytes
48322a4 f6fd5fc 48322a4 7e03cc0 48322a4 7e03cc0 48322a4 | 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 | from pydantic import BaseModel, Field, EmailStr, ConfigDict, BeforeValidator
from typing import Optional, Annotated
from datetime import datetime, timezone
from bson import ObjectId
from enum import Enum
# --- Custom Type for MongoDB ObjectId ---
PyObjectId = Annotated[str, BeforeValidator(str)]
# --- Base Model ---
class MongoBaseModel(BaseModel):
"""Base model to handle MongoDB's _id field mapping automatically."""
id: Optional[PyObjectId] = Field(alias="_id", default=None)
model_config = ConfigDict(
populate_by_name=True,
arbitrary_types_allowed=True,
)
# --- 1. User Roles & Authentication Models ---
class UserRole(str, Enum):
TEACHER = "teacher"
HOD = "hod"
SCHOOL_ADMIN = "school_admin"
SUPER_ADMIN = "super_admin"
class User(MongoBaseModel):
name: str
email: EmailStr
hashed_password: str
role: UserRole
school_id: Optional[str] = None # CHANGED: Now using UDISE string instead of ObjectId
department_id: Optional[PyObjectId] = None # Only for HODs and Teachers
is_verified: bool = False # NEW: Users must be verified to access dashboards
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
# --- 2. School Model ---
class School(MongoBaseModel):
udise_number: str # CHANGED: UDISE will act as the unique identifier
name: str
address: str
contact_email: Optional[str] = None # CHANGED: Fixed missing email field
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
# --- 3. Department Model ---
class Department(MongoBaseModel):
name: str
school_id: str # CHANGED: Now using UDISE
hod_id: Optional[PyObjectId] = None # Reference to a User with role="hod"
# --- 4. Class Model ---
class ClassModel(MongoBaseModel):
name: str # e.g., "FYBCA"
semester: int
academic_year: str # e.g., "2025-2026"
department_id: Optional[PyObjectId] = None # NEW: Made Optional to prevent validation crash for School Admins
class_teacher_id: Optional[PyObjectId] = None # NEW: Made Optional to prevent validation crash
school_id: str # CHANGED: Now using UDISE
# --- 5. Subject Model ---
class Subject(MongoBaseModel):
code: str # e.g., "BCA-111"
name: str # e.g., "FUNDAMENTALS OF COMPUTERS"
semester: Optional[int] = None # NEW: Added to differentiate subjects by semester
max_marks: float = 100.0 # NEW: Added for custom department subject maps
department_id: Optional[PyObjectId] = None # NEW: Added to link custom templates to a department
class_id: Optional[PyObjectId] = None # CHANGED: Made optional for department-level custom templates
teacher_id: Optional[PyObjectId] = None # CHANGED: Made optional for department-level custom templates
school_id: str # CHANGED: Now using UDISE
# --- 6. Student Model ---
class Student(MongoBaseModel):
roll_number: str
name: str
class_id: PyObjectId
school_id: str # CHANGED: Now using UDISE
department_id: Optional[PyObjectId] = None # NEW: Added to isolate students by department
# --- 7. Academic Record Model (The Fact Table) ---
class AcademicRecord(MongoBaseModel):
school_id: str # CHANGED: Now using UDISE
student_id: PyObjectId
teacher_id: PyObjectId
subject_id: PyObjectId
class_id: PyObjectId
department_id: Optional[PyObjectId] = None # NEW: Added to isolate records by department
# --- Denormalized Data for Fast Dashboards ---
subject_code: str
subject_name: str
# --- Time Context ---
semester: Optional[int] = None # CHANGED: Uncommented and activated for semester filtering
academic_year: str
# --- Metrics ---
marks_obtained: float
total_marks: float
grade: str
attendance_percentage: Optional[float] = 0.0
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) |