2023-05-12 17:26:04 +03:00
|
|
|
from flask_login import current_user
|
|
|
|
|
|
|
|
from app import db, models as m
|
2023-04-21 15:58:47 +03:00
|
|
|
from app.models.utils import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
class Book(BaseModel):
|
|
|
|
__tablename__ = "books"
|
|
|
|
|
2023-04-27 16:17:25 +03:00
|
|
|
label = db.Column(db.String(256), unique=False, nullable=False)
|
2023-04-24 17:59:50 +03:00
|
|
|
about = db.Column(db.Text, unique=False, nullable=True)
|
2023-04-21 15:58:47 +03:00
|
|
|
|
|
|
|
# Foreign keys
|
|
|
|
user_id = db.Column(db.ForeignKey("users.id"))
|
|
|
|
|
|
|
|
# Relationships
|
|
|
|
owner = db.relationship("User", viewonly=True)
|
|
|
|
stars = db.relationship("User", secondary="books_stars", back_populates="stars")
|
|
|
|
contributors = db.relationship("BookContributor")
|
|
|
|
versions = db.relationship("BookVersion")
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<{self.id}: {self.label}>"
|
2023-05-02 17:07:37 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def last_version(self):
|
|
|
|
return self.versions[-1]
|
2023-05-12 17:26:04 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def current_user_has_star(self):
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
book_star: m.BookStar = m.BookStar.query.filter_by(
|
|
|
|
user_id=current_user.id, book_id=self.id
|
|
|
|
).first()
|
|
|
|
if book_star:
|
|
|
|
return True
|