Files
remnawave-bedolaga-telegram…/app/services/subscription_checkout_service.py
c0mrade 9a2aea038a chore: add uv package manager and ruff linter configuration
- Add pyproject.toml with uv and ruff configuration
- Pin Python version to 3.13 via .python-version
- Add Makefile commands: lint, format, fix
- Apply ruff formatting to entire codebase
- Remove unused imports (base64 in yookassa/simple_subscription)
- Update .gitignore for new config files
2026-01-24 17:45:27 +03:00

76 lines
2.2 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.

import logging
from sqlalchemy.exc import MissingGreenlet
from app.database.models import Subscription, User
from app.utils.cache import UserCache
logger = logging.getLogger(__name__)
_CHECKOUT_SESSION_KEY = 'subscription_checkout'
_CHECKOUT_TTL_SECONDS = 3600
async def save_subscription_checkout_draft(user_id: int, data: dict, ttl: int = _CHECKOUT_TTL_SECONDS) -> bool:
"""Persist subscription checkout draft data in cache."""
return await UserCache.set_user_session(user_id, _CHECKOUT_SESSION_KEY, data, ttl)
async def get_subscription_checkout_draft(user_id: int) -> dict | None:
"""Retrieve subscription checkout draft from cache."""
return await UserCache.get_user_session(user_id, _CHECKOUT_SESSION_KEY)
async def clear_subscription_checkout_draft(user_id: int) -> bool:
"""Remove stored subscription checkout draft for the user."""
return await UserCache.delete_user_session(user_id, _CHECKOUT_SESSION_KEY)
async def has_subscription_checkout_draft(user_id: int) -> bool:
draft = await get_subscription_checkout_draft(user_id)
return draft is not None
def should_offer_checkout_resume(
user: User,
has_draft: bool,
*,
subscription: Subscription | None = None,
) -> bool:
"""
Determine whether checkout resume button should be available for the user.
Only users without an active paid subscription or users currently on trial
are eligible to continue assembling the subscription from the saved draft.
"""
if not has_draft:
return False
if subscription is None:
try:
subscription = getattr(user, 'subscription', None)
except MissingGreenlet as error:
logger.warning(
'Не удалось лениво загрузить подписку пользователя %s при проверке возврата к checkout: %s',
getattr(user, 'id', None),
error,
)
subscription = None
if subscription is None:
return True
if getattr(subscription, 'is_trial', False):
return True
if getattr(subscription, 'actual_status', None) == 'expired':
return True
return False