-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
133 lines (103 loc) · 4.63 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# models.py
# db 테이블을 구성하는 파일
from sqlite3 import Timestamp
from sqlalchemy import Column, Integer, String, ForeignKey, TIMESTAMP, Boolean, MetaData
from sqlalchemy.orm import relationship
from database import Base
import schemas, crud
metadata = MetaData()
word_limit = {"User_name_limit": 30, "Question_content_limit": 40, "Comment_content_limit": 100,
"Vote_content_limit":20, "Vote_option_limit": 10, "Min_option_count": 2, "Max_option_count": 4}
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, autoincrement=True)
insta_id = Column(String(word_limit["User_name_limit"]), unique=True) # 인스타 에서 관리하는 숫자형태의 id, user 구분을 위해 사용
username = Column(String(word_limit["User_name_limit"])) # 인스타 id 길이 제한이 30자 라서 수정
full_name = Column(String(word_limit["User_name_limit"]))
follower = Column(Integer)
following = Column(Integer)
profile_image_url = Column(String(500)) # 제 url 길이가 440자라서 여유있게 했습니다
is_deleted = Column(Boolean)
created_at = Column(TIMESTAMP)
updated_at = Column(TIMESTAMP)
def __init__(self, user: schemas.UserCreate):
self.insta_id = user.insta_id
self.username = user.username
self.full_name = user.full_name
self.follower = user.follower
self.following = user.following
self.profile_image_url = user.profile_image_url
self.is_deleted = False
self.created_at = Timestamp.now()
self.updated_at = self.created_at
# user - question 1:N 관계 설정
user_question = relationship("Question")
class Question(Base):
__tablename__ = "question"
id = Column(Integer, primary_key=True, autoincrement=True)
content = Column(String(word_limit["Question_content_limit"]))
user_id = Column(Integer, ForeignKey("user.id"))
type = Column(String(20))
comment_type = Column(String(20))
expired = Column(Boolean, default=False)
is_deleted = Column(Boolean, default=False)
created_at = Column(TIMESTAMP, default=Timestamp.now())
updated_at = Column(TIMESTAMP, default=Timestamp.now())
question_comment = relationship("Comment")
question_vote_option = relationship("VoteOption")
def __init__(self, question: schemas.VoteCreate, is_vote: bool):
#공통
self.content = question.content
self.user_id = question.user_id
self.expired = False
self.is_deleted = False
self.created_at = Timestamp.now()
self.updated_at = self.created_at
if is_vote:
self.type = crud.QuestionType.vote
self.comment_type = crud.CommentType.vote
else:
self.type = question.type
self.comment_type = question.comment_type
class Comment(Base):
__tablename__ = "comment"
id = Column(Integer, primary_key=True, autoincrement=True)
type = Column(String(20))
content = Column(String(word_limit["Comment_content_limit"]))
question_id = Column(Integer, ForeignKey("question.id"))
created_at = Column(TIMESTAMP, default=Timestamp.now())
updated_at = Column(TIMESTAMP, default=Timestamp.now())
def __init__(self, content: str, type: str, question_id: int):
self.content = content
self.type = type
self.question_id = question_id
self.created_at = Timestamp.now()
self.updated_at = self.created_at
class VoteOption(Base):
__tablename__ = "vote_option"
id = Column(Integer, primary_key=True, autoincrement=True)
num = Column(Integer)
content = Column(String(word_limit["Vote_option_limit"]))
count = Column(Integer)
question_id = Column(Integer, ForeignKey("question.id"))
created_at = Column(TIMESTAMP, default=Timestamp.now())
updated_at = Column(TIMESTAMP, default=Timestamp.now())
def __init__(self, num: int, content: str, question_id: int):
self.num = num
self.content = content
self.count = 0
self.question_id = question_id
self.created_at = Timestamp.now()
self.updated_at = self.created_at
class RandomQuestion(Base):
__tablename__ = "random_question"
id = Column(Integer, primary_key=True, autoincrement=True)
content = Column(String(word_limit["Question_content_limit"]))
type = Column(String(20))
created_at = Column(TIMESTAMP, default=Timestamp.now())
updated_at = Column(TIMESTAMP, default=Timestamp.now())
def __init__(self, content: str, type: str):
self.content = content
self.type = type
self.created_at = Timestamp.now()
self.updated_at = self.created_at