class Message(db.Model): __tablename__ = 'messages' id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text, nullable=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow()) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) room_id = db.Column(db.Integer, db.ForeignKey('rooms.id'))
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(254), unique=True, nullable=False) nickname = db.Column(db.String(30), unique=True, nullable=False) password_hash = db.Column(db.String(128)) email_hash = db.Column(db.String(128)) messages = db.relationship('Message', backref='user') room_id = db.Column(db.Integer, db.ForeignKey('rooms.id')) register = db.Column(db.DateTime, default=datetime.utcnow()) lastroom = db.Column(db.String(128)) def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.generate_email_hash() def set_password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def generate_email_hash(self): if self.email is not None and self.email_hash is None: self.email_hash = hashlib.md5(self.email.encode( 'utf-8')).hexdigest() # encode for py23 compatible @property def gravatar(self): return 'https://gravatar.com/avatar/%s?d=robohash' % self.email_hash
class SecondMenu(db.Model): __tablename__ = 'secondmenu' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) icon = db.Column(db.String(50)) url = db.Column(db.String(250)) order = db.Column(db.SmallInteger, default=0) menu_id=db.Column(db.Integer,db.ForeignKey('menu.id'))
class Seism(db.Model): id_num = db.Column(db.Integer, primary_key=True) datetime = db.Column(db.DateTime, nullable=False) depth = db.Column(db.Integer, nullable=False) magnitude = db.Column(db.Float, nullable=False) latitude = db.Column(db.String, nullable=False) longitude = db.Column(db.String, nullable=False) verified = db.Column(db.Boolean, nullable=False) sensor_id = db.Column(db.Integer, db.ForeignKey('sensor.id_num', ondelete="RESTRICT"), nullable=False) sensor = db.relationship("Sensor", back_populates="seisms", uselist=False, passive_deletes="all", single_parent=True) def __repr__(self): return f'<Seism {self.id_num} {self.datetime}'
class Line(GeoBase,db.Model): geo=db.Column(Geometry('LINESTRING')) roles=db.relationship('Role',secondary=roles_lines,back_populates='lines')#可查看该服务的用户角色 layer_id=db.Column(db.Integer,db.ForeignKey('layer.id')) def __init__(self, *args, **kwargs): super(Line, self).__init__(*args, **kwargs) self.uid=uuid.uuid4() self.create_time=datetime.datetime.now() self.create_user=current_user.name def getFields(self): return ['name','create_user','style','uid','create_time'] def hasField(self,field):#判断是否含有该字段 return field in self.getFields() def __getitem__(self,k): if(k=='name'): return self.name if(k=='create_user'): return self.create_user if(k=='style'): return self.style if(k=='uid'): return self.uid if(k=='create_time'): return self.create_time def setProperties(self,properties):#快速设置属性 keys=properties.keys() for key in keys: if(self.hasField(key)): self[key]=properties[key] return self def toGeoJson(self): geojson=db.session.execute(self.geo.ST_AsGeoJSON()).scalar() feature={ "type": "Feature", "geometry": json.loads(geojson), "properties": { } } fields=self.getFields() for field in fields: feature["properties"][field]=str(self[field]) return feature
class Sensor(db.Model): id_num = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) ip = db.Column(db.String(50), nullable=False) port = db.Column(db.Integer, nullable=False) status = db.Column(db.Boolean, nullable=False) active = db.Column(db.Boolean, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("user.id_num"), nullable=True) user = db.relationship("User", back_populates="sensors", uselist=False, single_parent=True) seisms = db.relationship("Seism", back_populates="sensor", passive_deletes="all", single_parent=True) def __repr__(self): return f'<Sensor {self.id_num} {self.name} >'
class ResultModel(db.Model, TimestampMixin): """The result model""" __tablename__ = 'result' id = db.Column(db.Integer(), primary_key=True) mode = db.Column(db.String(16)) time = db.Column(db.SmallInteger()) accuracy = db.Column(db.Float()) wpm = db.Column(db.Float()) user_id = db.Column(db.Integer(), db.ForeignKey('user.id')) def __init__(self, mode, time, accuracy, wpm, user_id): self.mode = mode self.time = time self.accuracy = accuracy self.wpm = wpm self.user_id = user_id def save_to_db(self): db.session.add(self) db.session.commit()
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, index=True) password = db.Column(db.String(128)) name = db.Column(db.String(30)) confirmed = db.Column(db.Boolean, default=False) active = db.Column(db.Boolean, default=True) role_id = db.Column(db.Integer, db.ForeignKey('role.id')) role = db.relationship('Role',cascade='all', back_populates='users') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.set_role('User') def set_password(self, password): self.password = generate_password_hash(password) def set_role(self,role): if role is None: self.role = Role.query.filter_by(name='User').first() # db.session.commit() else: self.role = Role.query.filter_by(name=role).first() # db.session.commit() def __str__(self): return str(self.name) def validate_password(self, password): if(check_password_hash(self.password, password) or self.password==password):#搞清楚xadmin为什么存储的是明文密码后把这个改了 return True else: return False def has_role(self,role): return self.role.name == role @property def is_admin(self): return self.role.name == 'Administrator' @property def is_active(self): return self.active def can(self, permission_name): permission = Permission.query.filter_by(name=permission_name).first() return permission is not None and self.role is not None and permission in self.role.permissions
def validate_password(self, password): print([self.password_hash,password]) return check_password_hash(self.password_hash, password) class _File(db.Model):#文件模型 id=db.Column(db.Integer,primary_key=True) describertion=db.Column(db.String(100))#文件描述 time=db.Column(db.DateTime) uid=db.Column(db.String(60))#唯一值 path=db.Column(db.String(100))#存放路径 def __init__(self): self.uid=uuid.uuid4() # 关联表 roles_permissions = db.Table('roles_permissions', db.Column('role_id', db.Integer, db.ForeignKey('role.id')), db.Column('permission_id', db.Integer, db.ForeignKey('permission.id')) ) roles_menus=db.Table('roles_menus', db.Column('role_id', db.Integer, db.ForeignKey('role.id')), db.Column('menu', db.Integer, db.ForeignKey('menu.id')) ) roles_imgs=db.Table('roles_imgs', db.Column('role_id', db.Integer, db.ForeignKey('role.id')), db.Column('img', db.Integer, db.ForeignKey('img.id')) ) roles_lines=db.Table('roles_lines', db.Column('role_id', db.Integer, db.ForeignKey('role.id')), db.Column('line', db.Integer, db.ForeignKey('line.id')) ) roles_points=db.Table('roles_points',