Coverage for docs_src/tutorial/fastapi/update/tutorial002.py: 100%

70 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-27 00:03 +0000

1from typing import List, Optional 1abcdefghij

2 

3from fastapi import FastAPI, HTTPException, Query 1abcdefghij

4from sqlmodel import Field, Session, SQLModel, create_engine, select 1abcdefghij

5 

6 

7class HeroBase(SQLModel): 1abcdefghij

8 name: str = Field(index=True) 1abcdefghij

9 secret_name: str 1abcdefghij

10 age: Optional[int] = Field(default=None, index=True) 1abcdefghij

11 

12 

13class Hero(HeroBase, table=True): 1abcdefghij

14 id: Optional[int] = Field(default=None, primary_key=True) 1abcdefghij

15 hashed_password: str = Field() 1abcdefghij

16 

17 

18class HeroCreate(HeroBase): 1abcdefghij

19 password: str 1abcdefghij

20 

21 

22class HeroPublic(HeroBase): 1abcdefghij

23 id: int 1abcdefghij

24 

25 

26class HeroUpdate(SQLModel): 1abcdefghij

27 name: Optional[str] = None 1abcdefghij

28 secret_name: Optional[str] = None 1abcdefghij

29 age: Optional[int] = None 1abcdefghij

30 password: Optional[str] = None 1abcdefghij

31 

32 

33sqlite_file_name = "database.db" 1abcdefghij

34sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdefghij

35 

36connect_args = {"check_same_thread": False} 1abcdefghij

37engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcdefghij

38 

39 

40def create_db_and_tables(): 1abcdefghij

41 SQLModel.metadata.create_all(engine) 1abcdefghij

42 

43 

44def hash_password(password: str) -> str: 1abcdefghij

45 # Use something like passlib here 

46 return f"not really hashed {password} hehehe" 1abcdefghij

47 

48 

49app = FastAPI() 1abcdefghij

50 

51 

52@app.on_event("startup") 1abcdefghij

53def on_startup(): 1abcdefghij

54 create_db_and_tables() 1abcdefghij

55 

56 

57@app.post("/heroes/", response_model=HeroPublic) 1abcdefghij

58def create_hero(hero: HeroCreate): 1abcdefghij

59 hashed_password = hash_password(hero.password) 1abcdefghij

60 with Session(engine) as session: 1abcdefghij

61 extra_data = {"hashed_password": hashed_password} 1abcdefghij

62 db_hero = Hero.model_validate(hero, update=extra_data) 1abcdefghij

63 session.add(db_hero) 1abcdefghij

64 session.commit() 1abcdefghij

65 session.refresh(db_hero) 1abcdefghij

66 return db_hero 1abcdefghij

67 

68 

69@app.get("/heroes/", response_model=List[HeroPublic]) 1abcdefghij

70def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): 1abcdefghij

71 with Session(engine) as session: 1abcdefghij

72 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1abcdefghij

73 return heroes 1abcdefghij

74 

75 

76@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1abcdefghij

77def read_hero(hero_id: int): 1abcdefghij

78 with Session(engine) as session: 1abcdefghij

79 hero = session.get(Hero, hero_id) 1abcdefghij

80 if not hero: 1abcdefghij

81 raise HTTPException(status_code=404, detail="Hero not found") 1abcdefghij

82 return hero 1abcdefghij

83 

84 

85@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abcdefghij

86def update_hero(hero_id: int, hero: HeroUpdate): 1abcdefghij

87 with Session(engine) as session: 1abcdefghij

88 db_hero = session.get(Hero, hero_id) 1abcdefghij

89 if not db_hero: 1abcdefghij

90 raise HTTPException(status_code=404, detail="Hero not found") 1abcdefghij

91 hero_data = hero.model_dump(exclude_unset=True) 1abcdefghij

92 extra_data = {} 1abcdefghij

93 if "password" in hero_data: 1abcdefghij

94 password = hero_data["password"] 1abcdefghij

95 hashed_password = hash_password(password) 1abcdefghij

96 extra_data["hashed_password"] = hashed_password 1abcdefghij

97 db_hero.sqlmodel_update(hero_data, update=extra_data) 1abcdefghij

98 session.add(db_hero) 1abcdefghij

99 session.commit() 1abcdefghij

100 session.refresh(db_hero) 1abcdefghij

101 return db_hero 1abcdefghij