2023-04-21 12:58:47 +00:00
|
|
|
from app import db
|
|
|
|
from app.models.utils import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
class Collection(BaseModel):
|
|
|
|
__tablename__ = "collections"
|
|
|
|
|
2023-04-27 13:17:25 +00:00
|
|
|
# need to redeclare id to use it in the parent relationship
|
2023-04-21 12:58:47 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2023-04-27 13:17:25 +00:00
|
|
|
label = db.Column(db.String(256), unique=False, nullable=False)
|
2023-04-26 14:57:40 +00:00
|
|
|
about = db.Column(db.Text, unique=False, nullable=True)
|
2023-04-21 12:58:47 +00:00
|
|
|
is_root = db.Column(db.Boolean, default=False)
|
|
|
|
is_leaf = db.Column(db.Boolean, default=False)
|
2023-06-01 14:01:45 +00:00
|
|
|
position = db.Column(db.Integer, default=-1, nullable=True)
|
2023-04-21 12:58:47 +00:00
|
|
|
|
|
|
|
# Foreign keys
|
|
|
|
version_id = db.Column(db.ForeignKey("book_versions.id"))
|
2023-04-27 13:17:25 +00:00
|
|
|
parent_id = db.Column(db.ForeignKey("collections.id"))
|
2023-04-21 12:58:47 +00:00
|
|
|
|
|
|
|
# Relationships
|
|
|
|
version = db.relationship("BookVersion")
|
|
|
|
children = db.relationship(
|
2023-05-18 13:07:59 +00:00
|
|
|
"Collection",
|
|
|
|
backref=db.backref("parent", remote_side=[id]),
|
|
|
|
viewonly=True,
|
2023-05-19 11:29:42 +00:00
|
|
|
order_by="asc(Collection.id)",
|
2023-04-21 12:58:47 +00:00
|
|
|
)
|
|
|
|
sections = db.relationship("Section")
|
2023-05-24 14:06:33 +00:00
|
|
|
access_groups = db.relationship(
|
|
|
|
"AccessGroup",
|
|
|
|
secondary="collections_access_groups",
|
|
|
|
) # access_groups related to current entity
|
2023-04-21 12:58:47 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<{self.id}: {self.label}>"
|
2023-05-03 07:04:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def active_sections(self):
|
2023-06-05 13:12:30 +00:00
|
|
|
items = [section for section in self.sections if not section.is_deleted]
|
|
|
|
items.sort(key=lambda item: item.position)
|
|
|
|
return items
|
2023-05-03 07:04:31 +00:00
|
|
|
|
2023-06-01 12:59:44 +00:00
|
|
|
@property
|
|
|
|
def active_children(self):
|
2023-06-05 13:12:30 +00:00
|
|
|
items = [child for child in self.children if not child.is_deleted]
|
|
|
|
items.sort(key=lambda item: item.position)
|
|
|
|
return items
|
2023-06-01 12:59:44 +00:00
|
|
|
|
2023-06-01 07:29:46 +00:00
|
|
|
@property
|
|
|
|
def book_id(self):
|
|
|
|
return self.version.book_id
|
|
|
|
|
2023-05-03 07:04:31 +00:00
|
|
|
@property
|
|
|
|
def sub_collections(self):
|
|
|
|
return [
|
|
|
|
sub_collection
|
|
|
|
for sub_collection in self.children
|
|
|
|
if not sub_collection.is_deleted
|
|
|
|
]
|