forked from xiaopeng163/fastapi-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.py
55 lines (37 loc) · 1.24 KB
/
schema.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
from sqlmodel import SQLModel, Field, Column, VARCHAR
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"])
class BookInput(SQLModel):
name: str
isbn: str
type_: str
publish: str
price: float
class Config:
json_schema_extra = {
"example": {
"name": "The Book Name",
"isbn": "abcd-1234",
"type_": "Fiction",
"publish": "2023-10-20",
"price": 120,
}
}
class Book(BookInput, table=True):
id_: int | None = Field(primary_key=True, default=None)
class AuthorInput(SQLModel):
name: str
nationality: str
class Author(AuthorInput, table=True):
id_: int | None = Field(primary_key=True, default=None)
class User(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
username: str = Field(sa_column=Column('username', VARCHAR, unique=True, index=True))
password_hash: str = ''
def set_password(self, password):
self.password_hash = pwd_context.hash(password)
def verify_password(self, password):
return pwd_context.verify(password, self.password_hash)
class UserOutput(SQLModel):
id: int
username: str