Spaces:
Sleeping
Sleeping
Upload handlers/admin.py with huggingface_hub
Browse files- handlers/admin.py +77 -0
handlers/admin.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from aiogram.filters import Command
|
| 2 |
+
from aiogram.types import Message, InlineKeyboardButton, InlineKeyboardMarkup
|
| 3 |
+
from aiogram import Router, F
|
| 4 |
+
from database import get_db
|
| 5 |
+
from config import config
|
| 6 |
+
from handlers.user import get_main_menu
|
| 7 |
+
|
| 8 |
+
router = Router()
|
| 9 |
+
|
| 10 |
+
def is_admin(user_id: int) -> bool:
|
| 11 |
+
return user_id in config.ADMIN_IDS
|
| 12 |
+
|
| 13 |
+
@router.message(Command("admin"))
|
| 14 |
+
async def cmd_admin(message: Message):
|
| 15 |
+
if not is_admin(message.from_user.id):
|
| 16 |
+
await message.answer("β Sizda admin huquqlari yo'q.")
|
| 17 |
+
return
|
| 18 |
+
|
| 19 |
+
keyboard = InlineKeyboardMarkup(
|
| 20 |
+
inline_keyboard=[
|
| 21 |
+
[InlineKeyboardButton(text="π Statistika", callback_data="admin_stats")],
|
| 22 |
+
[InlineKeyboardButton(text="β Mahsulot qo'shish", callback_data="admin_add_product")],
|
| 23 |
+
[InlineKeyboardButton(text="π¦ Mahsulotlar ro'yxati", callback_data="admin_products")],
|
| 24 |
+
[InlineKeyboardButton(text="π Buyurtmalar", callback_data="admin_orders")],
|
| 25 |
+
]
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
await message.answer("π¨βπΌ Admin panel:", reply_markup=keyboard)
|
| 29 |
+
|
| 30 |
+
@router.message(F.text.regexp(r"^/add (.+)$"))
|
| 31 |
+
async def add_product_quick(message: Message, is_admin: bool = False):
|
| 32 |
+
if not is_admin(message.from_user.id):
|
| 33 |
+
return
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
parts = message.text.split("|")
|
| 37 |
+
if len(parts) >= 4:
|
| 38 |
+
name = parts[0].replace("/add ", "")
|
| 39 |
+
price = int(parts[1])
|
| 40 |
+
category = parts[2]
|
| 41 |
+
description = parts[3] if len(parts) > 3 else ""
|
| 42 |
+
|
| 43 |
+
db = get_db()
|
| 44 |
+
await db.products.insert_one({
|
| 45 |
+
"name": name,
|
| 46 |
+
"price": price,
|
| 47 |
+
"category": category,
|
| 48 |
+
"description": description,
|
| 49 |
+
"image": "",
|
| 50 |
+
"sizes": ["S", "M", "L", "XL"],
|
| 51 |
+
"colors": ["Qora", "Oq"],
|
| 52 |
+
"stock": 100
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
await message.answer(f"β
Mahsulot qo'shildi: {name}")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
await message.answer(f"β Xatolik: {e}")
|
| 58 |
+
|
| 59 |
+
async def setup_admin_handlers(router: Router):
|
| 60 |
+
db = get_db()
|
| 61 |
+
|
| 62 |
+
@router.callback_query(F.data == "admin_stats")
|
| 63 |
+
async def admin_stats(callback):
|
| 64 |
+
if not is_admin(callback.from_user.id):
|
| 65 |
+
return
|
| 66 |
+
|
| 67 |
+
total_users = await db.users.count_documents({})
|
| 68 |
+
total_orders = await db.orders.count_documents({})
|
| 69 |
+
total_products = await db.products.count_documents({})
|
| 70 |
+
|
| 71 |
+
text = f"π Statistika:\n\n"
|
| 72 |
+
text += f"Foydalanuvchilar: {total_users}\n"
|
| 73 |
+
text += f"Buyurtmalar: {total_orders}\n"
|
| 74 |
+
text += f"Mahsulotlar: {total_products}"
|
| 75 |
+
|
| 76 |
+
await callback.message.answer(text)
|
| 77 |
+
await callback.answer()
|