72b6879f4b
- Refactored max_bot from nested packages to flat module structure - Q2: Extended BotUser model (patronymic, email, org, address, vcf_raw, contact_hash, phone_verified, email_verified, last_interaction, total_conversations, total_tickets) - Q2: VCF parser (FN, N, TEL, EMAIL, ORG, ADR), upsert on re-contact, NLP history context (_get_user_history_context -> YandexGPT) - Q1: Broadcast preview modal with 10s confirmation timer - Q3: CSS var(--white)->var(--bg-card), var(--text)->var(--text-primary) - Q4: bot_settings showNotification(), editable max_bot_id - Q5: Webhook secret passthrough via X-Max-Bot-Api-Secret - Masking sensitive keys, dialog_cleared handler, migrate via _add_column_if_not_exists() - Rate limit (asyncio.sleep 0.5 per 10), dead code removed, conv.intent context in contact.py - Portal pages: bot_consent, bot_kb (edit), bot_settings, bot_test, bot_tickets, portal_settings - Tests: 21/21 passing, added test_yandex_gpt.py, test_email_sender.py - Deploy: deploy_full.sh, schema.sql, seed_knowledge_base.sql
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
from typing import Optional, Dict
|
|
from sqlalchemy import select
|
|
from app.database import async_session
|
|
from app.models import BotSetting
|
|
|
|
|
|
class SettingsCache:
|
|
def __init__(self):
|
|
self._cache: dict[str, str] = {}
|
|
self._loaded = False
|
|
|
|
async def _load(self):
|
|
async with async_session() as db:
|
|
result = await db.execute(select(BotSetting))
|
|
rows = result.scalars().all()
|
|
self._cache = {row.key: row.value or "" for row in rows}
|
|
self._loaded = True
|
|
|
|
async def get(self, key: str, default: str = "") -> str:
|
|
if not self._loaded:
|
|
await self._load()
|
|
return self._cache.get(key, default)
|
|
|
|
async def set(self, key: str, value: str) -> bool:
|
|
async with async_session() as db:
|
|
result = await db.execute(select(BotSetting).where(BotSetting.key == key))
|
|
setting = result.scalar_one_or_none()
|
|
if setting:
|
|
setting.value = value
|
|
else:
|
|
db.add(BotSetting(key=key, value=value))
|
|
await db.commit()
|
|
self._cache[key] = value
|
|
return True
|
|
|
|
async def get_all(self) -> Dict[str, str]:
|
|
if not self._loaded:
|
|
await self._load()
|
|
return dict(self._cache)
|
|
|
|
def invalidate(self):
|
|
self._loaded = False
|
|
|
|
|
|
settings_cache = SettingsCache()
|