2023-06-06 11:03:20 +00:00
|
|
|
from sqlalchemy import func, text, and_
|
2023-05-18 13:35:50 +00:00
|
|
|
|
2023-04-21 12:58:47 +00:00
|
|
|
from app import db
|
2023-06-06 11:03:20 +00:00
|
|
|
from .collection import Collection
|
2023-04-21 12:58:47 +00:00
|
|
|
from app.models.utils import BaseModel
|
2023-05-12 06:55:10 +00:00
|
|
|
from app.controllers import create_breadcrumbs
|
2023-05-18 13:01:33 +00:00
|
|
|
from .interpretation import Interpretation
|
|
|
|
from .comment import Comment
|
2023-05-18 13:35:50 +00:00
|
|
|
from .interpretation_vote import InterpretationVote
|
2023-06-06 15:04:10 +00:00
|
|
|
from app.controllers.next_prev_section import recursive_move_down, recursive_move_up
|
2023-04-21 12:58:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
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 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-06-01 14:01:45 +00:00
|
|
|
position = db.Column(db.Integer, default=-1, 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)
|
2023-05-11 15:02:14 +00:00
|
|
|
interpretations = db.relationship(
|
|
|
|
"Interpretation", viewonly=True, order_by="desc(Interpretation.id)"
|
|
|
|
)
|
2023-05-24 14:06:33 +00:00
|
|
|
access_groups = db.relationship(
|
|
|
|
"AccessGroup",
|
|
|
|
secondary="sections_access_groups",
|
|
|
|
) # access_groups related to current entity
|
2023-05-17 15:39:37 +00:00
|
|
|
tags = db.relationship(
|
|
|
|
"Tag",
|
|
|
|
secondary="section_tags",
|
|
|
|
back_populates="sections",
|
|
|
|
)
|
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-05-12 06:55:10 +00:00
|
|
|
@property
|
|
|
|
def breadcrumbs_path(self):
|
2023-05-30 13:00:13 +00:00
|
|
|
breadcrumbs_path = create_breadcrumbs(
|
|
|
|
book_id=self.book_id, collection_id=self.collection.id
|
|
|
|
)
|
2023-05-12 06:55:10 +00:00
|
|
|
return breadcrumbs_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-05-18 13:01:33 +00:00
|
|
|
@property
|
|
|
|
def approved_interpretation(self):
|
|
|
|
interpretation = Interpretation.query.filter_by(
|
2023-05-18 14:50:07 +00:00
|
|
|
approved=True, section_id=self.id, is_deleted=False
|
2023-05-18 13:01:33 +00:00
|
|
|
).first()
|
|
|
|
|
2023-05-18 13:35:50 +00:00
|
|
|
if interpretation:
|
|
|
|
return interpretation
|
|
|
|
|
|
|
|
# most upvoted
|
|
|
|
result = (
|
|
|
|
db.session.query(
|
|
|
|
Interpretation, func.count(Interpretation.votes).label("total_votes")
|
|
|
|
)
|
|
|
|
.join(InterpretationVote)
|
2023-05-18 14:50:07 +00:00
|
|
|
.filter(
|
|
|
|
Interpretation.section_id == self.id, Interpretation.is_deleted is False
|
|
|
|
)
|
2023-05-18 13:35:50 +00:00
|
|
|
.group_by(Interpretation.id)
|
|
|
|
.order_by(text("total_votes DESC"))
|
|
|
|
).first()
|
|
|
|
if result:
|
|
|
|
return result[0]
|
|
|
|
|
|
|
|
# oldest
|
|
|
|
interpretation = (
|
2023-05-18 14:50:07 +00:00
|
|
|
Interpretation.query.filter_by(section_id=self.id, is_deleted=False)
|
2023-05-18 13:35:50 +00:00
|
|
|
.order_by(Interpretation.created_at)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
|
|
|
|
if interpretation:
|
|
|
|
return interpretation
|
2023-05-18 13:01:33 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def approved_comments(self):
|
|
|
|
interpretation_ids = [
|
|
|
|
interpretation.id for interpretation in self.interpretations
|
|
|
|
]
|
|
|
|
comments = (
|
|
|
|
Comment.query.filter_by(approved=True)
|
|
|
|
.filter(Comment.interpretation_id.in_(interpretation_ids))
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
|
|
|
|
return comments
|
|
|
|
|
2023-06-06 11:03:20 +00:00
|
|
|
@property
|
|
|
|
def next_section(self):
|
2023-06-06 15:04:10 +00:00
|
|
|
section = (
|
|
|
|
Section.query.filter(
|
|
|
|
Section.collection_id == self.collection_id,
|
|
|
|
Section.position > self.position,
|
|
|
|
)
|
|
|
|
.order_by(Section.position)
|
|
|
|
.first()
|
|
|
|
)
|
2023-06-06 14:17:43 +00:00
|
|
|
if section:
|
|
|
|
return section
|
|
|
|
|
|
|
|
section = recursive_move_down(self.collection)
|
|
|
|
return section
|
2023-06-06 11:03:20 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def previous_section(self):
|
2023-06-06 15:04:10 +00:00
|
|
|
section = (
|
|
|
|
Section.query.filter(
|
|
|
|
Section.collection_id == self.collection_id,
|
|
|
|
Section.position < self.position,
|
|
|
|
)
|
|
|
|
.order_by(Section.position.desc())
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if section:
|
|
|
|
return section
|
|
|
|
|
|
|
|
section = recursive_move_up(self.collection)
|
|
|
|
return section
|
2023-06-06 11:03:20 +00:00
|
|
|
|
2023-04-21 12:58:47 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<{self.id}: {self.label}>"
|