Compare commits
3 Commits
main
...
update-174
Author | SHA1 | Date | |
---|---|---|---|
![]() |
8f6f9f00f3 | ||
![]() |
b940724847 | ||
![]() |
ba552aff74 |
@ -1,5 +1,6 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from app.api.db.database import SessionLocal
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
|
@ -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')
|
||||
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
|
@ -1,7 +1,25 @@
|
||||
Here's a `comments.py` file for the `app/api/v1/models/` directory of the `blog_app_h23t0` FastAPI backend:
|
||||
|
||||
from typing import Optional
|
||||
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)
|
||||
author_id = Column(Integer, ForeignKey('users.id'), nullable=False)
|
||||
content = Column(Text, nullable=False)
|
||||
created_at = Column(Integer, nullable=False)
|
||||
updated_at = Column(Integer, nullable=True)
|
||||
|
||||
post = relationship('Posts', back_populates='comments')
|
||||
author = relationship('Users', back_populates='comments')
|
||||
|
||||
def __repr__(self):
|
||||
return f"Comments(id={self.id}, post_id={self.post_id}, author_id={self.author_id}, content='{self.content[:20]}...', created_at={self.created_at}, updated_at={self.updated_at})"
|
||||
|
||||
|
||||
The class includes the following columns:
|
@ -1,7 +1,35 @@
|
||||
Sure, here's the `posts.py` file for the `blog_app_h23t0` FastAPI backend:
|
||||
|
||||
|
||||
from typing import Optional
|
||||
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, nullable=False)
|
||||
content = Column(Text, nullable=False)
|
||||
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
|
||||
|
||||
user = relationship('Users', back_populates='posts')
|
||||
comments = relationship('Comments', back_populates='post')
|
||||
|
||||
def __repr__(self):
|
||||
return f"Posts(id={self.id}, title='{self.title}', user_id={self.user_id})"
|
||||
|
||||
Explanation:
|
||||
|
||||
1. We import the necessary modules from SQLAlchemy: `Column`, `ForeignKey`, `Integer`, `String`, and `Text`.
|
||||
6. We define the columns for the `Posts` model:
|
||||
- `id`: Integer primary key with index
|
||||
- `title`: String column, cannot be null
|
||||
- `content`: Text column, cannot be null
|
||||
- `user_id`: Integer foreign key referencing the `id` column of the `users` table, cannot be null
|
||||
7. We define the relationships:
|
||||
- `user`: One-to-many relationship with the `Users` model, backref `'posts'`
|
||||
- `comments`: One-to-many relationship with the `Comments` model, backref `'post'`
|
||||
|
||||
Note: This code assumes that you have a `Users` model defined elsewhere in your project, and that you will define a `Comments` model as well. You may need to adjust the relationship definitions and foreign key constraints based on your project's requirements.
|
@ -1,7 +1,32 @@
|
||||
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
||||
Sure, here's an example of the `tags.py` file for the `blog_app` FastAPI backend:
|
||||
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import Column, ForeignKey, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.api.db.database import Base
|
||||
|
||||
class Tags(Base):
|
||||
__tablename__ = 'tags'
|
||||
__tablename__ = "tags"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, nullable=False, unique=True)
|
||||
description = Column(String, nullable=True)
|
||||
|
||||
posts = relationship("PostTags", back_populates="tag")
|
||||
|
||||
def __repr__(self):
|
||||
return f"Tag(id={self.id}, name='{self.name}', description='{self.description}')"
|
||||
|
||||
Explanation:
|
||||
|
||||
1. We import the necessary modules from SQLAlchemy (`Column`, `ForeignKey`, `Integer`, `String`) and `relationship` from `sqlalchemy.orm`.
|
||||
4. We define the following columns for the `Tags` model:
|
||||
- `id`: an integer primary key with an index.
|
||||
- `name`: a string column that cannot be null and must be unique.
|
||||
- `description`: an optional string column that can be null.
|
||||
5. We define a relationship with the `PostTags` model (not shown here) using the `relationship` function from SQLAlchemy. This relationship is bi-directional, as indicated by `back_populates="tag"`.
|
||||
|
||||
Note: This example assumes that you have a `PostTags` model (or a similar model) defined elsewhere in your application to represent the many-to-many relationship between posts and tags.
|
@ -1,7 +1,47 @@
|
||||
Here's the `user.py` file for the `blog_app` FastAPI backend, located in the `app/api/v1/models/` directory:
|
||||
|
||||
from typing import Optional
|
||||
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'
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
username = Column(String, unique=True, nullable=False)
|
||||
email = Column(String, unique=True, nullable=False)
|
||||
password = Column(String, nullable=False)
|
||||
full_name = Column(String)
|
||||
bio = Column(Text)
|
||||
|
||||
posts = relationship("Post", back_populates="author")
|
||||
comments = relationship("Comment", back_populates="author")
|
||||
|
||||
def __repr__(self):
|
||||
return f"User(id={self.id}, username='{self.username}', email='{self.email}')"
|
||||
|
||||
Explanation:
|
||||
|
||||
1. Imports:
|
||||
- `typing.Optional`: Used for type hints.
|
||||
- `sqlalchemy`: Imported `Column`, `ForeignKey`, `Integer`, `String`, and `Text` classes for defining table columns.
|
||||
- `sqlalchemy.orm`: Imported `relationship` for defining relationships between models.
|
||||
- `app.api.db.database`: Imported `Base` class, which is the base class for all SQLAlchemy models.
|
||||
|
||||
2. `User` class:
|
||||
- Inherits from the `Base` class.
|
||||
- `__tablename__` attribute specifies the name of the database table for this model.
|
||||
- Columns:
|
||||
- `id`: Integer primary key column with an index.
|
||||
- `username`: String column, unique and not nullable.
|
||||
- `email`: String column, unique and not nullable.
|
||||
- `password`: String column, not nullable.
|
||||
- `full_name`: String column.
|
||||
- `bio`: Text column.
|
||||
- Relationships:
|
||||
- `posts`: One-to-many relationship with the `Post` model, where a user can have multiple posts.
|
||||
- `comments`: One-to-many relationship with the `Comment` model, where a user can have multiple comments.
|
||||
- `__repr__` method: Provides a string representation of the `User` object for debugging purposes.
|
||||
|
||||
Note: The `Post` and `Comment` models are not defined in this file, but they are assumed to exist and have the appropriate relationships defined with the `User` model.
|
@ -1,2 +1,3 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
@ -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
|
@ -1,10 +1,9 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class PostsBase(BaseModel):
|
||||
pass
|
||||
|
||||
class Posts(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
body: str
|
||||
|
||||
class Posts(PostsBase):
|
||||
class Config:
|
||||
orm_mode = True
|
@ -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
|
8
main.py
8
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():
|
||||
app.include_router(router, prefix="/v1")
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup_event():
|
||||
Base.metadata.create_all(bind=engine)
|
@ -1 +1,5 @@
|
||||
# No code generated
|
||||
fastapi
|
||||
uvicorn
|
||||
sqlalchemy
|
||||
pydantic
|
||||
loguru
|
||||
|
Loading…
x
Reference in New Issue
Block a user