Files
remnawave-bedolaga-telegram…/app/database/crud/squad.py
Fringg 1f0fef114b refactor: complete structlog migration with contextvars, kwargs, and logging hardening
- Add ContextVarsMiddleware for automatic user_id/chat_id/username binding
  via structlog contextvars (aiogram) and http_method/http_path (FastAPI)
- Use bound_contextvars() context manager instead of clear_contextvars()
  to safely restore previous state instead of wiping all context
- Register ContextVarsMiddleware as outermost middleware (before GlobalError)
  so all error logs include user context
- Replace structlog.get_logger() with structlog.get_logger(__name__) across
  270 calls in 265 files for meaningful logger names
- Switch wrapper_class from BoundLogger to make_filtering_bound_logger()
  for pre-processor level filtering (performance optimization)
- Migrate 1411 %-style positional arg logger calls to structlog kwargs
  style across 161 files via AST script
- Migrate log_rotation_service.py from stdlib logging to structlog
- Add payment module prefixes to TelegramNotifierProcessor.IGNORED_LOGGER_PREFIXES
  and ExcludePaymentFilter.PAYMENT_MODULES to prevent payment data leaking
  to Telegram notifications and general log files
- Fix LoggingMiddleware: add from_user null-safety for channel posts,
  switch time.time() to time.monotonic() for duration measurement
- Remove duplicate logger assignments in purchase.py, config.py,
  inline.py, and admin/payments.py
2026-02-16 09:18:12 +03:00

43 lines
1.2 KiB
Python

import structlog
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.database.models import Squad
logger = structlog.get_logger(__name__)
async def get_squad_by_uuid(db: AsyncSession, uuid: str) -> Squad | None:
result = await db.execute(select(Squad).where(Squad.uuid == uuid))
return result.scalar_one_or_none()
async def get_available_squads(db: AsyncSession) -> list[Squad]:
result = await db.execute(select(Squad).where(Squad.is_available == True))
return result.scalars().all()
async def create_squad(
db: AsyncSession, uuid: str, name: str, country_code: str = None, price_kopeks: int = 0, description: str = None
) -> Squad:
squad = Squad(uuid=uuid, name=name, country_code=country_code, price_kopeks=price_kopeks, description=description)
db.add(squad)
await db.commit()
await db.refresh(squad)
logger.info('✅ Создан сквад', name=name)
return squad
async def update_squad(db: AsyncSession, squad: Squad, **kwargs) -> Squad:
for field, value in kwargs.items():
if hasattr(squad, field):
setattr(squad, field, value)
await db.commit()
await db.refresh(squad)
return squad