46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
from app import db
|
|
from datetime import datetime
|
|
|
|
class Building(db.Model):
|
|
__tablename__ = 'buildings'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
name = db.Column(db.String(100), unique=True, nullable=False, index=True)
|
|
code = db.Column(db.String(50), unique=True, nullable=True)
|
|
address = db.Column(db.String(255), nullable=True)
|
|
floors = db.Column(db.Integer, default=1, nullable=False)
|
|
description = db.Column(db.Text, nullable=True)
|
|
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
|
|
classrooms = db.relationship('Classroom', backref='building_entity', lazy=True, foreign_keys='Classroom.building_id')
|
|
|
|
@property
|
|
def total_capacity(self):
|
|
"""Total student capacity across active classrooms in this building"""
|
|
return sum(c.capacity for c in self.classrooms if c.is_active)
|
|
|
|
@property
|
|
def classrooms_count(self):
|
|
"""Number of active classrooms"""
|
|
return len([c for c in self.classrooms if c.is_active])
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'id': self.id,
|
|
'name': self.name,
|
|
'code': self.code,
|
|
'address': self.address,
|
|
'floors': self.floors,
|
|
'description': self.description,
|
|
'is_active': self.is_active,
|
|
'classrooms_count': self.classrooms_count,
|
|
'total_capacity': self.total_capacity,
|
|
'created_at': self.created_at.isoformat() if self.created_at else None
|
|
}
|
|
|
|
def __repr__(self):
|
|
return f'<Building {self.name}>'
|