class Message(db.Model): __tablename__ = "messages" id = db.Column(db.Integer, primary_key=True) sender_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) recipient_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) body = db.Column(db.String(255), nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) sender = db.relationship("User", foreign_keys=[sender_id], back_populates="sent_messages") recipient = db.relationship("User", foreign_keys=[recipient_id], back_populates="received_messages") def to_dict(self): return { "id": self.id, "sender": MinimalUserResponse.from_orm(self.sender).dict(), "recipient": MinimalUserResponse.from_orm(self.recipient).dict(), "created_at": self.created_at, }
class PostRating(db.Model): __tablename__ = "post_ratings" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), nullable=False) rating = db.Column(db.Integer, nullable=False) created_at = db.Column( db.DateTime, nullable=False, default=datetime.datetime.utcnow ) updated_at = db.Column( db.DateTime, nullable=False, default=datetime.datetime.utcnow ) user = db.relationship("User") post = db.relationship("Post", back_populates="ratings") def to_simple_dict(self): return { "user_id": self.user_id, "rating": self.rating, } def to_dict(self): return { "id": self.id, "user": MinimalUserResponse.from_orm(self.user).dict(), "post": self.post.to_simple_dict(), "rating": self.rating, }
class Retailer(db.Model): __tablename__ = "retailers" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) name = db.Column(db.String(50), nullable=False) description = db.Column(db.Text, nullable=False) city = db.Column(db.String(50), nullable=False) state = db.Column(db.String(25), nullable=False) lat = db.Column(db.Numeric(scale=7)) lng = db.Column(db.Numeric(scale=7)) created_at = db.Column( db.DateTime, nullable=False, default=datetime.datetime.utcnow ) updated_at = db.Column( db.DateTime, nullable=False, default=datetime.datetime.utcnow ) user = db.relationship("User", back_populates="retailers") images = db.relationship("RetailerImage", back_populates="retailer") ratings = db.relationship("RetailerRating", back_populates="retailer") def to_dict(self): return { "id": self.id, "owner": MinimalUserResponse.from_orm(self.user).dict(), "name": self.name, "description": self.description, "city": self.city, "state": self.state, "lat": float(self.lat or 0), "lng": float(self.lng or 0), "created_at": self.created_at, "images": [image.image_url for image in self.images], "ratings": {rating.user_id: rating.to_dict() for rating in self.ratings}, } def to_simple_dict(self): return { "id": self.id, "owner": MinimalUserResponse.from_orm(self.user).dict(), "name": self.name, "description": self.description, "city": self.city, "state": self.state, "created_at": self.created_at, "ratings": [rating.to_dict() for rating in self.ratings], }
class Meetup(db.Model): __tablename__ = "meetups" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) name = db.Column(db.String(50), nullable=False) description = db.Column(db.Text, nullable=False) city = db.Column(db.String(50), nullable=False) state = db.Column(db.String(25), nullable=False) lat = db.Column(db.Numeric(scale=7)) lng = db.Column(db.Numeric(scale=7)) date = db.Column(db.DateTime, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) user = db.relationship("User", back_populates="meetups") def to_dict(self): return { "id": self.id, "user": MinimalUserResponse.from_orm(self.user).dict(), "name": self.name, "description": self.description, "city": self.city, "state": self.state, "lat": float(self.lat or 0), "lng": float(self.lng or 0), "date": self.date, "created_at": self.created_at, "updated_at": self.updated_at, }
class Community(db.Model): __tablename__ = "communities" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) description = db.Column(db.Text, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) posts = db.relationship("Post", back_populates="community") def to_simple_dict(self): return { "id": self.id, "name": self.name, } def to_dict(self): return { "id": self.id, "name": self.name, "description": self.description, "created_at": self.created_at, }
class Thread(db.Model): __tablename__ = "threads" id = db.Column(db.Integer, primary_key=True) post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), nullable=False) comments = db.relationship("Comment", back_populates="thread") post = db.relationship("Post", back_populates="threads") def to_dict(self): return { "id": self.id, "comments": { comment.id: comment.to_simple_dict() for comment in self.comments }, "post_id": self.post_id, }
class PostsImage(db.Model): __tablename__ = "posts_images" id = db.Column(db.Integer, primary_key=True) post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), nullable=False) image_url = db.Column(db.String, nullable=False) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) post = db.relationship("Post", back_populates="images") def to_dict(self): return { "id": self.id, "post_id": self.post_id, "image_url": self.image_url, "created_at": self.created_at, "updated_at": self.updated_at, "post": self.post.to_dict(), }
class RetailerImage(db.Model): __tablename__ = "retailer_images" id = db.Column(db.Integer, primary_key=True) retailer_id = db.Column(db.Integer, db.ForeignKey("retailers.id"), nullable=False) image_url = db.Column(db.String, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) retailer = db.relationship("Retailer", back_populates="images") def to_dict(self): return { "id": self.id, "retailer_id": self.retailer_id, "image_url": self.image_url, "created_at": self.created_at, }
class Comment(db.Model): __tablename__ = "comments" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) thread_id = db.Column(db.Integer, db.ForeignKey("threads.id"), nullable=False) comment_id = db.Column(db.Integer, db.ForeignKey("comments.id"), nullable=True) path = db.Column(db.String(255), nullable=False) level = db.Column(db.Integer, nullable=False) body = db.Column(db.Text, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) thread = db.relationship("Thread", back_populates="comments") user = db.relationship("User", back_populates="comments") children = db.relationship("Comment", backref=db.backref("parent", remote_side=[id])) ratings = db.relationship("CommentRating", back_populates="comment") def to_simple_dict(self): return { "id": self.id, "body": self.body, "user": MinimalUserResponse.from_orm(self.user).dict(), "comment_id": self.comment_id, } def to_search_dict(self): return { "id": self.id, "body": self.body, "user": MinimalUserResponse.from_orm(self.user).dict(), "post": self.thread.post.to_search_dict(), "ratings": {rating.user_id: rating.to_dict() for rating in self.ratings}, "created_at": self.created_at, } def to_dict(self): return { "id": self.id, "user": MinimalUserResponse.from_orm(self.user).dict(), "body": self.body, "thread_id": self.thread_id, "path": self.path, "level": self.level, "created_at": self.created_at, "updated_at": self.updated_at, "children": [child.to_simple_dict() for child in self.children], "ratings": {rating.user_id: rating.to_dict() for rating in self.ratings}, "post": self.thread.post.to_search_dict(), }