-
Notifications
You must be signed in to change notification settings - Fork 48
/
schemas.py
66 lines (46 loc) · 1.63 KB
/
schemas.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
from sqlmodel import SQLModel, Field, Relationship, Column, VARCHAR
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"])
class UserOutput(SQLModel):
id: int
username: str
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):
"""Setting the passwords actually sets password_hash."""
self.password_hash = pwd_context.hash(password)
def verify_password(self, password):
"""Verify given password by hashing and comparing to password_hash."""
return pwd_context.verify(password, self.password_hash)
class TripInput(SQLModel):
start: int
end: int
description: str
class TripOutput(TripInput):
id: int
class Trip(TripInput, table=True):
id: int | None = Field(default=None, primary_key=True)
car_id: int = Field(foreign_key="car.id")
car: "Car" = Relationship(back_populates="trips")
class CarInput(SQLModel):
size: str
fuel: str | None = "electric"
doors: int
transmission: str | None = "auto"
class Config:
schema_extra = {
"example": {
"size": "m",
"doors": 5,
"transmission": "manual",
"fuel": "hybrid"
}
}
class Car(CarInput, table=True):
id: int | None = Field(primary_key=True, default=None)
trips: list[Trip] = Relationship(back_populates="car")
class CarOutput(CarInput):
id: int
trips: list[TripOutput] = []