-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
191 lines (138 loc) · 4.51 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from typing import Optional, List
from uuid import UUID
from datetime import datetime
from sqlmodel import SQLModel, Field, Relationship
class UserBase(SQLModel):
email: str = Field(nullable=False, max_length=255)
username: Optional[str] = None
class UserSignup(UserBase):
password: str = Field(default=None)
# table = True => in database
class User(UserBase, table=True):
__tablename__ = 'users'
id: UUID = Field(primary_key=True, nullable=False)
created_at: datetime = Field(default=datetime.now())
updated_at: datetime = Field(default=datetime.now())
hashed_password: str = None
password_version: int = Field(default=0)
is_active: bool = Field(default=True)
role: int = Field(default=0)
projects: List["Project"] = Relationship(
back_populates="user", )
class ProjectCount(SQLModel):
live_count: Optional[int] = None
draft_count: Optional[int] = None
total_count: Optional[int] = None
class UserInfo(UserBase):
id: UUID
created_at: datetime
email: Optional[str] = None
username: Optional[str] = None
is_active: Optional[bool] = None
role: Optional[int] = None
projects_counts: Optional[ProjectCount] = None
class UserInfoInProject(SQLModel):
username: Optional[str] = None
class UserUpdate(UserBase):
email: Optional[str] = None
password: Optional[str] = None
role: Optional[int] = None
class Token(SQLModel):
access_token: str
token_type: str
email: str
role: int
expires_at: datetime
class CategoryCreate(SQLModel):
categoryName: str
class CategoryBase(SQLModel):
categoryId: int = Field(primary_key=True, nullable=False)
categoryName: str
class CategoryUpdate(CategoryBase):
CategoryId: Optional[int] = None
categoryName: Optional[str] = None
class Category(CategoryBase, table=True):
__tablename__ = 'categories'
tags: List["Tag"] = Relationship(
back_populates="category")
class CategoryWithTags(CategoryBase):
tags: List["Tag"] = []
class project_tags(SQLModel, table=True):
project_id: UUID = Field(foreign_key="projects.id",
primary_key=True,
nullable=False)
tag_id: int = Field(foreign_key="tags.tagId",
primary_key=True,
nullable=False)
class TagCreate(SQLModel):
tagName: str
tagNameShort: Optional[str] = None
categoryId: int
class TagBase(SQLModel):
tagId: int
tagName: str
tagNameShort: str
categoryId: int
class TagUpdate(TagBase):
tagId: Optional[int] = None
tagName: Optional[str] = None
tagNameShort: Optional[str] = None
categoryId: Optional[int] = None
class Tag(TagBase, table=True):
__tablename__ = 'tags'
tagId: int = Field(primary_key=True, nullable=False)
categoryId: int = Field(default=None, foreign_key="categories.categoryId")
projects: List["Project"] = Relationship(
back_populates="tags",
link_model=project_tags)
category: Category = Relationship(
back_populates="tags")
class TagCount(SQLModel):
tag: Optional[Tag]
count: int
class ProjectBase(SQLModel):
title: str
link: str
description: str
content: str
tags: List[int] # tagId
class ProjectUpdate(SQLModel):
title: Optional[str]
link: Optional[str]
description: Optional[str]
content: Optional[str]
date: Optional[datetime]
tags: Optional[List[int]]
is_live: Optional[bool]
class Project(ProjectBase, table=True):
__tablename__ = 'projects'
id: UUID = Field(primary_key=True, nullable=False)
date: datetime = Field(default_factory=datetime.utcnow, nullable=False)
is_live: bool = Field(default=False)
is_featured: bool = Field(default=False)
visit_count: int = Field(default=0)
user_id: UUID = Field(foreign_key="users.id")
user: User = Relationship(back_populates="projects"
)
tags: List["Tag"] = Relationship(
back_populates="projects",
link_model=project_tags)
class ProjectWithUserAndTags(SQLModel):
id: UUID
title: str
link: str
description: str
is_live: bool
is_featured: bool
visit_count: int
date: datetime
user: Optional[UserInfoInProject] = None
tags: List["Tag"] = []
class ProjectFull(ProjectWithUserAndTags):
content: str
class ProjectFeatured(ProjectBase):
id: UUID
tags: List["Tag"] = []
date: datetime
ProjectWithUserAndTags.update_forward_refs()
CategoryWithTags.update_forward_refs()