From edf213bfe2ad2fbb8ac82626711f3150fd0c3a2e Mon Sep 17 00:00:00 2001 From: Backend IM Bot <> Date: Thu, 20 Mar 2025 17:14:30 +0100 Subject: [PATCH] Update generated backend for blog_app with entities: tags, user, and --- app/api/core/dependencies/dependencies.py | 1 + app/api/core/middleware/activity_tracker.py | 16 ++++------------ app/api/db/database.py | 1 + app/api/v1/models/and.py | 4 ++-- app/api/v1/models/user.py | 7 +++---- app/api/v1/routes/__init__.py | 6 +++--- app/api/v1/routes/and.py | 18 +++++++++--------- app/api/v1/schemas/and.py | 4 +++- app/api/v1/schemas/user.py | 11 ++++++++--- main.py | 8 +------- requirements.txt | 6 +++++- 11 files changed, 40 insertions(+), 42 deletions(-) diff --git a/app/api/core/dependencies/dependencies.py b/app/api/core/dependencies/dependencies.py index 1ba6818..965f5ff 100644 --- a/app/api/core/dependencies/dependencies.py +++ b/app/api/core/dependencies/dependencies.py @@ -1,3 +1,4 @@ +from sqlalchemy.orm import Session from app.api.db.database import SessionLocal def get_db(): diff --git a/app/api/core/middleware/activity_tracker.py b/app/api/core/middleware/activity_tracker.py index 8144e80..f9a3f8e 100644 --- a/app/api/core/middleware/activity_tracker.py +++ b/app/api/core/middleware/activity_tracker.py @@ -7,15 +7,7 @@ class ActivityTrackerMiddleware(BaseHTTPMiddleware): start_time = time.time() response = await call_next(request) process_time = time.time() - start_time - logger.info(f"{request.method} {request.url} - {process_time:.4f} seconds") - return response - - -The `dispatch` method first records the start time using `time.time()`. It then awaits the next middleware or the application itself by calling `call_next(request)`. After the response is received, the processing time is calculated by subtracting the start time from the current time. Finally, the request method, URL, and processing time are logged using `logger.info`. - - -from fastapi import FastAPI -from app.api.core.middleware.activity_tracker import ActivityTrackerMiddleware - -app = FastAPI() -app.add_middleware(ActivityTrackerMiddleware) \ No newline at end of file + request_method = request.method + request_url = str(request.url) + logger.info(f"{request_method} {request_url} processed in {process_time:.5f} seconds") + return response \ No newline at end of file diff --git a/app/api/db/database.py b/app/api/db/database.py index c6d7a01..62b0c00 100644 --- a/app/api/db/database.py +++ b/app/api/db/database.py @@ -6,6 +6,7 @@ SQLALCHEMY_DATABASE_URL = "sqlite:///./blog_app.db" engine = create_engine( SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} +) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() \ No newline at end of file diff --git a/app/api/v1/models/and.py b/app/api/v1/models/and.py index 5b9193c..5db1b15 100644 --- a/app/api/v1/models/and.py +++ b/app/api/v1/models/and.py @@ -8,6 +8,6 @@ class And(Base): id = Column(Integer, primary_key=True, index=True) title = Column(String) content = Column(String) - author_id = Column(Integer, ForeignKey("users.id")) + user_id = Column(Integer, ForeignKey("users.id")) - author = relationship("User", back_populates="ands") \ No newline at end of file + user = relationship("User", back_populates="ands") \ No newline at end of file diff --git a/app/api/v1/models/user.py b/app/api/v1/models/user.py index 5db6ec6..2c259f8 100644 --- a/app/api/v1/models/user.py +++ b/app/api/v1/models/user.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, Integer, String +from sqlalchemy import Column, Integer, String, Boolean from app.api.db.database import Base class User(Base): @@ -8,6 +8,5 @@ class User(Base): username = Column(String, unique=True, index=True) email = Column(String, unique=True, index=True) password = Column(String) - full_name = Column(String) - bio = Column(String) - profile_picture = Column(String) \ No newline at end of file + is_active = Column(Boolean, default=True) + is_superuser = Column(Boolean, default=False) \ No newline at end of file diff --git a/app/api/v1/routes/__init__.py b/app/api/v1/routes/__init__.py index 591bba2..3d1bb32 100644 --- a/app/api/v1/routes/__init__.py +++ b/app/api/v1/routes/__init__.py @@ -1,11 +1,11 @@ from fastapi import APIRouter -from .and import router as and_router from .tags import router as tags_router from .user import router as user_router +from .and import router as ands_router router = APIRouter() -router.include_router(and_router, prefix="/ands", tags=["ands"]) router.include_router(tags_router, prefix="/tags", tags=["tags"]) -router.include_router(user_router, prefix="/users", tags=["users"]) \ No newline at end of file +router.include_router(user_router, prefix="/users", tags=["users"]) +router.include_router(ands_router, prefix="/ands", tags=["ands"]) \ No newline at end of file diff --git a/app/api/v1/routes/and.py b/app/api/v1/routes/and.py index 145fc8e..fe8e524 100644 --- a/app/api/v1/routes/and.py +++ b/app/api/v1/routes/and.py @@ -2,9 +2,9 @@ from typing import List from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session +from app.api.core.dependencies.dependencies import get_db from app.api.v1.models.and import And from app.api.v1.schemas.and import AndCreate, AndResponse -from app.api.core.dependencies.dependencies import get_db router = APIRouter() @@ -14,16 +14,16 @@ def read_ands(db: Session = Depends(get_db)): return ands @router.post("/ands", response_model=AndResponse) -def create_and(and_create: AndCreate, db: Session = Depends(get_db)): - and_db = And(**and_create.dict()) - db.add(and_db) +def create_and(and_data: AndCreate, db: Session = Depends(get_db)): + and_instance = And(**and_data.dict()) + db.add(and_instance) db.commit() - db.refresh(and_db) - return and_db + db.refresh(and_instance) + return and_instance @router.get("/ands/{id}", response_model=AndResponse) def read_and(id: int, db: Session = Depends(get_db)): - and_db = db.query(And).get(id) - if not and_db: + and_instance = db.query(And).get(id) + if not and_instance: raise HTTPException(status_code=404, detail="And not found") - return and_db \ No newline at end of file + return and_instance \ No newline at end of file diff --git a/app/api/v1/schemas/and.py b/app/api/v1/schemas/and.py index 7ed1361..78fc207 100644 --- a/app/api/v1/schemas/and.py +++ b/app/api/v1/schemas/and.py @@ -1,13 +1,15 @@ from pydantic import BaseModel class AndBase(BaseModel): - pass + title: str + body: str class AndCreate(AndBase): pass class And(AndBase): id: int + owner_id: int class Config: orm_mode = True \ No newline at end of file diff --git a/app/api/v1/schemas/user.py b/app/api/v1/schemas/user.py index 72cee50..6e6fd82 100644 --- a/app/api/v1/schemas/user.py +++ b/app/api/v1/schemas/user.py @@ -1,16 +1,21 @@ from pydantic import BaseModel, EmailStr +from typing import Optional class UserBase(BaseModel): - email: EmailStr - is_active: bool = True + email: Optional[EmailStr] = None + is_active: Optional[bool] = True is_superuser: bool = False - full_name: str | None = None + full_name: Optional[str] = None class UserCreate(UserBase): + email: EmailStr password: str class User(UserBase): id: int + is_active: bool + is_superuser: bool + full_name: str class Config: orm_mode = True \ No newline at end of file diff --git a/main.py b/main.py index 0af0cb0..aa50460 100644 --- a/main.py +++ b/main.py @@ -8,10 +8,4 @@ app = FastAPI() app.include_router(v1_router, prefix="/v1") app.add_middleware(ActivityTrackerMiddleware) -@app.on_event("startup") -async def startup_event(): - Base.metadata.create_all(bind=engine) - -if __name__ == "__main__": - import uvicorn - uvicorn.run(app, host="0.0.0.0", port=8000) \ No newline at end of file +Base.metadata.create_all(bind=engine) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 5dcc60d..1bbf170 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,5 @@ -# No valid code generated +fastapi +uvicorn +sqlalchemy +pydantic +loguru \ No newline at end of file