class Songs(db.Model): __tablename__ = 'song' id = db.Column(db.String(), primary_key=True) title = db.Column(db.String()) artist = db.Column(db.String()) album = db.Column(db.String()) duration = db.Column(db.String()) created = db.Column(db.DateTime()) updated = db.Column(db.DateTime()) def __init__(self, _id, title, artist, album, duration, created, updated): self.id = _id self.title = title self.artist = artist self.album = album self.duration = duration self.created = created self.updated = updated def __repr__(self): return '<id {}>'.format(self.id) def serialize(self): return { 'id': self.id, 'title': self.title, 'artist': self.artist, 'album': self.album, 'duration': self.duration }
class Playlist(db.Model): __tablename__ = 'playlist' id = db.Column(db.String(), primary_key=True) name = db.Column(db.String()) user_id = db.Column(db.String()) created = db.Column(db.DateTime()) updated = db.Column(db.DateTime()) def __init__(self, _id, name, user_id, created, updated): self.id = _id self.name = name self.user_id = user_id self.created = created self.updated = updated def __repr__(self): return '<id {}>'.format(self.id) def serialize(self): return { 'id': self.id, 'name': self.name, 'user_id': self.user_id, 'created_date': self.created }
class PlayListSongs(db.Model): __tablename__ = 'playlistitem' id = db.Column(db.String(), primary_key=True) playlist_id = db.Column(db.String()) song_id = db.Column(db.String()) user_id = db.Column(db.String()) created = db.Column(db.DateTime()) updated = db.Column(db.DateTime()) def __init__(self, _id, playlist_id, song_id, user_id, created, updated): self.id = _id self.playlist_id = playlist_id self.song_id = song_id self.user_id = user_id self.created = created self.updated = updated def __repr__(self): return '<id {}>'.format(self.id) def serialize(self): return { 'id': self.id, 'song_id': self.song_id, 'user_id': self.user_id, 'playlist_id': self.playlist_id }
class CommonMenuOptInfo(db.Model): """ 菜单操作表 """ __tablename__ = 'common_menu_opt_info' id = db.Column(db.Integer, primary_key=True) #操作编号 menu_id = db.Column(db.Integer) #操作代码 opt_code = db.Column(db.String(64)) #操作名称 opt_name = db.Column(db.String(64)) #操作url opt_url = db.Column(db.String(64)) #操作方法 opt_method = db.Column(db.String(64)) #操作状态 opt_status = db.Column(db.Integer) #操作备注 opt_remark = db.Column(db.String(64)) #排序 opt_order = db.Column(db.Integer) # 状态 status = db.Column(db.Integer) # 操作人 operate_user_id = db.Column(db.Integer) # 操作时间 operate_time = db.Column(db.DateTime(), default=datetime.now) def __repr__(self): return '<common_org_info %r>' % self.opt_name
class CommonRoleInfo(db.Model): """ 角色表 """ __tablename__ = 'common_role_info' id = db.Column(db.Integer, primary_key=True) #角色代码 role_code = db.Column(db.String(64), unique=True) #角色名称 role_name = db.Column(db.String(64)) #角色顺序 role_order = db.Column(db.Integer) #备注 role_remark = db.Column(db.Text) # 角色装 role_status = db.Column(db.Integer) # 所属系统 sys_id = db.Column(db.Integer) # 用户状态 status = db.Column(db.Integer) # 操作人 operate_user_id = db.Column(db.Integer) # 操作时间 operate_time = db.Column(db.DateTime(), default=datetime.now) def __repr__(self): return '<common_role_info %r>' % self.role_name
class Show(db.Model): __tablename__ = 'Show' id = db.Column(db.Integer, primary_key=True) artist_id = db.Column(db.Integer, db.ForeignKey('Artist.id'), nullable=False) venue_id = db.Column(db.Integer, db.ForeignKey('Venue.id'), nullable=False) start_time = db.Column(db.DateTime())
class Messages(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.String(), unique=False) category = db.Column(db.String(), unique=False) datetime = db.Column(db.DateTime(), unique=False) from_id = None to_id = None is_read = db.Column(db.Boolean(), unique=False) is_send = db.Column(db.Boolean(), unique=False)
class Event(db.Model): __tablename__ = "events" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), nullable=False) date = db.Column(db.DateTime(), nullable=False) results = db.relationship("Result", backref="event", lazy=True) def __repr__(self): return "Event({}; {}; {})".format(self.name, self.date, self.city)
class CommonHealth(db.Model): """ 健康检查 """ __tablename__ = 'common_health' id = db.Column(db.Integer, primary_key=True) # 请求流水号 timestamp = db.Column(db.String(500)) # 分类 catalog = db.Column(db.String(50)) #请求数据 request_data = db.Column(db.Text) # 返回状态码 response_status_code = db.Column(db.String(100)) # 返回数据 response_data = db.Column(db.Text) # 开始时间 start_time = db.Column(db.DateTime(), default=datetime.now) # 结束时间 end_time = db.Column(db.DateTime(), default=datetime.now) # 请求耗时 time_consuming = db.Column(db.String(100)) # 状态 status = db.Column(db.Integer) # 操作人 operate_user_id = db.Column(db.Integer) # 操作时间 operate_time = db.Column(db.DateTime(), default=datetime.now) def __repr__(self): return '<common_health %r>' % self.id def to_json(self): return JsonUtil.ObjToJson(self)
class Training(db.Model): __tablename__ = "trainings" id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime(), nullable=False) distance = db.Column(db.Float) time = db.Column(db.Interval) average_speed = db.Column(db.Float) elevation = db.Column(db.Integer) rider_id = db.Column(db.Integer, db.ForeignKey("riders.id"), nullable=False)
class CommonMenuInfo(db.Model): """ 菜单表 """ __tablename__ = 'common_menu_info' id = db.Column(db.Integer, primary_key=True) # 菜单代码 menu_code = db.Column(db.String(64), unique=True) # 菜单名称 menu_name = db.Column(db.String(64)) # 菜单导航 menu_nav = db.Column(db.String(64)) # 备注 menu_remark = db.Column(db.String(64)) # url menu_url = db.Column(db.String(64)) #菜单样式 menu_cls = db.Column(db.String(64), unique=True) #菜单级别 menu_level = db.Column(db.Integer) # 类别 menu_type = db.Column(db.Integer) #菜单排序 menu_order = db.Column(db.Integer) # 状态 menu_status = db.Column(db.Integer) #父节点 menu_pid = db.Column(db.String(64)) #是否隐藏 menu_hidden = db.Column(db.Integer) #布局 menu_use_sys_layout = db.Column(db.Integer) #目标 menu_target = db.Column(db.String(64)) # 所属系统 menu_sysid = db.Column(db.String(64)) #spt menu_spt = db.Column(db.String(64)) # 状态 status = db.Column(db.Integer) # 操作人 operate_user_id = db.Column(db.Integer) # 操作时间 operate_time = db.Column(db.DateTime(), default=datetime.now) def __repr__(self): return '<common_org_info %r>' % self.role_name
class User(db.Model): __tablename__ = 'person' id = db.Column(db.String(), primary_key=True) username = db.Column(db.String()) password = db.Column(db.String()) created = db.Column(db.DateTime()) updated = db.Column(db.DateTime()) def __init__(self, _id, username, password, created, updated): self.id = _id self.username = username self.password = password self.created = created self.updated = updated def __repr__(self): return '<id {}>'.format(self.id) def serialize(self): return { 'id': self.id, 'username': self.username, 'password': self.password }
class CommonOrgInfo(db.Model): """ 组织机构表 """ __tablename__ = 'common_org_info' id = db.Column(db.Integer, primary_key=True) #组织机构代码 org_code = db.Column(db.String(64), unique=True) # 组织机构名称 org_name = db.Column(db.String(64), unique=True) #组织机构全称 org_fullname = db.Column(db.String(64), unique=True) # 节点状态 org_status = db.Column(db.Integer) org_area_id = db.Column(db.String(64)) org_remark = db.Column(db.String(200)) # 父节点编号 org_parent_id = db.Column(db.Integer) # 排序 org_sort = db.Column(db.Integer) # 机构类别 org_type = db.Column(db.Integer) # 状态 status = db.Column(db.Integer) # 操作人 operate_user_id = db.Column(db.Integer) # 操作时间 operate_time = db.Column(db.DateTime(), default=datetime.now) def __repr__(self): return '<common_org_info %r>' % self.org_name
class Posts(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(150), nullable=False) text = db.Column(db.TEXT, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) date = db.Column(db.DateTime(), default=datetime.utcnow, index=True)
class User(UserMixin, db.Model): def __init__(self, **kwargs): super(User, self).__init__(**kwargs) if self.role is None: if self.email == current_app.config.get('ADMIN_EMAIL'): self.role = Role.query.filter_by(name='Admin').first() else: self.role = Role.query.filter_by(name='User').first() __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) #Profile info fullname = db.Column(db.String(64)) location = db.Column(db.String(64)) desc = db.Column(db.Text()) member_since = db.Column(db.DateTime(), default=datetime.utcnow) last_visit = db.Column(db.DateTime(), default=datetime.utcnow) #Article posts = db.relationship('Post', backref='author', lazy='dynamic') @property def password(self): raise AttributeError('password is not readable property') @password.setter def password(self, password): self.password_hash = generate_password_hash(password=password) def verify_password(self, password): return check_password_hash(self.password_hash, password) #Check permissions def check_permission(self, permission): if self.role is not None: return (self.role.permissions & permission) == permission return False def check_admin(self): return self.check_permission(Permission.ADMINISTER) # Update vist time def ping(self): self.last_visit = datetime.utcnow() db.session.add(self) db.session.commit() # Generate fake records, Only for test!! @staticmethod def generate_fake(count=100): from sqlalchemy.exc import IntegrityError from random import seed import forgery_py seed() for i in range(count): u = User(email=forgery_py.internet.email_address(), username=forgery_py.internet.user_name(True), password=forgery_py.lorem_ipsum.word(), fullname=forgery_py.name.full_name(), location=forgery_py.address.city(), desc=forgery_py.lorem_ipsum.sentence(), member_since=forgery_py.date.date(True)) db.session.add(u) try: db.session.commit() except IntegrityError: db.session.rollback()
class CommonHttpRequestLogs(db.Model): """ http请求日志表 """ __tablename__ = 'common_http_request_logs' id = db.Column(db.Integer, primary_key=True) # 请求流水号 timestamp = db.Column(db.String(500)) # 分类 catalog = db.Column(db.String(50)) #请求url request_url = db.Column(db.String(500)) #请求数据 request_data = db.Column(db.Text) # 返回状态码 response_status_code = db.Column(db.String(100)) # 返回数据 response_data = db.Column(db.Text) # 请求类别 1 外部请求本应用 2 本应用请求外部 type = db.Column(db.Integer) # 开始时间 start_time = db.Column(db.DateTime(), default=datetime.now) # 结束时间 end_time = db.Column(db.DateTime(), default=datetime.now) # 请求耗时 time_consuming = db.Column(db.String(100)) # 状态 status = db.Column(db.Integer) # 操作人 operate_user_id = db.Column(db.Integer) # 操作时间 operate_time = db.Column(db.DateTime(), default=datetime.now) def __repr__(self): return '<common_http_request_logs %r>' % self.id def to_json(self): return JsonUtil.ObjToJson(self) def start(self, timestamp="20190308000000", catalog="1001", request_url="", type="1", request_data=""): self.timestamp = str(timestamp) self.catalog = str(catalog) self.request_url = str(request_url) self.type = str(type) self.request_data = str(request_data) self.start_time = datetime.now() def end(self, response_status_code="200", response_data=""): self.response_status_code = response_status_code self.response_data = str(response_data) self.end_time = datetime.now() self.time_consuming = (self.end_time - self.start_time).total_seconds()
class CommonUserInfo(UserMixin, db.Model): """ 用户表 """ __tablename__ = 'common_user_info' id = db.Column(db.Integer, primary_key=True) # 账户 login_account = db.Column(db.String(64), unique=True) # 密码 login_password = db.Column(db.String(128)) # 真实名称 user_name = db.Column(db.String(64)) # 用户编号 user_no = db.Column(db.String(64)) # 账号状态 user_status = db.Column(db.String(64)) # 所属系统 user_sys = db.Column(db.String(64)) # 手机号 user_phone = db.Column(db.String(64)) # 用户分类(1:内部人员;2:外部人员) user_type = db.Column(db.String(64)) # 备注 user_remark = db.Column(db.String(64)) # 用户性别(1:男性,0:女性) user_sex = db.Column(db.String(64)) # 性别 user_gender = db.Column(db.Integer) # 邮箱 user_email = db.Column(db.String(64)) # 所属组织机构 user_org = db.Column(db.Integer) # extend # 真实头像 user_icon = db.Column(db.String(64)) # 最后登录时间 last_login = db.Column(db.DateTime(), default=datetime.now) # 用户状态 status = db.Column(db.Integer) # 操作人 operate_user_id = db.Column(db.Integer) # 操作时间 operate_time = db.Column(db.DateTime(), default=datetime.now) roles = db.relationship('CommonRoleInfo', secondary=user_role_mapper, lazy='dynamic', backref=db.backref('users', lazy='dynamic')) orgs = db.relationship('CommonOrgInfo', secondary=user_org_mapper, lazy='dynamic', backref=db.backref('users', lazy='dynamic')) def __repr__(self): return '<common_user_info %r>' % self.__dict__ @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.login_password = generate_password_hash(password) def verify_password(self, password): """ 密码验证方法 :param password: 需要验证的密码 :return: """ return check_password_hash(self.login_password, password) @login_manager.user_loader def load_user(user_id): return CommonUserInfo.query.get(int(user_id)) def to_json(self): return JsonUtil.ObjToJson(self) @staticmethod def generate_fake(count=100): from sqlalchemy.exc import IntegrityError from random import seed import forgery_py seed() for i in range(count): u = CommonUserInfo( user_email=forgery_py.internet.email_address(), login_account=forgery_py.internet.user_name(True), login_password=forgery_py.lorem_ipsum.word(), icon="images/2.jpg") db.session.add(u) try: db.session.commit() except IntegrityError: db.session.rollback()