Coverage for docs_src/tutorial/fastapi/update/tutorial002.py: 100%
70 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from typing import List, Optional 1fabcde
3from fastapi import FastAPI, HTTPException, Query 1fabcde
4from sqlmodel import Field, Session, SQLModel, create_engine, select 1fabcde
7class HeroBase(SQLModel): 1fabcde
8 name: str = Field(index=True) 1fabcde
9 secret_name: str 1fabcde
10 age: Optional[int] = Field(default=None, index=True) 1fabcde
13class Hero(HeroBase, table=True): 1fabcde
14 id: Optional[int] = Field(default=None, primary_key=True) 1fabcde
15 hashed_password: str = Field() 1fabcde
18class HeroCreate(HeroBase): 1fabcde
19 password: str 1fabcde
22class HeroPublic(HeroBase): 1fabcde
23 id: int 1fabcde
26class HeroUpdate(SQLModel): 1fabcde
27 name: Optional[str] = None 1fabcde
28 secret_name: Optional[str] = None 1fabcde
29 age: Optional[int] = None 1fabcde
30 password: Optional[str] = None 1fabcde
33sqlite_file_name = "database.db" 1fabcde
34sqlite_url = f"sqlite:///{sqlite_file_name}" 1fabcde
36connect_args = {"check_same_thread": False} 1fabcde
37engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1fabcde
40def create_db_and_tables(): 1fabcde
41 SQLModel.metadata.create_all(engine) 1fabcde
44def hash_password(password: str) -> str: 1fabcde
45 # Use something like passlib here
46 return f"not really hashed {password} hehehe" 1fabcde
49app = FastAPI() 1fabcde
52@app.on_event("startup") 1fabcde
53def on_startup(): 1abcde
54 create_db_and_tables() 1fabcde
57@app.post("/heroes/", response_model=HeroPublic) 1fabcde
58def create_hero(hero: HeroCreate): 1fabcde
59 hashed_password = hash_password(hero.password) 1fabcde
60 with Session(engine) as session: 1fabcde
61 extra_data = {"hashed_password": hashed_password} 1fabcde
62 db_hero = Hero.model_validate(hero, update=extra_data) 1fabcde
63 session.add(db_hero) 1fabcde
64 session.commit() 1fabcde
65 session.refresh(db_hero) 1fabcde
66 return db_hero 1fabcde
69@app.get("/heroes/", response_model=List[HeroPublic]) 1fabcde
70def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): 1fabcde
71 with Session(engine) as session: 1fabcde
72 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1fabcde
73 return heroes 1fabcde
76@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1fabcde
77def read_hero(hero_id: int): 1fabcde
78 with Session(engine) as session: 1fabcde
79 hero = session.get(Hero, hero_id) 1fabcde
80 if not hero: 1fabcde
81 raise HTTPException(status_code=404, detail="Hero not found") 1fabcde
82 return hero 1fabcde
85@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1fabcde
86def update_hero(hero_id: int, hero: HeroUpdate): 1fabcde
87 with Session(engine) as session: 1fabcde
88 db_hero = session.get(Hero, hero_id) 1fabcde
89 if not db_hero: 1fabcde
90 raise HTTPException(status_code=404, detail="Hero not found") 1fabcde
91 hero_data = hero.model_dump(exclude_unset=True) 1fabcde
92 extra_data = {} 1fabcde
93 if "password" in hero_data: 1fabcde
94 password = hero_data["password"] 1fabcde
95 hashed_password = hash_password(password) 1fabcde
96 extra_data["hashed_password"] = hashed_password 1fabcde
97 db_hero.sqlmodel_update(hero_data, update=extra_data) 1fabcde
98 session.add(db_hero) 1fabcde
99 session.commit() 1fabcde
100 session.refresh(db_hero) 1fabcde
101 return db_hero 1fabcde