Spaces:
Runtime error
Runtime error
| """ | |
| Notification model - For system notifications to users | |
| """ | |
| from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, JSON, Enum | |
| from sqlalchemy.orm import relationship | |
| from sqlalchemy.sql import func | |
| from config.database import Base | |
| import enum | |
| class NotificationType(enum.Enum): | |
| STUDENT_CONNECTED = "student_connected" | |
| STUDENT_COMPLETED_SESSION = "student_completed_session" | |
| ASSIGNMENT_DUE = "assignment_due" | |
| INVITATION_ACCEPTED = "invitation_accepted" | |
| STUDENT_REMOVED = "student_removed" | |
| SYSTEM = "system" | |
| class Notification(Base): | |
| __tablename__ = "notifications" | |
| id = Column(Integer, primary_key=True, index=True) | |
| # Who the notification is for | |
| user_id = Column(Integer, ForeignKey("users.id"), nullable=False) | |
| # Notification details | |
| type = Column(Enum(NotificationType), nullable=False) | |
| title = Column(String(200), nullable=False) | |
| message = Column(String(1000), nullable=False) | |
| # Additional data (JSON for flexibility) | |
| data = Column(JSON, nullable=True) | |
| # Status | |
| is_read = Column(Boolean, default=False) | |
| read_at = Column(DateTime(timezone=True), nullable=True) | |
| # Timestamps | |
| created_at = Column(DateTime(timezone=True), server_default=func.now()) | |
| # Relationships | |
| user = relationship("User", backref="notifications") | |
| def mark_as_read(self): | |
| """Mark notification as read""" | |
| from datetime import datetime, timezone | |
| self.is_read = True | |
| self.read_at = datetime.now(timezone.utc) | |
| def __repr__(self): | |
| return f"<Notification {self.type.value} for user {self.user_id}>" |