Task 004: add demo auth and role checks
This commit is contained in:
@@ -270,6 +270,93 @@ class TargetSitesRepository:
|
||||
raise LookupError(f"Target site not found: {slug}")
|
||||
return _target_site_from_row(row)
|
||||
|
||||
def get_by_id(self, site_id: UUID) -> TargetSiteConfig:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
row = connection.execute(
|
||||
f"""
|
||||
SELECT
|
||||
id,
|
||||
name,
|
||||
slug,
|
||||
publishing_type,
|
||||
default_language,
|
||||
brand_voice,
|
||||
audience,
|
||||
seo_rules,
|
||||
visual_rules,
|
||||
source_rules,
|
||||
publishing_rules,
|
||||
active_script_config_version_id,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM target_sites
|
||||
WHERE id = {placeholder}
|
||||
""",
|
||||
(str(site_id),),
|
||||
).fetchone()
|
||||
|
||||
if row is None:
|
||||
raise LookupError(f"Target site not found: {site_id}")
|
||||
return _target_site_from_row(row)
|
||||
|
||||
def update(
|
||||
self,
|
||||
*,
|
||||
site_id: UUID,
|
||||
name: str,
|
||||
slug: str,
|
||||
publishing_type: str,
|
||||
default_language: str,
|
||||
brand_voice: str,
|
||||
audience: str,
|
||||
seo_rules: JsonObject,
|
||||
visual_rules: JsonObject,
|
||||
source_rules: JsonObject,
|
||||
publishing_rules: PublishingRules,
|
||||
active_script_config_version_id: UUID | None,
|
||||
updated_at: datetime,
|
||||
) -> TargetSiteConfig:
|
||||
placeholder = self._repository.placeholder()
|
||||
json_cast = self._repository.json_cast()
|
||||
with self._repository.connection() as connection:
|
||||
connection.execute(
|
||||
f"""
|
||||
UPDATE target_sites
|
||||
SET
|
||||
name = {placeholder},
|
||||
slug = {placeholder},
|
||||
publishing_type = {placeholder},
|
||||
default_language = {placeholder},
|
||||
brand_voice = {placeholder},
|
||||
audience = {placeholder},
|
||||
seo_rules = {placeholder}{json_cast},
|
||||
visual_rules = {placeholder}{json_cast},
|
||||
source_rules = {placeholder}{json_cast},
|
||||
publishing_rules = {placeholder}{json_cast},
|
||||
active_script_config_version_id = {placeholder},
|
||||
updated_at = {placeholder}
|
||||
WHERE id = {placeholder}
|
||||
""",
|
||||
(
|
||||
name,
|
||||
slug,
|
||||
publishing_type,
|
||||
default_language,
|
||||
brand_voice,
|
||||
audience,
|
||||
_json_value(seo_rules),
|
||||
_json_value(visual_rules),
|
||||
_json_value(source_rules),
|
||||
_json_value(publishing_rules.model_dump(mode="json")),
|
||||
_uuid_value(active_script_config_version_id),
|
||||
_datetime_value(updated_at),
|
||||
str(site_id),
|
||||
),
|
||||
)
|
||||
|
||||
return self.get_by_id(site_id)
|
||||
|
||||
def list(self) -> list[TargetSiteConfig]:
|
||||
with self._repository.connection() as connection:
|
||||
rows = connection.execute(
|
||||
@@ -407,6 +494,88 @@ class ScriptConfigVersionsRepository:
|
||||
)
|
||||
return _plain_row(row)
|
||||
|
||||
def get_by_id(self, version_id: UUID) -> dict[str, Any]:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
row = connection.execute(
|
||||
f"""
|
||||
SELECT *
|
||||
FROM script_config_versions
|
||||
WHERE id = {placeholder}
|
||||
""",
|
||||
(str(version_id),),
|
||||
).fetchone()
|
||||
|
||||
if row is None:
|
||||
raise LookupError(f"Script config version not found: {version_id}")
|
||||
return _plain_row(row)
|
||||
|
||||
def activate(
|
||||
self,
|
||||
*,
|
||||
target_site_id: UUID,
|
||||
version_id: UUID,
|
||||
activated_at: datetime,
|
||||
) -> dict[str, Any]:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
existing = connection.execute(
|
||||
f"""
|
||||
SELECT id
|
||||
FROM script_config_versions
|
||||
WHERE id = {placeholder} AND target_site_id = {placeholder}
|
||||
""",
|
||||
(str(version_id), str(target_site_id)),
|
||||
).fetchone()
|
||||
if existing is None:
|
||||
raise LookupError(
|
||||
f"Script config version not found: {target_site_id} {version_id}"
|
||||
)
|
||||
|
||||
connection.execute(
|
||||
f"""
|
||||
UPDATE script_config_versions
|
||||
SET
|
||||
status = {placeholder},
|
||||
updated_at = {placeholder}
|
||||
WHERE target_site_id = {placeholder} AND id <> {placeholder}
|
||||
""",
|
||||
(
|
||||
ScriptConfigVersionStatus.DEPRECATED.value,
|
||||
_datetime_value(activated_at),
|
||||
str(target_site_id),
|
||||
str(version_id),
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
f"""
|
||||
UPDATE script_config_versions
|
||||
SET
|
||||
status = {placeholder},
|
||||
activated_at = {placeholder},
|
||||
updated_at = {placeholder}
|
||||
WHERE id = {placeholder}
|
||||
""",
|
||||
(
|
||||
ScriptConfigVersionStatus.ACTIVE.value,
|
||||
_datetime_value(activated_at),
|
||||
_datetime_value(activated_at),
|
||||
str(version_id),
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
f"""
|
||||
UPDATE target_sites
|
||||
SET
|
||||
active_script_config_version_id = {placeholder},
|
||||
updated_at = {placeholder}
|
||||
WHERE id = {placeholder}
|
||||
""",
|
||||
(str(version_id), _datetime_value(activated_at), str(target_site_id)),
|
||||
)
|
||||
|
||||
return self.get_by_id(version_id)
|
||||
|
||||
def list_for_site(self, target_site_id: UUID) -> list[dict[str, Any]]:
|
||||
placeholder = self._repository.placeholder()
|
||||
with self._repository.connection() as connection:
|
||||
|
||||
Reference in New Issue
Block a user