Skip to content

Commit 940d67d

Browse files
committed
Update code
1 parent 941bddd commit 940d67d

File tree

3 files changed

+18
-73
lines changed

3 files changed

+18
-73
lines changed

ch05/planner/models/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Event(BaseModel):
1414
class Config:
1515
schema_extra = {
1616
"example": {
17-
"title": "FastAPI BookLaunch",
17+
"title": "FastAPI Book Launch",
1818
"image": "https://linktomyimage.com/image.png",
1919
"description": "We will be discussing the contents of the FastAPI book in this event.Ensure to come with your own copy to win gifts!",
2020
"tags": ["python", "fastapi", "book", "launch"],

ch05/planner/models/users.py

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
11
from typing import Optional, List
2-
3-
from fastapi import Form
42
from pydantic import BaseModel, EmailStr
53

64
from models.events import Event
75

86

97
class User(BaseModel):
108
email: EmailStr
11-
username: str
9+
password: str
1210
events: Optional[List[Event]]
1311

14-
@classmethod
15-
def as_form(
16-
cls,
17-
email: EmailStr = Form(...),
18-
username: str = Form(...),
19-
password: str = Form(...)
20-
):
21-
return cls(email=email, username=username, password=password)
22-
2312
class Config:
2413
schema_extra = {
2514
"example": {
2615
"email": "fastapi@packt.com",
27-
"username": "fastapipackt001",
16+
"password": "strong!!!",
2817
"events": [],
2918
}
3019
}
3120

3221

33-
class NewUser(User):
22+
class UserSignIn(BaseModel):
23+
email: EmailStr
3424
password: str
3525

36-
class Config:
37-
schema_extra = {
26+
schema_extra = {
3827
"example": {
3928
"email": "fastapi@packt.com",
40-
"password": "Stro0ng!",
41-
"username": "FastPackt"
29+
"password": "strong!!!"
4230
}
43-
}
44-
45-
46-
class UserSignIn(BaseModel):
47-
email: EmailStr
48-
password: str
49-
50-
@classmethod
51-
def as_form(
52-
cls,
53-
email: EmailStr = Form(...),
54-
password: str = Form(...)
55-
):
56-
return cls(email=email, password=password)
57-
58-
31+
}

ch05/planner/routes/users.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
from fastapi import APIRouter, HTTPException, Request, status, Depends
2-
from fastapi.templating import Jinja2Templates
1+
from fastapi import APIRouter, HTTPException, status
32

4-
from models.users import NewUser, UserSignIn
3+
from models.users import User, UserSignIn
54

65
user_router = APIRouter(
76
tags=["User"],
87
)
98

109
users = {}
11-
templates = Jinja2Templates(directory="templates/")
12-
1310

1411
@user_router.post("/signup")
15-
async def sign_user_up(request: Request, data: NewUser = Depends(NewUser.as_form)):
12+
async def sign_user_up( data: User):
1613
if data.email in users:
1714
raise HTTPException(
1815
status_code=status.HTTP_409_CONFLICT,
@@ -21,14 +18,13 @@ async def sign_user_up(request: Request, data: NewUser = Depends(NewUser.as_form
2118

2219
users[data.email] = data
2320

24-
return templates.TemplateResponse("user.html", {
25-
"request": request,
26-
"signed_in": True,
27-
})
21+
return {
22+
"message": "User successfully registered!"
23+
}
2824

2925

3026
@user_router.post("/signin")
31-
async def sign_user_in(request: Request, user: UserSignIn = Depends(UserSignIn.as_form)):
27+
async def sign_user_in(user: UserSignIn):
3228
if user.email not in users:
3329
raise HTTPException(
3430
status_code=status.HTTP_404_NOT_FOUND,
@@ -40,30 +36,6 @@ async def sign_user_in(request: Request, user: UserSignIn = Depends(UserSignIn.a
4036
status_code=status.HTTP_403_FORBIDDEN,
4137
detail="Wrong credential passed"
4238
)
43-
return templates.TemplateResponse(
44-
"index.html",
45-
{
46-
"request": request,
47-
"signed_in": True
48-
}
49-
)
50-
51-
52-
@user_router.get("/")
53-
async def render_login_page(request: Request):
54-
return templates.TemplateResponse(
55-
"user.html", {
56-
"request": request,
57-
"sign_in": True
58-
}
59-
)
60-
61-
62-
@user_router.get("/signup")
63-
async def render_signup_page(request: Request):
64-
return templates.TemplateResponse(
65-
"user.html", {
66-
"request": request,
67-
"sign_in": False
68-
}
69-
)
39+
return {
40+
"message": "User signed in successfully"
41+
}

0 commit comments

Comments
 (0)