class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80)) password = db.Column(db.String(80)) def __init__(self, username, password): self.username = username self.password = password def save_to_db(self): db.session.add(self) db.session.commit() def json(self): return {'username': self.username, 'password': self.password} @classmethod def find_by_username(cls, username): return cls.query.filter_by( username=username).first() # SELECT * FROM users WHERE username=? @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first()
class SensorModel(db.Model): __tablename__ = 'controllers' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) data = db.Column(db.Float(precision=2)) def __init__(self, name, data): self.name = name self.data = data def json(self): return { 'id': self.id, 'name': self.name, 'set_value': self.set_value, 'range': self.range, 'unit': self.unit } @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first( ) # SELECT * FROM controllers WHERE name=? LIMIT 1 def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class ClusterModel(db.Model): __tablename__ = 'clusters' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) controllers = db.relationship('ControllerModel', lazy='dynamic') def __init__(self, name): self.name = name def json(self): return {'name': self.name, 'controllers': [controller.json() for controller in self.controllers.all()]} @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class Base(db.Model): __abstract__ = True id = db.Column(db.Integer(), primary_key=True, nullable=False, autoincrement=True) date_created = db.Column(db.DateTime, default=db.func.current_timestamp()) date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class ControllerModel(db.Model): __tablename__ = 'controllers' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) set_value = db.Column(db.Integer) range = db.Column(db.Integer) unit = db.Column(db.String(80)) clusted_id = db.Column(db.Integer, db.ForeignKey('clusters.id')) cluster = db.relationship('ClusterModel') def __init__(self, name, set_value, range, unit, cluster_id): self.name = name self.set_value = set_value self.range = range self.unit = unit self.clusted_id = cluster_id def json(self): return {'id': self.id, 'name': self.name, 'set_value': self.set_value, 'range': self.range, 'unit': self.unit} @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() # SELECT * FROM controllers WHERE name=? LIMIT 1 def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()