feat: add comments feature with nested replies and recursive rendering
All checks were successful
ci/woodpecker/pr/pipeline Pipeline was successful
All checks were successful
ci/woodpecker/pr/pipeline Pipeline was successful
Implement full comments system: domain entities (Comment, CommentLike), value objects (CommentContent), use cases (CRUD, like toggle), SQLAlchemy repository, API v1 endpoints, web UI with comment form and nested replies, i18n translations (EN/RU/FR/DE), and E2E tests. Fix nested reply (reply-to-reply) not displaying — the flat reply_comments dict was only queried for top-level comment IDs, so deeply nested replies were saved to DB (incrementing comment count) but never rendered. Switch to a recursive Jinja2 macro that renders any nesting depth.
This commit is contained in:
@@ -4,10 +4,12 @@ This module defines the database ORM models that map to database tables.
|
||||
Models are used by repositories for data persistence.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import JSON, Boolean, DateTime, ForeignKey, String, Text
|
||||
from sqlalchemy import JSON, Boolean, DateTime, ForeignKey, Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, declarative_base, mapped_column, relationship
|
||||
|
||||
Base = declarative_base()
|
||||
@@ -43,7 +45,10 @@ class PostORM(Base): # type: ignore[valid-type,misc]
|
||||
author_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||||
published: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True)
|
||||
like_count: Mapped[int] = mapped_column(default=0, nullable=False)
|
||||
likes: Mapped[list["PostLikeORM"]] = relationship(
|
||||
likes: Mapped[list[PostLikeORM]] = relationship(
|
||||
back_populates="post", cascade="all, delete-orphan"
|
||||
)
|
||||
comments: Mapped[list[CommentORM]] = relationship(
|
||||
back_populates="post", cascade="all, delete-orphan"
|
||||
)
|
||||
tags: Mapped[list[str]] = mapped_column(JSON, default=list)
|
||||
@@ -87,3 +92,87 @@ class PostLikeORM(Base): # type: ignore[valid-type,misc]
|
||||
)
|
||||
|
||||
post: Mapped[PostORM] = relationship(back_populates="likes")
|
||||
|
||||
|
||||
class CommentORM(Base): # type: ignore[valid-type,misc]
|
||||
"""SQLAlchemy model for Comment.
|
||||
|
||||
Database table representation of comments on blog posts.
|
||||
Maps to the 'comments' table with support for nested replies.
|
||||
|
||||
Attributes:
|
||||
id: Primary key as UUID string.
|
||||
post_id: Foreign key to the parent post.
|
||||
author_id: Comment author reference.
|
||||
content: Comment text content (Markdown supported).
|
||||
parent_id: Optional foreign key to parent comment (replies).
|
||||
like_count: Number of likes on this comment.
|
||||
created_at: Creation timestamp.
|
||||
updated_at: Last update timestamp.
|
||||
"""
|
||||
|
||||
__tablename__ = "comments"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
|
||||
post_id: Mapped[str] = mapped_column(
|
||||
String(36), ForeignKey("posts.id", ondelete="CASCADE"), nullable=False, index=True
|
||||
)
|
||||
author_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
parent_id: Mapped[str | None] = mapped_column(
|
||||
String(36),
|
||||
ForeignKey("comments.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
like_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(UTC),
|
||||
nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(UTC),
|
||||
onupdate=lambda: datetime.now(UTC),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
post: Mapped[PostORM] = relationship(back_populates="comments")
|
||||
replies: Mapped[list[CommentORM]] = relationship(
|
||||
back_populates="parent",
|
||||
)
|
||||
parent: Mapped[CommentORM | None] = relationship(
|
||||
back_populates="replies",
|
||||
remote_side="CommentORM.id",
|
||||
)
|
||||
|
||||
|
||||
class CommentLikeORM(Base): # type: ignore[valid-type,misc]
|
||||
"""SQLAlchemy model for CommentLike.
|
||||
|
||||
Database table representation of comment likes.
|
||||
Maps to the 'comment_likes' table tracking which users liked which comments.
|
||||
|
||||
Attributes:
|
||||
id: Primary key as UUID string.
|
||||
comment_id: Foreign key to the liked comment.
|
||||
liked_by: User identifier.
|
||||
created_at: Creation timestamp.
|
||||
"""
|
||||
|
||||
__tablename__ = "comment_likes"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
|
||||
comment_id: Mapped[str] = mapped_column(
|
||||
String(36),
|
||||
ForeignKey("comments.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
)
|
||||
liked_by: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(UTC),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user