-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_panel.py
220 lines (183 loc) · 8.92 KB
/
admin_panel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from asyncio import sleep
from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from config import admin_id
from load_all import dp, _, bot
from states import NewItem, Mailing, Confirming,Edit
from database import Item, User, Purchase
import database
db = database.DBCommands()
@dp.message_handler(user_id=admin_id, commands=["cancel"], state=NewItem)
async def cancel(message: types.Message, state: FSMContext):
await message.answer(_("Вы отменили создание услуги"))
await state.reset_state()
@dp.message_handler(user_id=admin_id, commands=["cancel"], state=Mailing)
async def cancel(message: types.Message, state: FSMContext):
await message.answer(_("Вы отменили рассылку"))
await state.reset_state()
@dp.message_handler(user_id=admin_id, commands=["cancel"], state=Confirming)
async def cancel(message: types.Message, state: FSMContext):
await message.answer(_("Вы вышли из панели просмотра заявок"))
await state.reset_state()
@dp.callback_query_handler(user_id=admin_id,text_contains="add_item")
async def add_item(call:types.CallbackQuery):
await bot.send_message(chat_id = admin_id, text= _("Введите название услуги или введите /cancel"))
await NewItem.Name.set()
@dp.message_handler(user_id=admin_id, state=NewItem.Name)
async def enter_name(message: types.Message, state: FSMContext):
name = message.text
item = Item()
item.name = name
await message.answer(_("Введите описание услуги или введите /cancel"))
await NewItem.Description.set()
await state.update_data(item=item)
@dp.message_handler(user_id=admin_id, state=NewItem.Description)
async def set_desc(message: types.Message,state:FSMContext):
description = message.text
data = await state.get_data()
item: Item = data.get("item")
item.description = description
await message.answer(_("\nЗагрузите фотографию или введите /cancel"))
await NewItem.Photo.set()
await state.update_data(item=item)
@dp.message_handler(user_id=admin_id, state=NewItem.Photo, content_types=types.ContentType.PHOTO)
async def add_photo(message: types.Message, state: FSMContext):
photo = message.photo[-1].file_id
data = await state.get_data()
item: Item = data.get("item")
item.photo = photo
await message.answer_photo(
photo=photo,
caption=_(
"\nПришлите мне цену товара в копейках или введите /cancel"))
await NewItem.Price.set()
await state.update_data(item=item)
@dp.message_handler(user_id=admin_id, state=NewItem.Price)
async def enter_price(message: types.Message, state: FSMContext):
data = await state.get_data()
item: Item = data.get("item")
try:
price = int(message.text)
except ValueError:
await message.answer(_("Неверное значение, введите число"))
return
item.price = price
markup = InlineKeyboardMarkup(
inline_keyboard=
[
[InlineKeyboardButton(text=_("Да"), callback_data="confirm")],
[InlineKeyboardButton(text=_("Ввести заново"), callback_data="change")],
]
)
await message.answer(_("Цена: {price:,}0\n"
).format(price=price / 100),
reply_markup=markup)
await state.update_data(item=item)
await NewItem.Confirm.set()
@dp.callback_query_handler(user_id=admin_id, text_contains="change", state=NewItem.Confirm)
async def enter_price(call: types.CallbackQuery):
await call.message.edit_reply_markup()
await call.message.answer(_("Введите заново цену услуги в копейках"))
await NewItem.Price.set()
@dp.callback_query_handler(user_id=admin_id, text_contains="confirm", state=NewItem.Confirm)
async def enter_price(call: types.CallbackQuery, state: FSMContext):
await call.message.edit_reply_markup()
data = await state.get_data()
item: Item = data.get("item")
await item.create()
await call.message.answer(_("Услуга удачно создана."))
await state.reset_state()
# @dp.message_handler(user_id = admin_id, text_contains = '/add_admin')
# async def add_admin(message: Message):
# Фича для рассылки по юзерам (учитывая их язык)
@dp.callback_query_handler(user_id=admin_id, text_contains = "tell_everyone")
async def mailing(call: types.CallbackQuery):
await bot.send_message(chat_id = admin_id, text=_("Пришлите текст рассылки"))
await call.message.edit_reply_markup()
await Mailing.Text.set()
@dp.message_handler(user_id=admin_id, state=Mailing.Text)
async def mailing(message: types.Message, state: FSMContext):
text = message.text
await state.update_data(text=text)
markup = InlineKeyboardMarkup(
inline_keyboard=
[
[InlineKeyboardButton(text="Русский", callback_data="ru")],
[InlineKeyboardButton(text="English", callback_data="en")],
[InlineKeyboardButton(text="Україньска", callback_data="uk")],
]
)
await message.answer(_("\n"
"Текст вашей рассылки:\n"
"{text}").format(text=text),
reply_markup=markup)
await Mailing.Language.set()
@dp.callback_query_handler(user_id=admin_id, state=Mailing.Language)
async def mailing_start(call: types.CallbackQuery, state: FSMContext):
data = await state.get_data()
text = data.get("text")
await state.reset_state()
await call.message.edit_reply_markup()
users = await User.query.where(User.language == call.data).gino.all()
for user in users:
try:
await bot.send_message(chat_id=user.user_id,
text=text)
await sleep(0.3)
except Exception:
pass
await call.message.answer(_("Рассылка выполнена."))
@dp.callback_query_handler(user_id=admin_id,text_contains = "show_orders")
async def show_orders(call:types.CallbackQuery, state:FSMContext):
await call.message.edit_reply_markup()
await bot.send_message(chat_id = admin_id, text = "Актуальные заявки:")
all_orders, all_items = await db.show_orders()
orders_markup = InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(text=_("Бронь"),callback_data="confirm"),
]
])
for num, order in enumerate(all_orders):
confirm = order
# if not order.delivered and (order.successful == True):
text = _("<b>ID заказа:</b> \t <u>{id}</u>\n"
"<b>ID Комнаты:</b> \t <u>{item_id}</u>\n"
"<b>Название:</b> \t <u>{item_name}</u>\n"
"<b>Имя Клиента:</b> \t <u>{name}</u>\n"
"<b>Итоговый чек:</b> \t{amount:,}RUB\n"
"<b>E-mail: \t<u>{email}</u></b>\n"
"<b>Номер телефона: \t<u>{phone_number}</u></b>\n"
"<b>Время: \t<u>{purchase_time}</u></b>\n"
"<b>Количество дней: \t<u>{quantity}</u></b>")
for num, occup in enumerate(all_items):
confirm = occup
await bot.send_message(chat_id = admin_id, text = text.format(
id = order.id,
item_id = order.item_id,
item_name=order.item_name,
name= order.receiver,
amount=order.amount / 100,
email=order.email,
phone_number = order.phone_number,
purchase_time = order.purchase_time,
quantity = order.quantity
), reply_markup=orders_markup)
await bot.send_message(chat_id = admin_id, text = "Чтобы вернуться введите /cancel")
await state.update_data(confirm1=occup)
await Confirming.Confirm1.set()
@dp.callback_query_handler(text_contains = "confirm", state=Confirming.Confirm1)
async def confirm_order(call: types.CallbackQuery,state: FSMContext):
await call.message.edit_reply_markup()
data = await state.get_data()
confirm1 = data.get("confirm1")
if confirm1.occupied == False:
await confirm1.update(occupied=True).apply()
text = _("Комната с номером <b>{id}</b> отмечена как забронированная\n")
await state.finish()
else:
await confirm1.update(occupied=False).apply()
text = _("Комната с номером <b>{id}</b> отмечена как свободная\n")
await state.finish()
await bot.send_message(text = text.format(id = confirm1.id),chat_id = admin_id)