feat(task-005): complete article intake dashboard detail flow
This commit is contained in:
@@ -6,10 +6,14 @@ from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from src.domain.contracts import (
|
||||
ArticleSummary,
|
||||
PublishingRules,
|
||||
PublishingStatus,
|
||||
ArticleWorkflowStatus,
|
||||
WorkflowEventSummary,
|
||||
Role,
|
||||
ScriptConfigVersionStatus,
|
||||
TargetSiteConfig,
|
||||
@@ -31,6 +35,7 @@ class BackendRepository:
|
||||
self.dialect = "sqlite" if dsn.startswith("sqlite:///") else "postgres"
|
||||
self.users = UsersRepository(self)
|
||||
self.target_sites = TargetSitesRepository(self)
|
||||
self.articles = ArticlesRepository(self)
|
||||
self.script_config_versions = ScriptConfigVersionsRepository(self)
|
||||
self.schema = SchemaRepository(self)
|
||||
|
||||
@@ -384,6 +389,204 @@ class TargetSitesRepository:
|
||||
return [_target_site_from_row(row) for row in rows]
|
||||
|
||||
|
||||
class ArticlesRepository:
|
||||
def __init__(self, repository: BackendRepository) -> None:
|
||||
self._repository = repository
|
||||
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
target_site_id: UUID,
|
||||
status: ArticleWorkflowStatus,
|
||||
publishing_status: PublishingStatus,
|
||||
brief_description: str,
|
||||
working_title: str | None,
|
||||
language: str,
|
||||
content_type: str,
|
||||
primary_keyword: str | None,
|
||||
assigned_editor_id: UUID | None,
|
||||
created_at: datetime,
|
||||
updated_at: datetime,
|
||||
) -> ArticleSummary:
|
||||
article_id = uuid4()
|
||||
placeholder = self._repository.placeholder()
|
||||
sql = f"""
|
||||
INSERT INTO articles (
|
||||
id,
|
||||
target_site_id,
|
||||
status,
|
||||
publishing_status,
|
||||
brief_description,
|
||||
working_title,
|
||||
language,
|
||||
content_type,
|
||||
primary_keyword,
|
||||
assigned_editor_id,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder}
|
||||
)
|
||||
"""
|
||||
with self._repository.connection() as connection:
|
||||
connection.execute(
|
||||
sql,
|
||||
(
|
||||
str(article_id),
|
||||
str(target_site_id),
|
||||
status.value,
|
||||
publishing_status.value,
|
||||
brief_description,
|
||||
working_title,
|
||||
language,
|
||||
content_type,
|
||||
primary_keyword,
|
||||
_uuid_value(assigned_editor_id),
|
||||
_datetime_value(created_at),
|
||||
_datetime_value(updated_at),
|
||||
),
|
||||
)
|
||||
|
||||
return self.get(article_id)
|
||||
|
||||
def list(self) -> list[ArticleSummary]:
|
||||
with self._repository.connection() as connection:
|
||||
rows = connection.execute(
|
||||
"""
|
||||
SELECT
|
||||
id,
|
||||
target_site_id,
|
||||
status,
|
||||
publishing_status,
|
||||
brief_description,
|
||||
working_title,
|
||||
language,
|
||||
content_type,
|
||||
primary_keyword,
|
||||
assigned_editor_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM articles
|
||||
ORDER BY updated_at DESC
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
return [_article_summary_from_row(row) for row in rows]
|
||||
|
||||
def get(self, article_id: UUID) -> ArticleSummary:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
row = connection.execute(
|
||||
f"""
|
||||
SELECT
|
||||
id,
|
||||
target_site_id,
|
||||
status,
|
||||
publishing_status,
|
||||
brief_description,
|
||||
working_title,
|
||||
language,
|
||||
content_type,
|
||||
primary_keyword,
|
||||
assigned_editor_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM articles
|
||||
WHERE id = {placeholder}
|
||||
""",
|
||||
(str(article_id),),
|
||||
).fetchone()
|
||||
|
||||
if row is None:
|
||||
raise LookupError(f"Article not found: {article_id}")
|
||||
return _article_summary_from_row(row)
|
||||
|
||||
def create_workflow_event(
|
||||
self,
|
||||
*,
|
||||
article_id: UUID,
|
||||
event_type: str,
|
||||
from_status: ArticleWorkflowStatus | None,
|
||||
to_status: ArticleWorkflowStatus | None,
|
||||
actor_user_id: UUID | None,
|
||||
payload: dict[str, Any],
|
||||
created_at: datetime,
|
||||
) -> None:
|
||||
placeholder = self._repository.placeholder()
|
||||
json_cast = self._repository.json_cast()
|
||||
sql = f"""
|
||||
INSERT INTO workflow_events (
|
||||
id,
|
||||
article_id,
|
||||
event_type,
|
||||
from_status,
|
||||
to_status,
|
||||
actor_user_id,
|
||||
payload,
|
||||
created_at
|
||||
)
|
||||
VALUES (
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder},
|
||||
{placeholder}{json_cast},
|
||||
{placeholder}
|
||||
)
|
||||
"""
|
||||
with self._repository.connection() as connection:
|
||||
connection.execute(
|
||||
sql,
|
||||
(
|
||||
str(uuid4()),
|
||||
str(article_id),
|
||||
event_type,
|
||||
_article_status_value(from_status),
|
||||
_article_status_value(to_status),
|
||||
_uuid_value(actor_user_id),
|
||||
_json_value(payload),
|
||||
_datetime_value(created_at),
|
||||
),
|
||||
)
|
||||
|
||||
def list_workflow_events(self, article_id: UUID) -> list[WorkflowEventSummary]:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
rows = connection.execute(
|
||||
f"""
|
||||
SELECT
|
||||
id,
|
||||
article_id,
|
||||
event_type,
|
||||
from_status,
|
||||
to_status,
|
||||
actor_user_id,
|
||||
payload,
|
||||
created_at
|
||||
FROM workflow_events
|
||||
WHERE article_id = {placeholder}
|
||||
ORDER BY created_at
|
||||
""",
|
||||
(str(article_id),),
|
||||
).fetchall()
|
||||
|
||||
return [_workflow_event_from_row(row) for row in rows]
|
||||
|
||||
|
||||
class ScriptConfigVersionsRepository:
|
||||
def __init__(self, repository: BackendRepository) -> None:
|
||||
self._repository = repository
|
||||
@@ -687,6 +890,36 @@ def _target_site_from_row(row: Any) -> TargetSiteConfig:
|
||||
)
|
||||
|
||||
|
||||
def _article_summary_from_row(row: Any) -> ArticleSummary:
|
||||
return ArticleSummary(
|
||||
id=_row_value(row, "id"),
|
||||
target_site_id=_row_value(row, "target_site_id"),
|
||||
status=_row_value(row, "status"),
|
||||
publishing_status=_row_value(row, "publishing_status"),
|
||||
brief_description=_row_value(row, "brief_description"),
|
||||
working_title=_row_value(row, "working_title"),
|
||||
language=_row_value(row, "language"),
|
||||
content_type=_row_value(row, "content_type"),
|
||||
primary_keyword=_row_value(row, "primary_keyword"),
|
||||
assigned_editor_id=_row_value(row, "assigned_editor_id"),
|
||||
created_at=_row_value(row, "created_at"),
|
||||
updated_at=_row_value(row, "updated_at"),
|
||||
)
|
||||
|
||||
|
||||
def _workflow_event_from_row(row: Any) -> WorkflowEventSummary:
|
||||
return WorkflowEventSummary(
|
||||
id=_row_value(row, "id"),
|
||||
article_id=_row_value(row, "article_id"),
|
||||
event_type=_row_value(row, "event_type"),
|
||||
from_status=_row_value(row, "from_status"),
|
||||
to_status=_row_value(row, "to_status"),
|
||||
actor_user_id=_row_value(row, "actor_user_id"),
|
||||
payload=_json_from_row(row, "payload"),
|
||||
created_at=_row_value(row, "created_at"),
|
||||
)
|
||||
|
||||
|
||||
def _plain_row(row: Any) -> dict[str, Any]:
|
||||
if isinstance(row, sqlite3.Row):
|
||||
result = dict(row)
|
||||
@@ -733,3 +966,9 @@ def _datetime_value(value: datetime | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
return value.isoformat()
|
||||
|
||||
|
||||
def _article_status_value(value: ArticleWorkflowStatus | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
return value.value
|
||||
|
||||
Reference in New Issue
Block a user