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

70 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 00:02 +0000

1from typing import Optional 1abcd

2 

3from fastapi import FastAPI, HTTPException, Query 1abcd

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

5 

6 

7class HeroBase(SQLModel): 1abcd

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

9 secret_name: str 1abcd

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

11 

12 

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

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

15 hashed_password: str = Field() 1abcd

16 

17 

18class HeroCreate(HeroBase): 1abcd

19 password: str 1abcd

20 

21 

22class HeroPublic(HeroBase): 1abcd

23 id: int 1abcd

24 

25 

26class HeroUpdate(SQLModel): 1abcd

27 name: Optional[str] = None 1abcd

28 secret_name: Optional[str] = None 1abcd

29 age: Optional[int] = None 1abcd

30 password: Optional[str] = None 1abcd

31 

32 

33sqlite_file_name = "database.db" 1abcd

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

35 

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

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

38 

39 

40def create_db_and_tables(): 1abcd

41 SQLModel.metadata.create_all(engine) 1abcd

42 

43 

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

45 # Use something like passlib here 

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

47 

48 

49app = FastAPI() 1abcd

50 

51 

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

53def on_startup(): 1abcd

54 create_db_and_tables() 1abcd

55 

56 

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

58def create_hero(hero: HeroCreate): 1abcd

59 hashed_password = hash_password(hero.password) 1abcd

60 with Session(engine) as session: 1abcd

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

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

63 session.add(db_hero) 1abcd

64 session.commit() 1abcd

65 session.refresh(db_hero) 1abcd

66 return db_hero 1abcd

67 

68 

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

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

71 with Session(engine) as session: 1abcd

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

73 return heroes 1abcd

74 

75 

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

77def read_hero(hero_id: int): 1abcd

78 with Session(engine) as session: 1abcd

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

80 if not hero: 1abcd

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

82 return hero 1abcd

83 

84 

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

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

87 with Session(engine) as session: 1abcd

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

89 if not db_hero: 1abcd

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

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

92 extra_data = {} 1abcd

93 if "password" in hero_data: 1abcd

94 password = hero_data["password"] 1abcd

95 hashed_password = hash_password(password) 1abcd

96 extra_data["hashed_password"] = hashed_password 1abcd

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

98 session.add(db_hero) 1abcd

99 session.commit() 1abcd

100 session.refresh(db_hero) 1abcd

101 return db_hero 1abcd