99 lines
4.0 KiB
Python
99 lines
4.0 KiB
Python
from app import db
|
|
from datetime import datetime
|
|
|
|
class Subject(db.Model):
|
|
__tablename__ = 'subjects'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
code = db.Column(db.String(20), unique=True, nullable=False, index=True)
|
|
name = db.Column(db.String(200), nullable=False)
|
|
description = db.Column(db.Text)
|
|
department = db.Column(db.String(100), nullable=False)
|
|
credits = db.Column(db.Integer, nullable=False)
|
|
# duration_in_minutes = db.Column(db.Integer, nullable=False)
|
|
is_active = db.Column(db.Boolean, default=True, nullable=False)
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# Relationships
|
|
commissions = db.relationship('Commission', backref='subject', lazy=True, cascade='all, delete-orphan')
|
|
|
|
def to_dict(self):
|
|
"""Convert subject to dictionary"""
|
|
return {
|
|
'id': self.id,
|
|
'code': self.code,
|
|
'name': self.name,
|
|
'description': self.description,
|
|
'department': self.department,
|
|
'credits': self.credits,
|
|
# 'duration_minutes': self.duration_in_minutes,
|
|
'is_active': self.is_active,
|
|
'created_at': self.created_at.isoformat() if self.created_at else None,
|
|
'updated_at': self.updated_at.isoformat() if self.updated_at else None
|
|
}
|
|
|
|
def __repr__(self):
|
|
return f'<Subject {self.code}>'
|
|
|
|
class Commission(db.Model):
|
|
__tablename__ = 'commissions'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
subject_id = db.Column(db.Integer, db.ForeignKey('subjects.id'), nullable=False)
|
|
code = db.Column(db.String(50), nullable=False)
|
|
semester = db.Column(db.String(20), nullable=False)
|
|
year = db.Column(db.Integer, nullable=False)
|
|
teacher_id = db.Column(db.Integer, db.ForeignKey('users.id'))
|
|
max_students = db.Column(db.Integer, nullable=False)
|
|
current_students = db.Column(db.Integer, default=0)
|
|
schedule = db.Column(db.String(255))
|
|
active = db.Column(db.Boolean, default=True) # Changed from is_active to active
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# Virtual property for compatibility
|
|
@property
|
|
def is_active(self):
|
|
return self.active
|
|
|
|
@is_active.setter
|
|
def is_active(self, value):
|
|
self.active = value
|
|
|
|
# Relationships
|
|
reservations = db.relationship('Reservation', backref='commission', lazy=True, cascade='all, delete-orphan')
|
|
teacher = db.relationship('User', backref='teaching_commissions', foreign_keys=[teacher_id])
|
|
|
|
# Virtual field for teacher_name
|
|
@property
|
|
def teacher_name(self):
|
|
return f"{self.teacher.first_name} {self.teacher.last_name}" if self.teacher else "TBD"
|
|
|
|
# Unique constraint
|
|
__table_args__ = (db.UniqueConstraint('subject_id', 'code', 'semester', 'year'),)
|
|
|
|
def to_dict(self):
|
|
"""Convert commission to dictionary"""
|
|
return {
|
|
'id': self.id,
|
|
'subject_id': self.subject_id,
|
|
'code': self.code,
|
|
'semester': self.semester,
|
|
'year': self.year,
|
|
'teacher_name': self.teacher_name,
|
|
'max_students': self.max_students,
|
|
'current_students': self.current_students,
|
|
'schedule': self.schedule,
|
|
'active': self.active,
|
|
'created_at': self.created_at.isoformat() if self.created_at else None,
|
|
'updated_at': self.updated_at.isoformat() if self.updated_at else None,
|
|
'subject': self.subject.to_dict() if self.subject else None
|
|
}
|
|
|
|
def get_full_code(self):
|
|
"""Get full commission code"""
|
|
return f"{self.subject.code if self.subject else 'N/A'}-{self.code}-{self.semester.upper()}{self.year if self.year else ''}"
|
|
|
|
def __repr__(self):
|
|
return f'<Commission {self.get_full_code()}>' |