class Library(db.Model): __tablename__ = "library" id = db.Column("l_id", db.Integer, db.Sequence("library_lid_seq", start=10000, increment=1), primary_key=True) name = db.Column("l_name", db.String(16), nullable=False) status = db.Column("l_status", db.Enum(LibraryStatus), nullable=False) create_date = db.Column("l_date", db.DateTime, server_default=func.now()) attributes = db.relationship("Attribute", back_populates="library", cascade="all") metas = db.relationship("LibraryMeta", back_populates="library", cascade="all") books = db.relationship("Book", back_populates="library", cascade="all") borrows = db.relationship("Borrow", back_populates="library", cascade="all")
class Role(db.Model): id = db.Column("r_id", db.Integer, primary_key=True) name = db.Column("r_name", db.String(32), nullable=False) permissions = db.relationship("Permission", secondary=Can, back_populates="roles")
class LibraryMeta(db.Model): __tablename__ = "librarymeta" id = db.Column("meta_id", db.Integer, primary_key=True, autoincrement=True) lid = db.Column("l_id", db.Integer, db.ForeignKey("library.l_id")) key = db.Column("meta_key", db.String(100)) value = db.Column("meta_value", db.String(500)) library = db.relationship("Library", back_populates="metas")
class Borrow(db.Model): __tablename__ = "borrow" id = db.Column("br_id", db.Integer, primary_key=True) uid = db.Column("u_id", db.Integer, db.ForeignKey("user.u_id"), nullable=False) lid = db.Column("l_id", db.Integer, db.ForeignKey("library.l_id")) bid = db.Column("b_id", db.Integer, db.ForeignKey("book.b_id")) borrow_date = db.Column(db.DateTime) deadtime = db.Column(db.DateTime) return_date = db.Column(db.DateTime, nullable=True) create_date = db.Column(db.DateTime, server_default=func.now()) state = db.Column("state", db.Enum(BorrowState)) user = db.relationship("User", back_populates="borrows") book = db.relationship("Book") library = db.relationship("Library", back_populates="borrows")
class Attribute(db.Model): __tablename__ = "attribute" lid = db.Column("l_id", db.Integer, db.ForeignKey("library.l_id"), primary_key=True) uid = db.Column("u_id", db.Integer, db.ForeignKey("user.u_id"), primary_key=True) rid = db.Column("r_id", db.Integer, db.ForeignKey("role.r_id")) type = db.Column("rt_id", db.Integer, db.ForeignKey("rtype.rt_id")) library = db.relationship("Library", back_populates="attributes", lazy="joined") user = db.relationship("User", back_populates="attributes", lazy="joined") rtype = db.relationship("RType") role = db.relationship("Role")
class User(db.Model): __tablename__ = "user" id = db.Column("u_id", db.Integer, db.Sequence("users_uid_seq", start=10000), primary_key=True) name = db.Column("u_name", db.String(16), nullable=True) gender = db.Column("u_sex", db.Enum(Gender), nullable=True) phone = db.Column("u_tel", db.String(11), unique=True, index=True) email = db.Column("u_email", db.String(50), unique=True, index=True) register_date = db.Column("u_date", db.DateTime, server_default=func.now()) _password = db.Column("u_password", db.String(128), nullable=False) selecting_library_id = db.Column("selecting_library", db.Integer, db.ForeignKey("library.l_id"), nullable=True) attributes = db.relationship("Attribute", back_populates="user", cascade="all") borrows = db.relationship("Borrow", back_populates="user") selecting_library = db.relationship("Library") def set_password(self, raw): self._password = generate_password_hash(raw) def validate_password(self, raw): if not self._password: return False return check_password_hash(self._password, raw) def can(self, permission_name, lid=None): permission = Permission.query.filter_by(name=permission_name).first() attribute = Attribute.query.filter_by( uid=self.id, lid=lid or self.selecting_library_id).first() if attribute is None: return False return permission in attribute.role.permissions