open-law/app/models/collection.py

28 lines
907 B
Python
Raw Normal View History

2023-04-21 15:58:47 +03:00
from app import db
from app.models.utils import BaseModel
class Collection(BaseModel):
__tablename__ = "collections"
2023-04-27 16:17:25 +03:00
# need to redeclare id to use it in the parent relationship
2023-04-21 15:58:47 +03:00
id = db.Column(db.Integer, primary_key=True)
2023-04-27 16:17:25 +03:00
label = db.Column(db.String(256), unique=False, nullable=False)
2023-04-26 17:57:40 +03:00
about = db.Column(db.Text, unique=False, nullable=True)
2023-04-21 15:58:47 +03:00
is_root = db.Column(db.Boolean, default=False)
is_leaf = db.Column(db.Boolean, default=False)
# Foreign keys
version_id = db.Column(db.ForeignKey("book_versions.id"))
2023-04-27 16:17:25 +03:00
parent_id = db.Column(db.ForeignKey("collections.id"))
2023-04-21 15:58:47 +03:00
# Relationships
version = db.relationship("BookVersion")
children = db.relationship(
"Collection", backref=db.backref("parent", remote_side=[id]), viewonly=True
)
sections = db.relationship("Section")
def __repr__(self):
return f"<{self.id}: {self.label}>"