From 87ea9c4b5464cb49fa9f8dfa60b2f4c2e5f93f22 Mon Sep 17 00:00:00 2001 From: samani siewe darix Date: Mon, 2 Oct 2023 20:35:38 +0100 Subject: [PATCH] added support version 2 pydantic --- app.py | 7 +------ config/config.py | 3 +-- models/admin.py | 6 +++--- models/student.py | 2 +- requirements.txt | 49 +++++++++++++++++++++++++++++++++-------------- routes/admin.py | 5 +++-- schemas/admin.py | 22 +++++++++++++++++++++ 7 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 schemas/admin.py diff --git a/app.py b/app.py index b120052..e59b3aa 100644 --- a/app.py +++ b/app.py @@ -21,9 +21,4 @@ async def read_root(): app.include_router(AdminRouter, tags=["Administrator"], prefix="/admin") -app.include_router( - StudentRouter, - tags=["Students"], - prefix="/student", - dependencies=[Depends(token_listener)], -) +app.include_router(StudentRouter,tags=["Students"],prefix="/student",dependencies=[Depends(token_listener)],) diff --git a/config/config.py b/config/config.py index 9283dcd..70075ed 100644 --- a/config/config.py +++ b/config/config.py @@ -2,8 +2,7 @@ from beanie import init_beanie from motor.motor_asyncio import AsyncIOMotorClient -from pydantic import BaseSettings - +from pydantic_settings import BaseSettings import models as models diff --git a/models/admin.py b/models/admin.py index cf46eb7..39eaad7 100644 --- a/models/admin.py +++ b/models/admin.py @@ -9,7 +9,7 @@ class Admin(Document): password: str class Config: - schema_extra = { + json_schema_extra = { "example": { "fullname": "Abdulazeez Abdulazeez Adeshina", "email": "abdul@youngest.dev", @@ -23,7 +23,7 @@ class Settings: class AdminSignIn(HTTPBasicCredentials): class Config: - schema_extra = { + json_schema_extra = { "example": {"username": "abdul@youngest.dev", "password": "3xt3m#"} } @@ -33,7 +33,7 @@ class AdminData(BaseModel): email: EmailStr class Config: - schema_extra = { + json_schema_extra = { "example": { "fullname": "Abdulazeez Abdulazeez Adeshina", "email": "abdul@youngest.dev", diff --git a/models/student.py b/models/student.py index ec156ff..858647c 100644 --- a/models/student.py +++ b/models/student.py @@ -12,7 +12,7 @@ class Student(Document): gpa: float class Config: - schema_extra = { + json_schema_extra = { "example": { "fullname": "Abdulazeez Abdulazeez Adeshina", "email": "abdul@school.com", diff --git a/requirements.txt b/requirements.txt index 01e1e04..17d78f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,35 @@ -motor #3.1.1 -PyJWT -bcrypt # 4.0.1 -uvicorn # 0.21.1 -pymongo # 4.3.3 -fastapi # 0.95.0 -pydantic[email,dotenv] #1.10.7 -passlib # 1.7.4 - -beanie # 1.18.0 -pytest # 7.3.1 -httpx # 0.24.0 -mongomock_motor # 0.0.19 -pytest-mock # 3.10.0 +annotated-types==0.5.0 +anyio==3.7.1 +bcrypt==4.0.1 +beanie==1.22.6 +certifi==2023.7.22 +click==8.1.7 +dnspython==2.4.2 +email-validator==2.0.0.post2 +exceptiongroup==1.1.3 +fastapi==0.103.2 +h11==0.14.0 +httpcore==0.18.0 +httpx==0.25.0 +idna==3.4 +iniconfig==2.0.0 +lazy-model==0.2.0 +mongomock==4.1.2 +mongomock-motor==0.0.21 +motor==3.3.1 +packaging==23.2 +passlib==1.7.4 +pluggy==1.3.0 +pydantic==2.4.2 +pydantic_core==2.10.1 +PyJWT==2.8.0 +pymongo==4.5.0 +pytest==7.4.2 +pytest-mock==3.11.1 +sentinels==1.0.0 +sniffio==1.3.0 +starlette==0.27.0 +toml==0.10.2 +tomli==2.0.1 +typing_extensions==4.8.0 +uvicorn==0.23.2 diff --git a/routes/admin.py b/routes/admin.py index 1ab2b60..1a45bcb 100644 --- a/routes/admin.py +++ b/routes/admin.py @@ -3,7 +3,8 @@ from auth.jwt_handler import sign_jwt from database.database import add_admin -from models.admin import Admin, AdminData, AdminSignIn +from models.admin import Admin +from schemas.admin import AdminData, AdminSignIn router = APIRouter() @@ -23,7 +24,7 @@ async def admin_login(admin_credentials: AdminSignIn = Body(...)): raise HTTPException(status_code=403, detail="Incorrect email or password") -@router.post("/new", response_model=AdminData) +@router.post("", response_model=AdminData) async def admin_signup(admin: Admin = Body(...)): admin_exists = await Admin.find_one(Admin.email == admin.email) if admin_exists: diff --git a/schemas/admin.py b/schemas/admin.py new file mode 100644 index 0000000..a459d03 --- /dev/null +++ b/schemas/admin.py @@ -0,0 +1,22 @@ +from pydantic import BaseModel +from fastapi.security import HTTPBasicCredentials +from pydantic import EmailStr + +class AdminSignIn(HTTPBasicCredentials): + class Config: + schema_extra = { + "example": {"username": "abdul@youngest.dev", "password": "3xt3m#"} + } + + +class AdminData(BaseModel): + fullname: str + email: EmailStr + + class Config: + schema_extra = { + "example": { + "fullname": "Abdulazeez Abdulazeez Adeshina", + "email": "abdul@youngest.dev", + } + } \ No newline at end of file