Files
remnawave-bedolaga-telegram…/app/handlers/admin/trials.py
T
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

89 lines
2.6 KiB
Python

import structlog
from aiogram import Dispatcher, F, types
from sqlalchemy.ext.asyncio import AsyncSession
from app.database.crud.subscription import (
get_trial_statistics,
reset_trials_for_users_without_paid_subscription,
)
from app.database.models import User
from app.keyboards.admin import get_admin_trials_keyboard
from app.localization.texts import get_texts
from app.utils.decorators import admin_required, error_handler
logger = structlog.get_logger(__name__)
@admin_required
@error_handler
async def show_trials_panel(
callback: types.CallbackQuery,
db_user: User,
db: AsyncSession,
):
texts = get_texts(db_user.language)
stats = await get_trial_statistics(db)
message = (
texts.t('ADMIN_TRIALS_TITLE', '🧪 Управление триалами')
+ '\n\n'
+ texts.t(
'ADMIN_TRIALS_STATS',
'• Использовано всего: {used}\n• Активно сейчас: {active}\n• Доступно к сбросу: {resettable}',
).format(
used=stats.get('used_trials', 0),
active=stats.get('active_trials', 0),
resettable=stats.get('resettable_trials', 0),
)
)
await callback.message.edit_text(
message,
reply_markup=get_admin_trials_keyboard(db_user.language),
)
await callback.answer()
@admin_required
@error_handler
async def reset_trials(
callback: types.CallbackQuery,
db_user: User,
db: AsyncSession,
):
texts = get_texts(db_user.language)
reset_count = await reset_trials_for_users_without_paid_subscription(db)
stats = await get_trial_statistics(db)
message = texts.t(
'ADMIN_TRIALS_RESET_RESULT',
'♻️ Сбросили {reset_count} триалов.\n\n'
'• Использовано всего: {used}\n'
'• Активно сейчас: {active}\n'
'• Доступно к сбросу: {resettable}',
).format(
reset_count=reset_count,
used=stats.get('used_trials', 0),
active=stats.get('active_trials', 0),
resettable=stats.get('resettable_trials', 0),
)
await callback.message.edit_text(
message,
reply_markup=get_admin_trials_keyboard(db_user.language),
)
await callback.answer(texts.t('ADMIN_TRIALS_RESET_TOAST', '✅ Сброс завершен'))
def register_handlers(dp: Dispatcher) -> None:
dp.callback_query.register(
show_trials_panel,
F.data == 'admin_trials',
)
dp.callback_query.register(
reset_trials,
F.data == 'admin_trials_reset',
)