class Number(db.Model): __tablename__ = "numbers" id = db.Column(db.Integer, primary_key=True) number = db.Column(db.String(20)) created = db.Column(db.DateTime(), server_default=func.now())
class BidFile(db.Model): __tablename__ = 'bid_file' __table_args__ = (Index('bookings_partner_name_partner_id_index', 'channel', 'uploaded_at', unique=True), ) # id = db.Column(UUID(as_uuid=True), unique=True, nullable=False, primary_key=True) id = db.Column(UUID(as_uuid=True), unique=True, default=uuid.uuid4(), primary_key=True) channel = db.Column(db.String, index=True) created_at = db.Column(db.DateTime(timezone=True), default=db.func.now()) uploaded_at = db.Column(db.DateTime(timezone=True), default=db.func.now()) updated_at = db.Column(db.DateTime(timezone=True), default=db.func.now(), onupdate=db.func.now()) stored_at = db.Column(db.String)
class Deadlines(db.Model): __tablename__ = 'deadlines' id = db.Column(db.Integer(), primary_key=True) assignment_id = db.Column(db.Integer(), db.ForeignKey('assignment.id')) assignment = db.relationship("Assignment", backref=db.backref('deadlines', order_by=id)) section_id = db.Column(db.Integer(), db.ForeignKey('section.id')) section = db.relationship("Section", backref=db.backref('deadlines', order_by=id)) deadline = db.Column(db.DateTime()) acid = db.Column(db.String(255))
class Post (db.Model, BasicOperations): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.Text, nullable=False) content = db.Column(db.Text, nullable=False) created_at = db.Column(db.DateTime(timezone=True), nullable=False, server_default=db.func.statement_timestamp()) user_id = db.Column(db.Integer, db.ForeignKey( 'users.id', ondelete="cascade")) tags = db.relationship("Tag", secondary="posts_tags", backref="posts") def update_tags(self, new_tags): post_id = self.id current_tags = db.session.query( PostTag.tag_id).filter_by(post_id=post_id).all() curr_tags = [tag[0] for tag in current_tags] tags_to_add = set(new_tags) - set(curr_tags) tags_to_remove = set(curr_tags) - set(new_tags) for tag_id in tags_to_add: post_tag = PostTag(post_id=post_id, tag_id=tag_id) db.session.add(post_tag) for tag_id in tags_to_remove: PostTag.query.filter_by(post_id=post_id, tag_id=tag_id).delete() db.session.commit() @classmethod def get_last(cls, limit=1): posts = db.session.query(Post).order_by( Post.created_at.desc()).limit(limit).all() return posts @classmethod def add(cls, title, content, user_id): new_post = cls(title=title, content=content, user_id=user_id) db.session.add(new_post) db.session.commit() return new_post @classmethod def update_by_id(cls, post_id, title, content, tags): # get the post from the table post = cls.get(post_id) # update the post info from the form post.title = title post.content = content post.update_tags(tags) update_db(post) return post
class User(UserMixin, db.Model): """ 用户表 """ __tablename__ = 'user' id = db.Column(db.Integer(), primary_key=True) # 小程序 user_info openId = db.Column(db.String(128), unique=True) nickName = db.Column(db.String(50)) gender = db.Column(db.Integer(), server_default='0') # 1 男 0 女 city = db.Column(db.String(120)) province = db.Column(db.String(120)) country = db.Column(db.String(120)) avatarUrl = db.Column(db.String(200)) # 阅读器配置 preference = db.Column(db.Integer(), server_default='0') # 0 女 1 男 brightness = db.Column(db.Integer(), server_default='30') # 10~100 亮度 fontSize = db.Column(db.Integer(), server_default='14') # 字号 background = db.Column(db.String(10), default='B1') # B1 ~ B6 内置背景 turn = db.Column(db.String(10), default='T1') # T1 仿真 T2 平滑 T3 无 翻页模式 last_read = db.Column(db.Integer()) # 最后阅读一本书 last_read_chapter_id = db.Column(db.Integer()) # 最后阅读一本书的章节id modified = db.Column(db.DateTime(), server_default=func.now()) created = db.Column(db.DateTime(), server_default=func.now()) def __init__(self, data): self.openId = data['openId'] self.update_info(data) def update_info(self, data): self.nickName = data['nickName'] self.gender = data['gender'] self.city = data['city'] self.province = data['province'] self.country = data['country'] self.avatarUrl = data['avatarUrl']
class TaskAssignment(db.Model): __tablename__ = 'taskAssignments' id = db.Column(db.Integer, primary_key=True) start_date = db.Column('start_date', db.DateTime(), default=datetime.datetime.now) end_date = db.Column(db.DateTime, nullable=True) user_id = db.Column(db.Integer()) task_id = db.Column(db.Integer, db.ForeignKey('tasks.id')) task = db.relationship("Task", back_populates="assignments") def __init__(self, task_id, user_id): self.task_id = task_id self.user_id = user_id
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) # User authentication information username = db.Column(db.String(50), nullable=False, unique=True) password = db.Column(db.String(255), nullable=False, server_default='') # User email information email = db.Column(db.String(255), nullable=False, unique=True) confirmed_at = db.Column(db.DateTime()) # User information active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0') first_name = db.Column(db.String(100), nullable=False, server_default='') last_name = db.Column(db.String(100), nullable=False, server_default='')
class Image(db.Model): id = db.Column(db.String(), primary_key=True, default=lambda: str(uuid.uuid4())) parent_id = db.Column(db.String(), db.ForeignKey('image.id'), nullable=True) caption = db.Column(db.String()) url = db.Column(db.String()) finished = db.Column(db.Boolean(), default=False) created_at = db.Column(db.DateTime(), default=lambda: datetime.now().strftime("%Y-%m-%d, %H:%M:%S")) derivatives = db.relationship('Image', backref=db.backref('parent', uselist=False), lazy='joined', remote_side=[id], primaryjoin="Image.id == Image.parent_id", foreign_keys=[parent_id]) def save(self): db.session.add(self) db.session.commit() def __repr__(self): return f"<Image id={self.id} caption={self.caption} url={self.url} parent_id={self.parent_id}>"
class Food(Entry, db.Model): def __init__(self) -> None: super().__init__() __tablename__ = 'food' record_id = db.Column(db.Integer, db.ForeignKey('{}.id'.format(Record.__tablename__)), nullable=False) food_id = db.Column(db.String(20), nullable=False) calories = db.Column(db.Integer, nullable=False) name = db.Column(db.String(100), nullable=False) description = db.Column(db.String(100), nullable=True) meal_type = db.Column(db.String(), db.Enum(MealType), nullable=True) created_date = db.Column(db.DateTime(), nullable=False) insulin_per_20_grams = db.Column(db.Integer, nullable=True) @classmethod def getById(cls, id): return cls.query.filter_by(id=id).first() @classmethod def deleteById(cls, id): return cls.query.filter_by(id=id).delete()
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(50), nullable=False) created_at = db.Column(db.DateTime(), nullable=False, default=db.func.current_timestamp())
class Entry(Resource): takenAt = db.Column(db.DateTime(), nullable=False)