From b9407248473b105e5d5185b2c13f37cc3374db4a Mon Sep 17 00:00:00 2001 From: Backend IM Bot <> Date: Fri, 21 Mar 2025 10:58:48 +0100 Subject: [PATCH] Update generated backend for blog_app with entities: posts, comments, tags, user --- app/api/core/dependencies/dependencies.py | 2 -- app/api/core/middleware/activity_tracker.py | 13 ++++++----- app/api/v1/models/comments.py | 25 +++++++-------------- app/api/v1/models/posts.py | 6 +++++ app/api/v1/models/tags.py | 5 +++++ app/api/v1/models/user.py | 25 +++++---------------- app/api/v1/routes/__init__.py | 3 ++- app/api/v1/schemas/comments.py | 7 +++--- app/api/v1/schemas/posts.py | 9 ++++---- app/api/v1/schemas/tags.py | 7 +++--- app/api/v1/schemas/user.py | 7 +++--- main.py | 10 +++++---- 12 files changed, 56 insertions(+), 63 deletions(-) diff --git a/app/api/core/dependencies/dependencies.py b/app/api/core/dependencies/dependencies.py index 4f30cb4..965f5ff 100644 --- a/app/api/core/dependencies/dependencies.py +++ b/app/api/core/dependencies/dependencies.py @@ -1,5 +1,3 @@ -Here's the `dependencies.py` file for the `app/api/core/dependencies/` directory: - from sqlalchemy.orm import Session from app.api.db.database import SessionLocal diff --git a/app/api/core/middleware/activity_tracker.py b/app/api/core/middleware/activity_tracker.py index d4996e2..8c61125 100644 --- a/app/api/core/middleware/activity_tracker.py +++ b/app/api/core/middleware/activity_tracker.py @@ -1,12 +1,13 @@ -import time +from time import time from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request from starlette.responses import Response from loguru import logger + class ActivityTrackerMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): - start_time = time.time() - response = await call_next(request) - process_time = time.time() - start_time - logger.info(f'{request.method} {request.url} - Process Time: {process_time:.6f} seconds') - return response + start_time = time() + response: Response = await call_next(request) + process_time = time() - start_time + logger.info(f"Processed {request.method} {request.url} in {process_time:.4f} seconds") + return response \ No newline at end of file diff --git a/app/api/v1/models/comments.py b/app/api/v1/models/comments.py index 43b450d..00c5c71 100644 --- a/app/api/v1/models/comments.py +++ b/app/api/v1/models/comments.py @@ -1,25 +1,16 @@ -Here's the `comments.py` file for the `app/api/v1/models/` directory of the `blog_app_h23t0` FastAPI backend: - from sqlalchemy import Column, ForeignKey, Integer, String, Text +from sqlalchemy.orm import relationship from app.api.db.database import Base class Comments(Base): __tablename__ = 'comments' id = Column(Integer, primary_key=True, index=True) - post_id = Column(Integer, ForeignKey('posts.id'), nullable=False) - user_id = Column(Integer, ForeignKey('users.id'), nullable=False) - comment_text = Column(Text, nullable=False) - created_at = Column(String, nullable=False) - updated_at = Column(String, nullable=True) + post_id = Column(Integer, ForeignKey('posts.id')) + user_id = Column(Integer, ForeignKey('users.id')) + comment_text = Column(Text) + created_at = Column(Integer) + updated_at = Column(Integer) -Explanation: - -1. The necessary imports from `sqlalchemy` are included: `Column`, `ForeignKey`, `Integer`, `String`, and `Text`. -5. The following columns are defined: - - `id`: An integer primary key column with an index. - - `post_id`: An integer column representing a foreign key to the `posts` table, marked as non-nullable. - - `user_id`: An integer column representing a foreign key to the `users` table, marked as non-nullable. - - `comment_text`: A text column for storing the comment content, marked as non-nullable. - - `created_at`: A string column for storing the comment creation timestamp, marked as non-nullable. - - `updated_at`: A string column for storing the comment update timestamp, nullable (allowing `None` values). \ No newline at end of file + post = relationship('Post', back_populates='comments') + user = relationship('User', back_populates='comments') \ No newline at end of file diff --git a/app/api/v1/models/posts.py b/app/api/v1/models/posts.py index 79723fb..61ff5f6 100644 --- a/app/api/v1/models/posts.py +++ b/app/api/v1/models/posts.py @@ -1,7 +1,13 @@ from sqlalchemy import Column, ForeignKey, Integer, String, Text +from sqlalchemy.orm import relationship from app.api.db.database import Base class Posts(Base): __tablename__ = 'posts' id = Column(Integer, primary_key=True, index=True) + title = Column(String) + content = Column(Text) + user_id = Column(Integer, ForeignKey('users.id')) + + user = relationship('User', back_populates='posts') \ No newline at end of file diff --git a/app/api/v1/models/tags.py b/app/api/v1/models/tags.py index 813918e..689093b 100644 --- a/app/api/v1/models/tags.py +++ b/app/api/v1/models/tags.py @@ -1,7 +1,12 @@ from sqlalchemy import Column, ForeignKey, Integer, String, Text +from sqlalchemy.orm import relationship from app.api.db.database import Base class Tags(Base): __tablename__ = 'tags' id = Column(Integer, primary_key=True, index=True) + name = Column(String, nullable=False, unique=True) + description = Column(Text) + + posts = relationship("Post", back_populates="tags", secondary="post_tags") \ No newline at end of file diff --git a/app/api/v1/models/user.py b/app/api/v1/models/user.py index 0bf6713..4a2e76d 100644 --- a/app/api/v1/models/user.py +++ b/app/api/v1/models/user.py @@ -1,27 +1,14 @@ -Here's the `user.py` file for the `app/api/v1/models/` directory in the `blog_app` FastAPI backend: - from sqlalchemy import Column, ForeignKey, Integer, String, Text +from sqlalchemy.orm import relationship from app.api.db.database import Base class User(Base): - __tablename__ = "user" + __tablename__ = 'user' id = Column(Integer, primary_key=True, index=True) - username = Column(String, unique=True, nullable=False) - email = Column(String, unique=True, nullable=False) - password_hash = Column(String, nullable=False) + email = Column(String, unique=True, index=True) + hashed_password = Column(String) + is_active = Column(Integer, default=1) full_name = Column(String) - bio = Column(Text) - profile_pic = Column(String) -Explanation: - -1. We import the necessary classes from `sqlalchemy` for defining the database columns: `Column`, `ForeignKey`, `Integer`, `String`, and `Text`. -5. We define the following columns for the `User` model: - - `id`: An integer primary key column with an index. - - `username`: A string column that must be unique and cannot be null. - - `email`: A string column that must be unique and cannot be null. - - `password_hash`: A string column that cannot be null (for storing the hashed password). - - `full_name`: An optional string column for the user's full name. - - `bio`: An optional text column for the user's biography. - - `profile_pic`: An optional string column for the user's profile picture URL. \ No newline at end of file + posts = relationship("Post", back_populates="author") \ No newline at end of file diff --git a/app/api/v1/routes/__init__.py b/app/api/v1/routes/__init__.py index da060ab..4d5194a 100644 --- a/app/api/v1/routes/__init__.py +++ b/app/api/v1/routes/__init__.py @@ -1,2 +1,3 @@ from fastapi import APIRouter -router = APIRouter() + +router = APIRouter() \ No newline at end of file diff --git a/app/api/v1/schemas/comments.py b/app/api/v1/schemas/comments.py index a04f3ef..ce46571 100644 --- a/app/api/v1/schemas/comments.py +++ b/app/api/v1/schemas/comments.py @@ -1,10 +1,11 @@ from pydantic import BaseModel class CommentsBase(BaseModel): - pass + body: str -class Comments(BaseModel): +class Comments(CommentsBase): id: int + post_id: int class Config: - orm_mode = True + orm_mode = True \ No newline at end of file diff --git a/app/api/v1/schemas/posts.py b/app/api/v1/schemas/posts.py index 7536ecf..320aa5a 100644 --- a/app/api/v1/schemas/posts.py +++ b/app/api/v1/schemas/posts.py @@ -1,10 +1,9 @@ from pydantic import BaseModel class PostsBase(BaseModel): - pass - -class Posts(BaseModel): - id: int + title: str + content: str +class Posts(PostsBase): class Config: - orm_mode = True + orm_mode = True \ No newline at end of file diff --git a/app/api/v1/schemas/tags.py b/app/api/v1/schemas/tags.py index e212f4d..92f9fd2 100644 --- a/app/api/v1/schemas/tags.py +++ b/app/api/v1/schemas/tags.py @@ -1,10 +1,11 @@ from pydantic import BaseModel class TagsBase(BaseModel): - pass + name: str -class Tags(BaseModel): +class Tags(TagsBase): id: int + name: str class Config: - orm_mode = True + 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 819e527..b3cc369 100644 --- a/app/api/v1/schemas/user.py +++ b/app/api/v1/schemas/user.py @@ -1,10 +1,11 @@ from pydantic import BaseModel class UserBase(BaseModel): - pass + email: str -class User(BaseModel): +class User(UserBase): id: int + is_active: bool class Config: - orm_mode = True + orm_mode = True \ No newline at end of file diff --git a/main.py b/main.py index 1024a94..bf7adff 100644 --- a/main.py +++ b/main.py @@ -2,9 +2,11 @@ from fastapi import FastAPI from app.api.db.database import engine, Base from app.api.v1.routes import router from app.api.core.middleware.activity_tracker import ActivityTrackerMiddleware + app = FastAPI() app.add_middleware(ActivityTrackerMiddleware) -app.include_router(router, prefix='/v1') -@app.on_event('startup') -def startup(): - Base.metadata.create_all(bind=engine) +app.include_router(router, prefix="/v1") + +@app.on_event("startup") +def startup_event(): + Base.metadata.create_all(bind=engine) \ No newline at end of file