Rankmate / database /models.py
sumit1901's picture
Made major changes in ui and functionality to handle the ledger
7e03cc0
Raw History Blame Contribute Delete
4.03 kB
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))