class Bed(db.Model): __tablename__ = 'beds' id = db.Column(db.Integer, primary_key=True, autoincrement=True) bed_name = db.Column(db.String(60), nullable=False) bed_desc = db.Column(db.String(120)) bed_loc = db.Column( db.String(60)) # refer to a place (room, level, or building) bed_pic = db.Column(db.String(60)) # refer to staff/person/etc. bed_status = db.Column( db.Boolean) # refer to (used, unused, etc.) need to list more production_date = db.Column(db.DateTime) is_used = db.Column(db.Boolean) current_safety = db.Column(db.Integer) current_security = db.Column(db.Integer) current_productivity = db.Column(db.Integer) bed_history = db.relationship('BedHistory', backref='history_bed', lazy='dynamic') bed_safety = db.relationship('BedSafety', backref='safety_bed', lazy='dynamic') bed_security = db.relationship('BedSecurity', backref='security_bed', lazy='dynamic') bed_productivity = db.relationship('BedProductivity', backref='productivity_bed', lazy='dynamic') def __repr__(self): return '<Bed {}>'.format(self.bed_name)
class MedicalDevice(db.Model): __tablename__ = 'medical_devices' id = db.Column(db.Integer, primary_key=True, autoincrement=True) device_name = db.Column(db.String(60), nullable=False) device_desc = db.Column(db.String(120)) device_loc = db.Column( db.String(60)) # refer to a place (room, level, or building) device_pic = db.Column(db.String(60)) # refer to staff/person/etc. device_status = db.Column( db.Boolean) # refer to (used, unused, etc.) need to list more production_date = db.Column(db.DateTime) is_on = db.Column(db.Boolean) current_safety = db.Column(db.Integer) current_security = db.Column(db.Integer) current_productivity = db.Column(db.Integer) device_type_id = db.Column(db.Integer, db.ForeignKey('medical_devices_type.id')) device_history = db.relationship('MedicalDeviceHistory', backref='history_device', lazy='dynamic') device_safety = db.relationship('MedicalDeviceSafety', backref='safety_device', lazy='dynamic') device_security = db.relationship('MedicalDeviceSecurity', backref='security_device', lazy='dynamic') device_productivity = db.relationship('MedicalDeviceProductivity', backref='productivity_device', lazy='dynamic') def __repr__(self): return '<Device {}>'.format(self.device_name)
class Room(db.Model): __tablename__ = 'rooms' id = db.Column(db.Integer, primary_key=True, autoincrement=True) room_name = db.Column(db.String(60), nullable=False) room_pic = db.Column(db.String(60)) # refer to staff/person/etc. current_safety = db.Column(db.Integer) current_security = db.Column(db.Integer) current_productivity = db.Column(db.Integer) level_id = db.Column(db.Integer, db.ForeignKey('levels.id')) rooms_history = db.relationship('RoomHistory', backref='history_room', lazy='dynamic') rooms_safety = db.relationship('RoomSafety', backref='safety_room', lazy='dynamic') rooms_security = db.relationship('RoomSecurity', backref='security_room', lazy='dynamic') rooms_productivity = db.relationship('RoomProductivity', backref='productivity_room', lazy='dynamic') def __repr__(self): return '<Room {}>'.format(self.room_name)
class MedicalDeviceType(db.Model): __tablename__ = 'medical_devices_type' id = db.Column(db.Integer, primary_key=True, autoincrement=True) type_name = db.Column(db.String(120)) type_desc = db.Column(db.String(240)) devices = db.relationship('MedicalDevice', backref='device_type', lazy='dynamic')
class BedSecurity(db.Model): __tablename__ = 'beds_security' id = db.Column(db.Integer, primary_key=True, autoincrement=True) bed_id = db.Column(db.Integer, db.ForeignKey('beds.id')) current_loc = db.Column(db.String(60)) # refer to a place current_pic = db.Column(db.String(60)) # refer to a staff def __repr__(self): return '<BedSecurity {}>'.format(self.security_bed.current_security)
class User(db.Model): """ User Model for storing user related details """ __tablename__ = "users" id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(255), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) registered_on = db.Column(db.DateTime, nullable=False) admin = db.Column(db.Boolean, nullable=False, default=False) def __init__(self, email, password, admin=False): self.email = email self.password = bcrypt.generate_password_hash( password, app.config.get('BCRYPT_LOG_ROUNDS') ).decode() self.registered_on = datetime.datetime.now() self.admin = admin def encode_auth_token(self, user_id): """ Generates the Auth Token :return: string """ try: payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=5), 'iat': datetime.datetime.utcnow(), 'sub': user_id } return jwt.encode( payload, app.config.get('SECRET_KEY'), algorithm='HS256' ) except Exception as e: return e @staticmethod def decode_auth_token(auth_token): """ Validates the auth token :param auth_token: :return: integer|string """ try: payload = jwt.decode(auth_token, app.config.get('SECRET_KEY')) is_blacklisted_token = BlacklistToken.check_blacklist(auth_token) if is_blacklisted_token: return 'Token blacklisted. Please log in again.' else: return payload['sub'] except jwt.ExpiredSignatureError: return 'Signature expired. Please log in again.' except jwt.InvalidTokenError: return 'Invalid token. Please log in again.'
class Testcase(db.Model): id = db.Column(db.Integer, primary_key=True) nodeid = db.Column(db.String(80), nullable=False) remark = db.Column(db.String(120)) def as_dict(self): """ 返回一个标准的Python结构体 :return: """ return {'id': self.id, 'nodeid': self.nodeid, 'remark': self.remark}
class MedicalDeviceSecurity(db.Model): __tablename__ = 'medical_devices_security' id = db.Column(db.Integer, primary_key=True, autoincrement=True) device_id = db.Column(db.Integer, db.ForeignKey('medical_devices.id')) current_loc = db.Column(db.String(60)) # refer to a place current_pic = db.Column(db.String(60)) # refer to a staff def __repr__(self): return '<DeviceSecurity {}>'.format( self.security_device.current_security)
class BlacklistToken(db.Model): """ Token Model for storing JWT tokens """ __tablename__ = 'blacklist_tokens' id = db.Column(db.Integer, primary_key=True, autoincrement=True) token = db.Column(db.String(500), unique=True, nullable=False) blacklisted_on = db.Column(db.DateTime, nullable=False) def __init__(self, token): self.token = token self.blacklisted_on = datetime.datetime.now() def __repr__(self): return '<id: token: {}'.format(self.token) @staticmethod def check_blacklist(auth_token): # check whether auth token has been blacklisted res = BlacklistToken.query.filter_by(token=str(auth_token)).first() if res: return True else: return False
class Level(db.Model): __tablename__ = 'levels' id = db.Column(db.Integer, primary_key=True, autoincrement=True) level_name = db.Column(db.String(60), nullable=False) level_pic = db.Column(db.String(60), nullable=True) # refer to staff/person/etc. count_rooms = db.Column(db.Integer) current_safety = db.Column(db.Integer) current_security = db.Column(db.Integer) current_productivity = db.Column(db.Integer) building_id = db.Column(db.Integer, db.ForeignKey('buildings.id')) rooms = db.relationship('Room', backref='room_level', lazy='dynamic') levels_history = db.relationship('LevelHistory', backref='history_level', lazy='dynamic') def __repr__(self): return '<Level {}>'.format(self.level_name)
class Task(db.Model): id = db.Column(db.Integer, primary_key=True) # 人物执行的相关数据信息 remark = db.Column(db.String(120)) report = db.Column(db.String(120)) # 指定时间格式,默认值为当前时间 create_at = db.Column(db.DateTime, default=datetime.datetime.now) def as_dict(self): """ 返回一个标准的Python结构体 :return: """ return {'id': self.id, 'remark': self.remark, 'report': self.report, # 强转为字符串的格式 'create_at': str(self.create_at) }
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True, autoincrement=True) scope = db.Column(db.String(60), nullable=False) state_id = db.Column(db.Integer, db.ForeignKey('states.id')) hospital_id = db.Column(db.Integer, db.ForeignKey('hospitals.id')) users = db.relationship('User', backref='user_role', lazy='dynamic') def __repr__(self): return '<Role: {}>'.format(self.scope)
class State(db.Model): __tablename__ = 'states' id = db.Column(db.Integer, primary_key=True, autoincrement=True) state_name = db.Column(db.String(60), nullable=False) count_hospitals = db.Column(db.Integer) current_safety = db.Column(db.Integer) current_security = db.Column(db.Integer) current_productivity = db.Column(db.Integer) roles = db.relationship('Role', backref='role_state', lazy='dynamic') hospitals = db.relationship('Hospital', backref='hospital_state', lazy='dynamic') states_history = db.relationship('StateHistory', backref='history_state', lazy='dynamic') def __repr__(self): return '<State: {}>'.format(self.state_name)
class RevokedToken(db.Model): __tablename__ = 'revoked_tokens' id = db.Column(db.Integer, primary_key=True, autoincrement=True) jti = db.Column(db.String(120), nullable=False) def __init__(self, jti): self.jti = jti @classmethod def is_token_revoked(cls, jti): query = cls.query.filter_by(jti = jti).first() return bool(query) def db_commit(self): db.session.add(self) db.session.commit()
class Hospital(db.Model): __tablename__ = 'hospitals' id = db.Column(db.Integer, primary_key=True, autoincrement=True) hospital_name = db.Column(db.String(60), nullable=False) count_buildings = db.Column(db.Integer) count_rooms = db.Column(db.Integer) current_safety = db.Column(db.Integer) current_security = db.Column(db.Integer) current_productivity = db.Column(db.Integer) state_id = db.Column(db.Integer, db.ForeignKey('states.id')) roles = db.relationship('Role', backref='role_hospital', lazy='dynamic') buildings = db.relationship('Building', backref='building_hospital', lazy='dynamic') hospitals_history = db.relationship('HospitalHistory', backref='history_hospital', lazy='dynamic') def __repr__(self): return '<Hospital {}>'.format(self.hospital_name)