gitscope
https://dev.kronoz.win/r/marzneshin.git
app/notification/helper.py · v0.7.2
Source
1 8440155 mahdim10 1y ago from datetime import datetime
2 8440155 mahdim10 1y ago from typing import Dict, Optional
3 8440155 mahdim10 1y ago
4 8440155 mahdim10 1y ago from aiogram import html
5 8440155 mahdim10 1y ago
6 8440155 mahdim10 1y ago from app.models.admin import Admin
7 8440155 mahdim10 1y ago from app.models.notification import Notification, UserNotification
8 8440155 mahdim10 1y ago from app.models.user import UserResponse
9 8440155 mahdim10 1y ago from app.utils.system import readable_size
10 8440155 mahdim10 1y ago
11 8440155 mahdim10 1y ago
12 8440155 mahdim10 1y ago def create_text(notif: Notification) -> str:
13 8440155 mahdim10 1y ago data = prepare_data(notif)
14 8440155 mahdim10 1y ago A = UserNotification.Action
15 8440155 mahdim10 1y ago
16 8440155 mahdim10 1y ago texts: Dict[UserNotification.Action, str] = {
17 8440155 mahdim10 1y ago A.user_created: "🆕 <b>#Created</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username :</b> <code>{username}</code>\n<b>Traffic Limit :</b> <code>{data_limit}</code>\n<b>Expire Date :</b> <code>{expire_date}</code>\n<b>Services :</b> <code>{services}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
18 8440155 mahdim10 1y ago A.user_updated: "✏️ <b>#Modified</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username :</b> <code>{username}</code>\n<b>Traffic Limit :</b> <code>{data_limit}</code>\n<b>Expire Date :</b> <code>{expire_date}</code>\n<b>Services :</b> <code>{services}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
19 8440155 mahdim10 1y ago A.user_activated: "✅ <b>#Activated</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Belongs To :</b> <code>{owner_username}</code>",
20 8440155 mahdim10 1y ago A.user_deactivated: "❌ <b>#Deactivated</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Belongs To :</b> <code>{owner_username}</code>",
21 8440155 mahdim10 1y ago A.user_deleted: "🗑 <b>#Deleted</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
22 8440155 mahdim10 1y ago A.user_enabled: "☑️ <b>#Enabled</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
23 8440155 mahdim10 1y ago A.user_disabled: "🛑 <b>#Disabled</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
24 8440155 mahdim10 1y ago A.data_usage_reset: "🔁 <b>#Reset</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>By</b> : <b>#{by}</b>",
25 8440155 mahdim10 1y ago A.subscription_revoked: "🔁 <b>#Revoked</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>By</b> : <b>#{by}</b>",
26 8440155 mahdim10 1y ago A.reached_usage_percent: "⚠️<b>#DataLimitWarning</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Used Percent</b> : <code>{usage_percent}</code>\n<b>Remaining Traffic</b> : <code>{remaining_traffic}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>",
27 8440155 mahdim10 1y ago A.reached_days_left: "⚠️<b>#ExpirationWarning</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Remaining Days</b> : <code>{remaining_days}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>",
28 8440155 mahdim10 1y ago }
29 8440155 mahdim10 1y ago
30 8440155 mahdim10 1y ago if notif.action == A.user_deactivated and not notif.by:
31 8440155 mahdim10 1y ago if notif.user.expired:
32 8440155 mahdim10 1y ago texts[A.user_deactivated] = (
33 8440155 mahdim10 1y ago "🕔 <b>#Expired</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Belongs To :</b> <code>{owner_username}</code>"
34 8440155 mahdim10 1y ago )
35 8440155 mahdim10 1y ago elif notif.user.data_limit_reached:
36 8440155 mahdim10 1y ago texts[A.user_deactivated] = (
37 8440155 mahdim10 1y ago "🪫 <b>#Limited</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Belongs To :</b> <code>{owner_username}</code>"
38 8440155 mahdim10 1y ago )
39 8440155 mahdim10 1y ago
40 8440155 mahdim10 1y ago text = texts.get(notif.action)
41 8440155 mahdim10 1y ago formatted_message = text.format_map(data)
42 8440155 mahdim10 1y ago
43 8440155 mahdim10 1y ago return formatted_message
44 8440155 mahdim10 1y ago
45 8440155 mahdim10 1y ago
46 8440155 mahdim10 1y ago def prepare_data(notif: Notification) -> dict:
47 8440155 mahdim10 1y ago user = UserResponse.model_validate(notif.user)
48 8440155 mahdim10 1y ago by: Optional[Admin] = (
49 8440155 mahdim10 1y ago Admin.model_validate(notif.by)
50 8440155 mahdim10 1y ago if hasattr(notif, "by") and notif.by
51 8440155 mahdim10 1y ago else None
52 8440155 mahdim10 1y ago )
53 8440155 mahdim10 1y ago data = {
54 8440155 mahdim10 1y ago "username": html.quote(user.username),
55 8440155 mahdim10 1y ago "data_limit": (
56 8440155 mahdim10 1y ago readable_size(user.data_limit) if user.data_limit else "Unlimited"
57 8440155 mahdim10 1y ago ),
58 8440155 mahdim10 1y ago "remaining_traffic": (
59 8440155 mahdim10 1y ago readable_size(max(user.data_limit - user.used_traffic, 0))
60 8440155 mahdim10 1y ago if user.data_limit
61 8440155 mahdim10 1y ago else "Unlimited"
62 8440155 mahdim10 1y ago ),
63 8440155 mahdim10 1y ago "usage_percent": (
64 8440155 mahdim10 1y ago f"{round(min((user.used_traffic / user.data_limit) * 100, 100),2)}%"
65 8440155 mahdim10 1y ago if isinstance(user.data_limit, int) and user.data_limit > 0
66 8440155 mahdim10 1y ago else "0%"
67 8440155 mahdim10 1y ago ),
68 8440155 mahdim10 1y ago "expire_date": (
69 8440155 mahdim10 1y ago user.expire_date.strftime("%H:%M:%S %Y-%m-%d")
70 8440155 mahdim10 1y ago if user.expire_date
71 8440155 mahdim10 1y ago else "Never"
72 8440155 mahdim10 1y ago ),
73 8440155 mahdim10 1y ago "remaining_days": (
74 8440155 mahdim10 1y ago (user.expire_date - datetime.now()).days
75 8440155 mahdim10 1y ago if user.expire_date
76 8440155 mahdim10 1y ago else "Never"
77 8440155 mahdim10 1y ago ),
78 8440155 mahdim10 1y ago "services": user.service_ids if user.service_ids else "",
79 8440155 mahdim10 1y ago "owner_username": user.owner_username,
80 8440155 mahdim10 1y ago "by": html.quote(by.username) if by else None,
81 8440155 mahdim10 1y ago }
82 8440155 mahdim10 1y ago
83 8440155 mahdim10 1y ago return data