class Post(Model): """ Post Model for storing post related details """ # Basic details id = Column(db.Integer, primary_key=True) public_id = Column(db.String(15)) owner_id = Column(db.Integer, db.ForeignKey("user.id")) creator_public_id = Column(db.String(15)) # Post content and details content = Column(db.Text) image_file = Column(db.String(40), default=None, nullable=True) status = Column(db.String(10)) created = Column(db.DateTime, default=datetime.utcnow) edited = Column(db.Boolean, default=False) likes = db.relationship("PostLike", backref="post", cascade="all, delete-orphan") comments = db.relationship("Comment", backref="post", cascade="all, delete-orphan") def __repr__(self): return f"<Post '{self.id}'>"
class Comment(Model): """ Comment Model for storing comment related details """ # Basic details id = Column(db.Integer, primary_key=True) public_id = Column(db.String(15)) creator_public_id = Column(db.String(15)) on_post = Column(db.Integer, db.ForeignKey("post.id")) # Comment content and details content = Column(db.Text) image_file = Column(db.String(40), default=None, nullable=True) created = Column(db.DateTime, default=datetime.utcnow) edited = Column(db.Boolean, default=False) likes = db.relationship("CommentLike", backref="comment", cascade="all, delete-orphan") replies = db.relationship("Reply", backref="comment", cascade="all, delete-orphan") def __repr__(self): return f"<Comment '{self.id}'>"
class User(Model): """ User Model for storing user related details """ # Basic details id = Column(db.Integer, primary_key=True) public_id = Column(db.String(15), unique=True) email = Column(db.String(255), unique=True, nullable=False) username = Column(db.String(15), unique=True) full_name = Column(db.String(50), nullable=True) password_hash = Column(db.String(255)) bio = Column(db.String(150), nullable=True) # Media profile_picture = Column(db.String(40), nullable=True) background_cover = Column(db.String(40), nullable=True) # Relationships notifications = db.relationship("Notification", backref="user") posts = db.relationship("Post", backref="user") # Add Favorites (post) post_likes = db.relationship("PostLike", backref="user") comment_likes = db.relationship("CommentLike", backref="user") reply_likes = db.relationship("ReplyLike", backref="user") # Status joined_date = Column(db.DateTime) roles = db.relationship("Role", secondary=roles_users, backref=db.backref("users"), lazy="dynamic") @property def password(self): raise AttributeError("Password: Write-Only field") @password.setter def password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode( "utf-8") def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) def __repr__(self): return f"<User '{self.username}'>"