class QuestionTag(db.Model): __tablename__ = 'QuestionTag' questionId = db.Column(db.BigInteger, db.ForeignKey('Question.questionId', ondelete='CASCADE'), primary_key=True, nullable=False) tagId = db.Column(db.BigInteger, db.ForeignKey('Tag.tagId', ondelete='CASCADE'), primary_key=True, nullable=False) time = db.Column(db.DateTime, default=datetime.now(), nullable=False) def __init__(self, questionId, tagId, time): self.questionId = questionId self.tagId = tagId self.time = time # 将类转为字典,然后响应json def as_dict(obj): return dict((col.name, getattr(obj, col.name)) \ for col in class_mapper(obj.__class__).mapped_table.c) def get_object(self): return Tag.query.filter(Tag.tagId == self.tagId).first() def get_item(self): return Question.query.filter( Question.questionId == self.questionId).first()
class CourseFilter(db.Model): __tablename__ = 'course_filter' id = db.Column(db.Integer, primary_key=True, autoincrement=True) positive = db.Column(db.Boolean) user_id = db.Column(db.String(36), db.ForeignKey('user.id')) course_ids = db.relationship('CourseIdentifier', backref='filter', lazy='dynamic') def predicate(self, item): # Check if the item matches the String course_id values, not the model objects return (item in [course_id.id for course_id in self.course_ids]) ^ self.positive def apply(self, realm_ids): for realm_id in realm_ids[:]: if self.predicate(realm_id): realm_ids.remove(realm_id) def to_json(self): return { 'id': self.id, 'positive': self.positive, 'course_ids': [id.to_json() for id in self.course_ids], } def __repr__(self): return f'CourseFilter<{", ".join(str(course_id) for course_id in self.course_ids)}>'
class Posts(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.Text) timestamp = db.Column(DateTime, nullable=False, default=datetime.utcnow()) rid = db.Column(db.Integer, index=True, default=0) uid = db.Column(db.Integer, db.ForeignKey('user.id')) __mapper_args__ = {"order_by": timestamp.desc()}
class ProtestSubmission(db.Model): __tablename__ = "protestsubmission" id = db.Column(db.Integer, primary_key=True, autoincrement=True) protest_id = db.Column(db.Integer, db.ForeignKey("protest.id")) address = db.Column(db.String(200)) # lat and lng are hidden to the user on the ui lat = db.Column(db.Float(7)) lng = db.Column(db.Float(7)) description = db.Column(db.String(120)) size = db.Column(db.String(120)) issue_locality = db.Column(db.String(120)) issue_type = db.Column(db.String(120)) timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow) def to_json(self): return { 'id': self.id, 'protest_id': self.protest_id, 'address': self.address, 'lat': self.lat, 'lng': self.lng, 'description': self.description, 'size': self.size, 'issue_locality': self.issue_locality, 'issue_type': self.issue_type.split(',') if self.issue_type else [] }
class Category(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(10), unique=True) items = db.relationship('Item', back_populates='category', cascade='all, delete-orphan')
class User(db.Model): __tablename__ = 'users' id = db.Column(db.String(64), primary_key=True, default=generate_uuid(), nullable=False, unique=True) email = db.Column(db.String(256), unique=True, nullable=False) password = db.Column(db.String(256), nullable=False) created_on = db.Column(db.DateTime, default=datetime.datetime.utcnow) @classmethod def encode_auth_token(cls, user_id): payload = dict(exp=datetime.datetime.utcnow() + datetime.timedelta(seconds=5), iat=datetime.datetime.utcnow(), sub=user_id) return jwt.encode(payload, config.SECRET_KEY, algorithm='HS256') @classmethod def decode_auth_token(cls, auth_token): try: payload = jwt.decode(auth_token, config.SECRET_KEY, algorithms=['HS256']) return payload['sub'] except jwt.ExpiredSignature: return "Signature expired. Please log in again" except jwt.InvalidTokenError: return "Invalid token. Please log in again"
class Menu(db.Model): __tablename__ = 'menus' id = db.Column(db.Integer, primary_key=True) exclude_columns = ['created_at', 'updated_at'] auth_name = Column(db.String(32), nullable=False, index=True) path = Column(db.String(256), nullable=False) level = Column(db.Integer, nullable=True) created_at = Column(db.Date, nullable=True, default=datetime.now) updated_at = Column(db.Date, nullable=True, default=datetime.now) parent_id = db.Column(db.Integer, db.ForeignKey('menus.id')) children = db.relationship('Menu', back_populates='parent') parent = db.relationship('Menu', back_populates='children', remote_side=[id]) # role_id = Column(db.Integer, db.ForeignKey('roles.id')) # role = relationship('Role', backref=db.backref('menus', order_by=id)) # roles = relationship('Role', secondary=association_table, back_populates="menus") # 将back_populates修改为db.backref() 指定 lazy = 'dynamic' 参数,关系两侧返回的查询都可接受额外的过滤器 roles = relationship('Role', secondary=association_table, backref=db.backref("menus", lazy='dynamic')) def __init__(self, auth_name, path, **kwargs): db.Model.__init__(self, auth_name=auth_name, path=path, **kwargs)
class Tag(db.Model): __tablename__ = 'Tag' tagId = db.Column(db.BigInteger, primary_key=True, nullable=False, autoincrement=True) parentId = db.Column(db.BigInteger, db.ForeignKey('Tag.tagId'), nullable=True) name = db.Column(db.Unicode(30), nullable=False) description = db.Column(db.Text, nullable=False) popularity = db.Column(db.Integer, default=0, nullable=False) tagUsers = db.relationship('UserTag', foreign_keys=[UserTag.userId], backref=db.backref('users', lazy='joined'), lazy='dynamic', cascade='all, delete-orphan') articles = db.relationship('ArticleTag', foreign_keys=[ArticleTag.articleId], backref=db.backref('articles', lazy='joined'), lazy='dynamic', cascade='all, delete-orphan') problems = db.relationship('ProblemTag', foreign_keys=[QuestionTag.questionId], backref=db.backref('problems', lazy='joined'), lazy='dynamic', cascade='all, delete-orphan')
class Project(db.Model): __tablename__ = 'project' id = db.Column(db.Integer, primary_key=True, autoincrement=True) project_name = db.Column(db.String, nullable=False) def __repr__(self): return 'id:%s project:%s' % self.id, self.project_name
class Blogcol(db.Model): __tablename__ = 'blogcol' id = db.Column(db.Integer, primary_key=True) # 编号 blog_id = db.Column(db.Integer, db.ForeignKey('blog.id')) # 所属博客 user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 所属用户 add_time = db.Column(db.DateTime, index=True, default=datetime.now) # 收藏博客的时间
class Question(db.Model): id = db.Column(db.String(64), primary_key=True, default=generate_uuid) text = db.Column(db.String(1000), nullable=False) date_created = db.Column(db.DateTime, default=datetime.utcnow()) def __str__(self): return self.text
class Comment(db.Model): __tablename__ = "comment" id = db.Column(db.Integer, primary_key=True) # 编号 content = db.Column(db.Text) # 内容 blog_id = db.Column(db.Integer, db.ForeignKey('blog.id')) # 所属博客 user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 所属用户 add_time = db.Column(db.DateTime, index=True, default=datetime.now) # 收藏博客的时间
class GroupUser(db.Model): __tablename__ = 'group_user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) group_id = db.Column(db.Integer, nullable=False) uid = db.Column(db.Integer, nullable=False) def __repr__(self): return 'group_id:%s uid:%s' % self.group_id, self.uid
class Edit(db.Model): __tablename__ = 'edit' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(50), nullable=False) content = db.Column(db.Text, nullable=False) datetime = db.Column(db.DateTime, default=datetime.now) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('edits'))
class Permission(db.Model): __tablename__ = 'm_permission' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), nullable=True) parent_id = db.Column(db.Integer, nullable=True) endpoint = db.Column(db.String(255), nullable=True) # manager = db.relationship('Manager',backref='m_permission',uselist=False) # manager_id = db.Column(db.Integer,db.ForeignKey('m_manager.id'))
class Posts(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer,primary_key=True,autoincrement=True) # title = db.Column(db.String(20),default="无标题") content=db.Column(db.Text(140)) timestamp = db.Column(db.DateTime,default=datetime.utcnow()) rid = db.Column(db.Integer,index=True,default=0) uid = db.Column(db.Integer,db.ForeignKey('user.id'))
class Envip(db.Model): __tablename__ = 'envip' id = db.Column(db.Integer, primary_key=True, autoincrement=True) ip_name = db.Column(db.String(255), nullable=False) ip_type = db.Column(db.String(50), nullable=False) def __repr__(self): return 'ip_name:%s ip_type:%s' % self.ip_name, self.ip_type
class Group(db.Model): __tablename__ = 'group' id = db.Column(db.Integer, primary_key=True, autoincrement=True) group_name = db.Column(db.String(50), nullable=False) project_id = db.Column(db.String(255), nullable=False) def __repr__(self): return 'group_name:%s' % self.group_name
class Tag(db.Model): __tablename__ = "tag" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(100), unique=True) # 标题 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 def __repr__(self): return "<Tag %r>" % self.name
class User(db.Model): #用户表 id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(100), unique=True) password = db.Column(db.String(256), nullable=False) # 定义wai建 article_id = db.Column(db.Integer, db.ForeignKey('article.id'), nullable=True)
class Function(db.Model): __tablename__ = 'function' id = db.Column(db.Integer, primary_key=True, autoincrement=True) func_name = db.Column(db.String(50), nullable=False) port_name = db.Column(db.String(255), nullable=False) request_type = db.Column(db.String(255), nullable=False) def __repr__(self): return 'func_id:%s func_name:%s port_name:%s request_type:%s' % self.id, self.func_name, self.port_name, self.request_type
class Tag(db.Model): __tablename__ = "tag" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(80), unique=True) # 标题 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 movies = db.relationship("Movie", backref='tag') # 电影外键关系关联 def __repr__(self): return "<Tag %r>" % self.name
class Language(db.Model): __tablename__ = 'm_language' id = db.Column(db.Integer, primary_key=True, autoincrement=True) description = db.Column(db.String(255), nullable=True) name = db.Column(db.String(255), nullable=True) shortName = db.Column(db.String(255), nullable=True) createtime = db.Column(db.DateTime, default=datetime.now) # manager_id = db.Column(db.Integer,db.ForeignKey('m_manager.id')) entry = db.relationship('Entry', backref='m_language', uselist=False)
class Auth(db.Model): __tablename__ = "auth" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(100), unique=True) # 名称 url = db.Column(db.String(255), unique=True) # 地址 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 def __repr__(self): return "<Auth %r>" % self.name
class Role(db.Model): __tablename__ = "role" id = db.Column(db.Integer, primary_key=True) # 编号 name = db.Column(db.String(100), unique=True) # 名称 auths = db.Column(db.String(600)) # 权限列表 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 def __repr__(self): return "<Role %r>" % self.name
class UnlikeAnswer(db.Model): __tablename__ = 'UnlikeAnswer' userId = db.Column(db.BigInteger, db.ForeignKey('User.id', ondelete='CASCADE'), primary_key=True) answerId = db.Column(db.BigInteger, db.ForeignKey('Answer.answerId', ondelete='CASCADE'), primary_key=True) time = db.Column(db.DateTime, default=datetime.now(), nullable=False)
class Follow(db.Model): __tablename__ = 'Follow' followerId = db.Column(db.BigInteger, db.ForeignKey('User.id', ondelete='CASCADE'), primary_key=True) followedId = db.Column(db.BigInteger, db.ForeignKey('User.id', ondelete='CASCADE'), primary_key=True) followTime = db.Column(db.DateTime, default=datetime.now())
class ModelBase(db.Model, ModelMixin): __abstract__ = True id = db.Column(db.Integer, primary_key=True, index=True) created_time = db.Column(db.DateTime, default=datetime.now) updated_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
class GroupFunc(db.Model): __tablename__ = 'group_func' id = db.Column(db.Integer, primary_key=True, autoincrement=True) group_id = db.Column(db.Integer, nullable=False) tree_id = db.Column(db.Integer, nullable=False) func_id = db.Column(db.String, nullable=True) def __repr__(self): return 'group_id:%s tree_id:%s func_id:%s' % self.group_id, self.tree_id, self.func_id
class Preview(db.Model): __tablename__ = "preview" id = db.Column(db.Integer, primary_key=True) # 编号 title = db.Column(db.String(255), unique=True) # 标题 logo = db.Column(db.String(255), unique=True) # 封面 addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间 def __repr__(self): return "<Preview %r>" % self.title