open-law/app/models/interpretation.py

58 lines
1.5 KiB
Python
Raw Normal View History

2023-05-10 12:07:07 +00:00
from flask_login import current_user
2023-05-11 15:02:14 +00:00
from app import db, models as m
2023-04-21 12:58:47 +00:00
from app.models.utils import BaseModel
class Interpretation(BaseModel):
__tablename__ = "interpretations"
2023-04-21 14:01:07 +00:00
text = db.Column(db.Text, unique=False, nullable=False)
approved = db.Column(db.Boolean, default=False)
2023-04-21 12:58:47 +00:00
marked = db.Column(db.Boolean, default=False)
# Foreign keys
user_id = db.Column(db.ForeignKey("users.id"))
section_id = db.Column(db.ForeignKey("sections.id"))
# Relationships
user = db.relationship("User")
section = db.relationship("Section")
2023-05-11 15:02:14 +00:00
comments = db.relationship("Comment", viewonly=True, order_by="desc(Comment.id)")
2023-04-21 12:58:47 +00:00
votes = db.relationship("InterpretationVote", viewonly=True)
tags = db.relationship(
"Tag",
secondary="interpretation_tags",
back_populates="interpretations",
)
2023-05-09 14:56:29 +00:00
@property
def vote_count(self):
count = 0
for vote in self.votes:
if vote.positive:
count += 1
2023-05-10 12:38:04 +00:00
continue
count -= 1
2023-05-09 14:56:29 +00:00
return count
2023-05-10 12:07:07 +00:00
@property
def current_user_vote(self):
for vote in self.votes:
if vote.user_id == current_user.id:
return vote.positive
return None
2023-05-09 09:52:37 +00:00
@property
def active_comments(self):
return [comment for comment in self.comments if not comment.is_deleted]
2023-05-11 15:02:14 +00:00
@property
def book(self) -> m.Book:
return self.section.version.book
2023-04-21 12:58:47 +00:00
def __repr__(self):
2023-05-18 13:03:31 +00:00
return f"<Interpretation: {self.id}>"