Spaces:
Runtime error
Runtime error
from sqlalchemy import Column, Integer, String, ARRAY, DateTime, Boolean, ForeignKey, Text, Enum | |
from sqlalchemy.orm import relationship | |
from datetime import datetime | |
from database import Base | |
from enum import Enum as PyEnum | |
class Like(Base): | |
__tablename__ = 'likes' | |
id = Column(Integer, primary_key=True, index=True) | |
post_id = Column(Integer, ForeignKey('posts.id'), nullable=False) | |
user_id = Column(Integer, ForeignKey('users.id'), nullable=False) | |
post = relationship("Post", back_populates="likes") | |
user = relationship("User", back_populates="likes") | |
class Post(Base): | |
__tablename__ = "posts" | |
id = Column(Integer, primary_key=True, index=True) | |
title = Column(String, nullable=False) | |
content = Column(String, nullable=False) | |
created_at = Column(DateTime, default=datetime.utcnow) | |
user_id = Column(Integer, ForeignKey("users.id")) | |
user = relationship("User", back_populates="posts") | |
requested_at = Column(DateTime, default=datetime.utcnow) | |
likes = relationship("Like", back_populates="post", cascade="all, delete-orphan") | |
comments = relationship('Comment', back_populates='post', cascade='all, delete-orphan') | |
deleted = Column(Boolean, default=False) | |
def user_name(self): | |
return self.user.user_info.name if self.user and self.user.user_info else None | |
def user_nickname(self): | |
return self.user.user_info.nickname if self.user and self.user.user_info else None | |
def user_profile_image(self): | |
return self.user.user_info.profile_image_link if self.user and self.user.user_info else "https://storage.cloud.google.com/mippia-userfile-bucket/userfiles/blank-profile-picture.svg" | |
def to_dict(self): | |
comment_count = 0 | |
for comment in self.comments: | |
if not comment.deleted: | |
comment_count += 1 | |
return { | |
"id": self.id, | |
"content": self.content, | |
"created_at": self.created_at, | |
"nickname": self.user_nickname, # ํ๋กํผํฐ๋ฅผ ํฌํจ | |
"user_name": self.user_name, | |
"title" : self.title, | |
"likes" : len(self.likes), | |
"profile_image" : self.user_profile_image, | |
"comment_count" : comment_count, | |
"user_id" : self.user_id | |
# ๋ค๋ฅธ ํ๋ ์ถ๊ฐ ๊ฐ๋ฅ | |
} | |
def __repr__(self): | |
return f"<Post(id={self.id}, content='{self.content}')>" | |
class Comment(Base): | |
__tablename__ = "comments" | |
id = Column(Integer, primary_key=True, index=True) | |
content = Column(String, nullable=False) | |
created_at = Column(DateTime, default=datetime.utcnow) | |
post_id = Column(Integer, ForeignKey('posts.id')) | |
user_id = Column(Integer, ForeignKey('users.id')) # ๋๊ธ์ ์์ฑํ ์ฌ์ฉ์์ ID๋ฅผ ๋ํ๋ด๋ ์ธ๋ ํค ์ถ๊ฐ | |
parent_id = Column(Integer, ForeignKey('comments.id'), nullable=True) | |
deleted = Column(Boolean, default=False) | |
post = relationship('Post', back_populates='comments') # Comment์ Post ๊ฐ์ ๊ด๊ณ ์ค์ | |
user = relationship('User', back_populates='comments') # Comment์ User ๊ฐ์ ๊ด๊ณ ์ค์ | |
parent = relationship('Comment', remote_side=[id], backref='children') # ๋ถ๋ชจ-์์ ๊ด๊ณ ์ค์ | |
def user_name(self): | |
return self.user.user_info.name if self.user and self.user.user_info else None | |
def user_nickname(self): | |
return self.user.user_info.nickname if self.user and self.user.user_info else None | |
def profile_image(self): | |
return ( | |
self.user.user_info.profile_image_link | |
if self.user and self.user.user_info | |
else "https://storage.cloud.google.com/mippia-userfile-bucket/userfiles/blank-profile-picture.svg" | |
) | |
def __repr__(self): | |
return f"<Comment(id={self.id}, content='{self.content}')>" | |
def to_dict(self): | |
return { | |
"id": self.id, | |
"content": self.content, | |
"created_at": self.created_at, | |
"post_id": self.post_id, # ํ๋กํผํฐ๋ฅผ ํฌํจ | |
"user_id": self.user_id, | |
"parent_id" : self.parent_id, | |
"user_name" : self.user_name, | |
"nickname" : self.user_nickname, | |
"profile_image":self.profile_image, | |
# ๋ค๋ฅธ ํ๋ ์ถ๊ฐ ๊ฐ๋ฅ | |
} | |
class Ask(Base): | |
__tablename__ = "asks" | |
id = Column(Integer, primary_key=True, index=True) | |
title = Column(String, nullable=False) | |
content = Column(String, nullable=False) | |
response = Column(String, nullable=True) | |
responsed = Column(String, nullable=False) #will deprecate | |
is_responsed = Column(Boolean, default = False, nullable = False) | |
is_read = Column(Boolean, default = False, nullable = False) | |
created_at = Column(DateTime, default=datetime.utcnow) | |
user_id = Column(Integer, ForeignKey("users.id"), nullable = True) | |
user_name = Column(String) | |
email = Column(String) | |
ask_type = Column(String) | |
file_path = Column(String) | |
user = relationship("User", back_populates="asks") | |
requested_at = Column(DateTime, default=datetime.utcnow) | |
privacyAgree = Column(Boolean, default=False) | |
deleted = Column(Boolean, default=False) | |
def __repr__(self): | |
return f"<Ask(id={self.id}, title='{self.title}')>" | |
def to_dict(self): | |
return { | |
"id": self.id, | |
"title": self.title, | |
"content": self.content, | |
"created_at": self.created_at, | |
"user_id": self.user_id, | |
"user_name" : self.user_name, | |
"is_responsed" : self.is_responsed, | |
"response" : self.response | |
# ๋ค๋ฅธ ํ๋ ์ถ๊ฐ ๊ฐ๋ฅ | |
} | |
class Notice(Base): | |
__tablename__ = "notice" | |
id = Column(Integer, primary_key=True, index=True) | |
ko_title = Column(String, nullable=False) | |
en_title = Column(String, nullable=False) | |
created_at = Column(DateTime, default=datetime.utcnow) | |
en_content = Column(Text, nullable=False) | |
ko_content = Column(Text, nullable=False) | |
def __repr__(self): | |
return f"<Notice(id={self.id}, ko_content='{self.ko_content}')>, en_content='{self.en_content}'" | |
class Report(Base): | |
__tablename__ = "reports" | |
id = Column(Integer, primary_key=True, index=True) | |
report_type = Column(String, nullable=False) | |
target_id = Column(Integer, nullable=False) # ์ ๊ณ ๋์ type์ ID | |
user_id = Column(Integer, ForeignKey("users.id"), nullable=True) # ์ ๊ณ ํ ์ ์ ์ ID | |
reason = Column(String, nullable=True) | |
response = Column(String, nullable=True) | |
status = Column(String, default="Pending") | |
created_at = Column(DateTime, default=datetime.utcnow) | |
reviewed_at = Column(DateTime, nullable=True) | |
user = relationship("User", back_populates="reports") |