class Post(db.Model): """Model that represents a post.""" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255), nullable=False) post = db.Column(db.Text, nullable=False) date_created = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey("app_user.id"), nullable=False) community_id = db.Column(db.Integer, db.ForeignKey("community.id"), nullable=False) replies = db.relationship("Reply", backref="post", lazy="dynamic", cascade="all, delete-orphan") post_votes = db.relationship("PostVote", backref="post", lazy="dynamic", cascade="all, delete-orphan") def __repr__(self): return f"<Post (id='{self.id}', title='{self.title}', post='{self.post}', date_created='{self.date_created}')>"
class ReplyVote(db.Model): """Model that tracks a user's vote on a reply.""" id = db.Column(db.Integer, primary_key=True) vote = db.Column(db.Integer, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("app_user.id"), nullable=False) reply_id = db.Column(db.Integer, db.ForeignKey("reply.id"), nullable=False) def __repr__(self): return f"<ReplyVote (id='{self.id}', vote='{self.vote}')>"
class CommunityMember(db.Model): """Model that tracks a user's community membership.""" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("app_user.id"), nullable=False) community_id = db.Column(db.Integer, db.ForeignKey("community.id"), nullable=False) def __repr__(self): return f"<CommunityMember (id='{self.id}')>"
class Community(db.Model): """Model that represents a community.""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), unique=True, nullable=False) description = db.Column(db.Text, nullable=False) date_created = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey("app_user.id"), nullable=False) posts = db.relationship("Post", backref="community", lazy="dynamic", cascade="all, delete-orphan") community_members = db.relationship( "CommunityMember", backref="community", lazy="dynamic", cascade="all, delete-orphan", ) def __repr__(self): return f"<Community (id='{self.id}', name='{self.name}', description='{self.description}', date_created='{self.date_created}')>"
class Reply(db.Model): """Model that represents a reply.""" id = db.Column(db.Integer, primary_key=True) reply = db.Column(db.Text, nullable=False) date_created = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey("app_user.id"), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey("post.id"), nullable=False) reply_votes = db.relationship("ReplyVote", backref="reply", lazy="dynamic", cascade="all, delete-orphan") def __repr__(self): return f"<Reply (id='{self.id}', reply='{self.reply}', date_created='{self.date_created}')>"