fix: format premium traffic limits from bytes
This commit is contained in:
@@ -114,6 +114,20 @@ def _with_subscription_purchase_description(
|
||||
return f"{description}\n\n{text}"
|
||||
|
||||
|
||||
def _format_premium_bytes(value: object) -> str:
|
||||
try:
|
||||
bytes_value = max(0, int(value or 0))
|
||||
except (TypeError, ValueError):
|
||||
bytes_value = 0
|
||||
return f"{bytes_value / 2**30:.2f} GB"
|
||||
|
||||
|
||||
def _format_premium_usage_limit(active: dict[str, object]) -> str:
|
||||
used = _format_premium_bytes(active.get("premium_used_bytes"))
|
||||
limit = _format_premium_bytes(active.get("premium_limit_bytes"))
|
||||
return f"{used} из {limit}"
|
||||
|
||||
|
||||
async def display_subscription_options(
|
||||
event: Union[types.Message, types.CallbackQuery],
|
||||
i18n_data: dict,
|
||||
@@ -456,7 +470,7 @@ async def tariff_topup_list_callback(
|
||||
if len(labels) > len(visible):
|
||||
premium_lines.append(f"• ... еще {len(labels) - len(visible)}")
|
||||
premium_lines.append(
|
||||
f"Premium использовано: {active.get('premium_used')} из {active.get('premium_limit')}. Осталось: {premium_left / 2**30:.2f} GB." # noqa: E501
|
||||
f"Premium использовано: {_format_premium_usage_limit(active)}. Осталось: {premium_left / 2**30:.2f} GB." # noqa: E501
|
||||
)
|
||||
text = get_text("choose_payment_method_traffic")
|
||||
if carryover_lines:
|
||||
@@ -1065,7 +1079,7 @@ async def my_subscription_command_handler(
|
||||
text += (
|
||||
"\n\n🚀 <b>Premium-серверы</b>\n"
|
||||
f"Статус: <b>{premium_status}</b>\n"
|
||||
f"Лимит: <b>{active.get('premium_used')} из {active.get('premium_limit')}</b>\n"
|
||||
f"Лимит: <b>{_format_premium_usage_limit(active)}</b>\n"
|
||||
f"Осталось: <b>{premium_left / 2**30:.2f} GB</b>\n"
|
||||
f"Докупленный остаток: <b>{premium_balance / 2**30:.2f} GB</b>\n"
|
||||
"Отдельный лимит действует на:\n"
|
||||
|
||||
@@ -54,6 +54,37 @@ class SubscriptionLifecycleMixin:
|
||||
reason = f"{reason} message={message}"
|
||||
return reason
|
||||
|
||||
@staticmethod
|
||||
def _display_datetime_text(value: Optional[Any]) -> Optional[str]:
|
||||
if not value:
|
||||
return None
|
||||
if isinstance(value, datetime):
|
||||
normalized = value if value.tzinfo else value.replace(tzinfo=timezone.utc)
|
||||
return normalized.strftime("%d.%m.%Y %H:%M")
|
||||
return str(value)
|
||||
|
||||
@staticmethod
|
||||
def _device_topup_renewal_available(
|
||||
extra_hwid_devices: int,
|
||||
extra_hwid_valid_until: Optional[Any],
|
||||
subscription_end_date: Optional[Any],
|
||||
) -> bool:
|
||||
if not isinstance(extra_hwid_valid_until, datetime) or not isinstance(
|
||||
subscription_end_date, datetime
|
||||
):
|
||||
return False
|
||||
valid_until = (
|
||||
extra_hwid_valid_until
|
||||
if extra_hwid_valid_until.tzinfo
|
||||
else extra_hwid_valid_until.replace(tzinfo=timezone.utc)
|
||||
)
|
||||
end_date = (
|
||||
subscription_end_date
|
||||
if subscription_end_date.tzinfo
|
||||
else subscription_end_date.replace(tzinfo=timezone.utc)
|
||||
)
|
||||
return bool(int(extra_hwid_devices or 0) > 0 and valid_until < end_date)
|
||||
|
||||
async def _local_active_subscription_details_fallback(
|
||||
self,
|
||||
db_user: User,
|
||||
@@ -130,7 +161,9 @@ class SubscriptionLifecycleMixin:
|
||||
"base_hwid_device_limit": local_active_sub.hwid_device_limit,
|
||||
"extra_hwid_devices": int(local_active_sub.extra_hwid_devices or 0),
|
||||
"extra_hwid_devices_valid_until": None,
|
||||
"extra_hwid_devices_valid_until_text": None,
|
||||
"extra_hwid_devices_next_valid_from": None,
|
||||
"device_topup_renewal_available": False,
|
||||
"user_bot_username": db_user.username,
|
||||
"is_panel_data": False,
|
||||
"max_devices": self._effective_hwid_limit(
|
||||
@@ -1011,6 +1044,14 @@ class SubscriptionLifecycleMixin:
|
||||
if expected_hwid_limit is not None:
|
||||
hwid_limit = expected_hwid_limit
|
||||
|
||||
extra_hwid_valid_until = hwid_entitlement_summary.get("active_until")
|
||||
extra_hwid_next_valid_from = hwid_entitlement_summary.get("next_valid_from")
|
||||
device_topup_renewal_available = self._device_topup_renewal_available(
|
||||
active_extra_hwid_devices,
|
||||
extra_hwid_valid_until,
|
||||
panel_end_date,
|
||||
)
|
||||
|
||||
return {
|
||||
"user_id": panel_user_data.get("uuid"),
|
||||
"panel_subscription_uuid": panel_user_data.get("subscriptionUuid")
|
||||
@@ -1071,8 +1112,12 @@ class SubscriptionLifecycleMixin:
|
||||
if local_active_sub
|
||||
else None,
|
||||
"extra_hwid_devices": active_extra_hwid_devices,
|
||||
"extra_hwid_devices_valid_until": hwid_entitlement_summary.get("active_until"),
|
||||
"extra_hwid_devices_next_valid_from": hwid_entitlement_summary.get("next_valid_from"),
|
||||
"extra_hwid_devices_valid_until": extra_hwid_valid_until,
|
||||
"extra_hwid_devices_valid_until_text": self._display_datetime_text(
|
||||
extra_hwid_valid_until
|
||||
),
|
||||
"extra_hwid_devices_next_valid_from": extra_hwid_next_valid_from,
|
||||
"device_topup_renewal_available": device_topup_renewal_available,
|
||||
"user_bot_username": db_user.username,
|
||||
"is_panel_data": True,
|
||||
"max_devices": hwid_limit,
|
||||
|
||||
Reference in New Issue
Block a user