open-law/app/models/section.py

60 lines
1.8 KiB
Python
Raw Normal View History

2023-04-21 12:58:47 +00:00
from app import db
from app.models.utils import BaseModel
class Section(BaseModel):
__tablename__ = "sections"
2023-04-27 13:17:25 +00:00
label = db.Column(db.String(256), unique=False, nullable=False)
2023-04-21 14:20:22 +00:00
about = db.Column(db.Text, unique=False, nullable=True)
2023-04-21 12:58:47 +00:00
# Foreign keys
collection_id = db.Column(db.ForeignKey("collections.id"))
user_id = db.Column(db.ForeignKey("users.id"))
2023-04-21 14:20:22 +00:00
version_id = db.Column(db.ForeignKey("book_versions.id"))
selected_interpretation_id = db.Column(db.Integer, nullable=True)
2023-04-21 12:58:47 +00:00
# Relationships
collection = db.relationship("Collection", viewonly=True)
2023-04-21 14:20:22 +00:00
user = db.relationship("User", viewonly=True)
version = db.relationship("BookVersion", viewonly=True)
interpretations = db.relationship("Interpretation", viewonly=True)
2023-04-21 12:58:47 +00:00
2023-04-27 12:40:48 +00:00
@property
def path(self):
parent = self.collection
grand_parent = parent.parent
path = f"{self.version.book.label} / "
if grand_parent.is_root:
path += f"{parent.label} / "
else:
path += f"{grand_parent.label} / {parent.label} / "
path += self.label
return path
2023-04-28 09:12:46 +00:00
@property
def book_id(self):
_book_id = self.version.book_id
return _book_id
@property
def sub_collection_id(self):
parent = self.collection
grand_parent = parent.parent
if grand_parent.is_root:
_sub_collection_id = parent.id
else:
_sub_collection_id = grand_parent.id
return _sub_collection_id
2023-05-05 09:02:35 +00:00
@property
def active_interpretations(self):
return [
interpretation
for interpretation in self.interpretations
if not interpretation.is_deleted
]
2023-04-21 12:58:47 +00:00
def __repr__(self):
return f"<{self.id}: {self.label}>"