Replace video_warning with target_type + has_videos/has_photos
All checks were successful
Validate / Hassfest (push) Successful in 2s

Major template system improvements:
- Remove video_warning field from TemplateConfig model
- Add target_type, has_videos, has_photos to template context
- Templates use {% if target_type == "telegram" and has_videos %}
  for conditional Telegram warnings instead of a separate field
- date_format moved from "Telegram" to "Settings" group
- Add target type selector (Telegram/Webhook) in template editor
  to preview how templates render for each target type
- All template slots now use JinjaEditor (not plain <input>)
- Preview endpoint accepts target_type parameter
- Clean up TemplateConfigCreate schema (remove stale fields)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-19 21:11:38 +03:00
parent 510463cba6
commit 4babaddd87
9 changed files with 66 additions and 49 deletions

View File

@@ -67,7 +67,9 @@ _SAMPLE_CONTEXT = {
"removed_assets": ["asset-id-1", "asset-id-2"],
"people": ["Alice", "Bob"],
"shared": True,
"video_warning": "⚠️ Note: Videos may not be sent due to Telegram's 50 MB file size limit.",
"target_type": "telegram",
"has_videos": True,
"has_photos": True,
# Rename fields (always present, empty for non-rename events)
"old_name": "Old Album",
"new_name": "New Album",
@@ -82,27 +84,16 @@ _SAMPLE_CONTEXT = {
class TemplateConfigCreate(BaseModel):
name: str
description: str | None = None
icon: str | None = None
message_assets_added: str | None = None
message_assets_removed: str | None = None
message_album_renamed: str | None = None
message_album_deleted: str | None = None
message_asset_image: str | None = None
message_asset_video: str | None = None
message_assets_format: str | None = None
message_assets_more: str | None = None
message_people_format: str | None = None
date_format: str | None = None
common_date_template: str | None = None
date_if_unique_template: str | None = None
location_format: str | None = None
common_location_template: str | None = None
location_if_unique_template: str | None = None
favorite_indicator: str | None = None
periodic_summary_message: str | None = None
periodic_album_template: str | None = None
scheduled_assets_message: str | None = None
memory_mode_message: str | None = None
video_warning: str | None = None
date_format: str | None = None
TemplateConfigUpdate = TemplateConfigCreate # Same shape, all optional
@@ -203,6 +194,7 @@ async def preview_config(
class PreviewRequest(BaseModel):
template: str
target_type: str = "telegram" # "telegram" or "webhook"
@router.post("/preview-raw")
@@ -229,9 +221,10 @@ async def preview_raw(
# Pass 2: render with strict undefined to catch unknown variables
try:
ctx = {**_SAMPLE_CONTEXT, "target_type": body.target_type}
strict_env = SandboxedEnvironment(autoescape=False, undefined=StrictUndefined)
tmpl = strict_env.from_string(body.template)
rendered = tmpl.render(**_SAMPLE_CONTEXT)
rendered = tmpl.render(**ctx)
return {"rendered": rendered}
except UndefinedError as e:
# Still a valid template syntactically, but references unknown variable

View File

@@ -47,7 +47,9 @@ TEMPLATE_VARIABLES: dict[str, dict] = {
"removed_assets": "Always empty list",
"people": "Detected people across all added assets (list of strings)",
"shared": "Whether album is shared (boolean)",
"video_warning": "Video size warning text (set from template config if videos present)",
"target_type": "Target type: 'telegram' or 'webhook'",
"has_videos": "Whether added assets contain videos (boolean)",
"has_photos": "Whether added assets contain photos (boolean)",
"old_name": "Always empty (for rename events)",
"new_name": "Always empty (for rename events)",
},
@@ -66,7 +68,7 @@ TEMPLATE_VARIABLES: dict[str, dict] = {
"removed_assets": "List of removed asset IDs (strings)",
"people": "People in the album (list of strings)",
"shared": "Whether album is shared (boolean)",
"video_warning": "Always empty",
"target_type": "Target type: 'telegram' or 'webhook'",
"old_name": "Always empty",
"new_name": "Always empty",
},

View File

@@ -135,9 +135,6 @@ class TemplateConfig(SQLModel, table=True):
# Settings
date_format: str = Field(default="%d.%m.%Y, %H:%M UTC")
video_warning: str = Field(
default="⚠️ Note: Videos may not be sent due to Telegram's 50 MB file size limit."
)
created_at: datetime = Field(default_factory=_utcnow)
@@ -204,7 +201,9 @@ _INLINE_TEMPLATES_REMOVED = {
'{%- if asset.is_favorite %} ❤️{% endif %}\n'
'{%- endfor %}'
'{%- endif %}'
'{%- if video_warning %}\n\n{{ video_warning }}{%- endif %}'
'{%- if target_type == "telegram" and has_videos %}\n\n'
'⚠️ Videos may not be sent due to Telegram\'s 50 MB file size limit.'
'{%- endif %}'
),
"message_assets_removed": '🗑️ {{ removed_count }} photo(s) removed from album "{{ album_name }}".',
@@ -254,7 +253,9 @@ DEFAULT_TEMPLATE_RU = {
'{%- if asset.is_favorite %} ❤️{% endif %}\n'
'{%- endfor %}'
'{%- endif %}'
'{%- if video_warning %}\n\n{{ video_warning }}{%- endif %}'
'{%- if target_type == "telegram" and has_videos %}\n\n'
'⚠️ Видео может не отправиться из-за ограничения Telegram в 50 МБ.'
'{%- endif %}'
),
"message_assets_removed": '🗑️ {{ removed_count }} фото удалено из альбома "{{ album_name }}".',

View File

@@ -37,7 +37,7 @@ def _render(template_str: str, ctx: dict[str, Any]) -> str:
def build_full_context(
event_data: dict[str, Any],
template_config: TemplateConfig | None = None,
target_type: str = "webhook",
) -> dict[str, Any]:
"""Build template context by passing raw data directly to Jinja2.
@@ -50,10 +50,13 @@ def build_full_context(
if isinstance(ctx.get("people"), str):
ctx["people"] = [ctx["people"]] if ctx["people"] else []
# Video warning
# Asset type flags
added_assets = ctx.get("added_assets", [])
has_videos = any(a.get("type") == "VIDEO" for a in added_assets) if added_assets else False
ctx["video_warning"] = (template_config.video_warning if template_config and has_videos else "")
ctx["has_videos"] = any(a.get("type") == "VIDEO" for a in added_assets) if added_assets else False
ctx["has_photos"] = any(a.get("type") == "IMAGE" for a in added_assets) if added_assets else False
# Target type for conditional formatting (e.g. Telegram video size warning)
ctx["target_type"] = target_type
return ctx
@@ -75,7 +78,7 @@ async def send_notification(
# Render with template engine
if message is None:
ctx = build_full_context(event_data, template_config)
ctx = build_full_context(event_data, target_type=target.type)
template_body = DEFAULT_TEMPLATE
if template_config:

View File

@@ -9,7 +9,7 @@
{%- if asset.is_favorite %} ❤️{% endif %}
{%- endfor %}
{%- endif %}
{%- if video_warning %}
{%- if target_type == "telegram" and has_videos %}
{{ video_warning }}
⚠️ Videos may not be sent due to Telegram's 50 MB file size limit.
{%- endif %}

View File

@@ -9,7 +9,7 @@
{%- if asset.is_favorite %} ❤️{% endif %}
{%- endfor %}
{%- endif %}
{%- if video_warning %}
{%- if target_type == "telegram" and has_videos %}
{{ video_warning }}
⚠️ Видео может не отправиться из-за ограничения Telegram в 50 МБ.
{%- endif %}