open-law/app/models/book_version.py

46 lines
1.4 KiB
Python
Raw Permalink Normal View History

2023-04-21 13:13:43 +00:00
from datetime import datetime
2023-04-21 12:58:47 +00:00
from app import db
from app.models.utils import BaseModel
class BookVersion(BaseModel):
__tablename__ = "book_versions"
# need to redeclare id to use it in the derivative relationship
id = db.Column(db.Integer, primary_key=True)
2023-04-27 13:17:25 +00:00
semver = db.Column(db.String(16), unique=False, nullable=False)
2023-06-12 12:18:21 +00:00
is_active = db.Column(db.Boolean, default=False)
2023-04-21 13:13:43 +00:00
updated_at = db.Column(db.DateTime, default=datetime.now)
2023-04-21 12:58:47 +00:00
# Foreign keys
derivative_id = db.Column(db.Integer, db.ForeignKey("book_versions.id"))
book_id = db.Column(db.Integer, db.ForeignKey("books.id"))
2023-06-14 11:10:29 +00:00
user_id = db.Column(db.ForeignKey("users.id"))
2023-04-21 12:58:47 +00:00
# Relationships
2023-06-14 11:10:29 +00:00
user = db.relationship("User", viewonly=True)
2023-04-21 12:58:47 +00:00
book = db.relationship("Book", viewonly=True)
derivative = db.relationship("BookVersion", remote_side=[id])
2023-05-18 13:07:59 +00:00
sections = db.relationship("Section", viewonly=True, order_by="desc(Section.id)")
collections = db.relationship(
"Collection", viewonly=True, order_by="desc(Collection.id)"
)
2023-04-21 12:58:47 +00:00
def __repr__(self):
return f"<{self.id}: {self.semver}>"
@property
def root_collection(self):
for collection in self.collections:
if collection.is_root:
return collection
@property
def children_collections(self):
2023-05-26 09:33:47 +00:00
return [
collection
for collection in self.root_collection.children
if not collection.is_deleted
]