gendesign/tradein-mvp/backend/app/services/brand.py
lekss361 987dce11bc
All checks were successful
Deploy Trade-In / changes (push) Successful in 12s
Deploy Trade-In / build-frontend (push) Has been skipped
Deploy Trade-In / build-browser (push) Has been skipped
Deploy Trade-In / test (push) Successful in 52s
Deploy Trade-In / build-backend (push) Successful in 1m5s
Deploy Trade-In / deploy (push) Successful in 54s
fix(tradein/pdf): МЕРА branding + 4-page report, no empty pages (#2513)
2026-07-13 18:04:07 +00:00

66 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Brand resolver — multi-tenant white-label.
Читает бренд по slug из `brands` Postgres-таблицы.
Используется PDF exporter + frontend (через /api/v1/brand/{slug} endpoint).
"""
from __future__ import annotations
import logging
from dataclasses import dataclass
from sqlalchemy import text
from sqlalchemy.orm import Session
logger = logging.getLogger(__name__)
@dataclass(frozen=True, slots=True)
class Brand:
slug: str
name: str
logo_url: str | None
primary_color: str
accent_color: str
footer_text: str | None
pdf_disclaimer: str | None
_DEFAULT = Brand(
slug="generic",
name="МЕРА",
logo_url=None,
primary_color="#1d4ed8",
accent_color="#f59e0b",
footer_text=None,
pdf_disclaimer=None,
)
def get_brand(slug: str | None, db: Session) -> Brand:
"""Возвращает Brand по slug. Fallback на 'generic' если не найден."""
if not slug:
return _DEFAULT
row = db.execute(
text(
"""
SELECT slug, name, logo_url, primary_color, accent_color,
footer_text, pdf_disclaimer
FROM brands
WHERE slug = :slug
"""
),
{"slug": slug.lower()},
).fetchone()
if row is None:
logger.info("brand not found: %s — using generic", slug)
return _DEFAULT
return Brand(
slug=row.slug,
name=row.name,
logo_url=row.logo_url,
primary_color=row.primary_color or _DEFAULT.primary_color,
accent_color=row.accent_color or _DEFAULT.accent_color,
footer_text=row.footer_text,
pdf_disclaimer=row.pdf_disclaimer,
)