diff --git a/src/app/crud/crud_post.py b/src/app/crud/crud_post.py index 38f34c5..0c75c59 100644 --- a/src/app/crud/crud_post.py +++ b/src/app/crud/crud_post.py @@ -1,4 +1,7 @@ -from typing import List +from typing import ( + List, + Optional +) from fastapi.encoders import jsonable_encoder @@ -15,7 +18,11 @@ from app.schemas.post import ( class CRUDPost(CRUDBase[Post, PostCreate, PostUpdate]): async def create_with_owner( - self, db: Session, *, obj_in: PostCreate, owner_id: int + self, + db: Session, + *, + obj_in: PostCreate, + owner_id: int ) -> Post: obj_in_data = jsonable_encoder(obj_in) @@ -26,8 +33,23 @@ class CRUDPost(CRUDBase[Post, PostCreate, PostUpdate]): return db_obj + async def get_by_slug( + self, + db: Session, + slug: str + ) -> Optional[Post]: + + q = select(self.model).where(self.model.slug == slug) + obj = await db.execute(q) + return obj.scalar_one_or_none() + async def get_multi_by_owner( - self, db: Session, *, owner_id: int, skip: int = 0, limit: int = 100 + self, + db: Session, + *, + owner_id: int, + skip: int = 0, + limit: int = 100 ) -> List[Post]: q = ( diff --git a/src/app/main.py b/src/app/main.py index a3f06e9..f27663d 100644 --- a/src/app/main.py +++ b/src/app/main.py @@ -12,7 +12,7 @@ from app.schemas.post import Post, PostCreate, PostUpdate from sqlalchemy.ext.asyncio import AsyncSession from app.schemas.user import User, UserCreate from fastapi.responses import FileResponse, HTMLResponse -from app.views.depends import get_async_db, handle_image +from app.views.depends import get_async_db, handle_post_image_or_die from typing import Annotated, Any from fastapi.templating import Jinja2Templates diff --git a/src/app/models/post.py b/src/app/models/post.py index 775cd67..2516675 100644 --- a/src/app/models/post.py +++ b/src/app/models/post.py @@ -22,7 +22,7 @@ class Post(Base): title = Column(String(100), index=True, nullable=False) slug = Column(String(), index=True, nullable=False, unique=True) text = Column(String(), index=True, nullable=False) - image_path = Column(String(100), index=True, unique=True, nullable=True) + image_path = Column(String(100), index=True, unique=True, nullable=False) owner_id = Column(Integer(), ForeignKey("user.id")) owner = relationship("User", back_populates="posts") diff --git a/src/app/schemas/post.py b/src/app/schemas/post.py index e00fd33..ae93056 100644 --- a/src/app/schemas/post.py +++ b/src/app/schemas/post.py @@ -12,7 +12,7 @@ from app.core.config import settings class PostBase(BaseModel): - title: Optional[str] = None + title: Optional[str] = Field(max_length=100) text: Optional[str] = None image_path: Optional[str] = None @@ -23,8 +23,7 @@ class PostCreate(PostBase): image_path: str -class PostUpdate(PostBase): - title: Optional[str] = Field(max_length=100) +class PostUpdate(PostBase): ... class PostInTemplate(PostBase): @@ -43,7 +42,7 @@ class PostInDBBase(PostBase): slug: str title: str text: str - image_path: str + image_path: Optional[str] = None owner_id: int class Config: @@ -53,7 +52,10 @@ class PostInDBBase(PostBase): class Post(PostInDBBase): @computed_field @property - def image_url(self) -> str: + def image_url(self) -> str | None: + if self.image_path is None: + return None + return str( settings.MEDIA_FOLDER / settings.FILE_FOLDERS['post_images'] / diff --git a/src/app/templates/admin/add-post.html b/src/app/templates/admin/add-post.html new file mode 100644 index 0000000..70c1934 --- /dev/null +++ b/src/app/templates/admin/add-post.html @@ -0,0 +1,57 @@ + + + +
+ + +