Best Python code snippet using green
keyboards.py
Source:keyboards.py
1from aiogram import types2from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton3from aiogram.utils.callback_data import CallbackData4from data.buttons import *5from data.messages import CREATE_REVIEW_MESSAGE, REVIEW_MESSAGE6from db.models import TelegramUser, Category, Shop, CategoryShop, Product, ServiceCategory, Review, Form, Service7start_callback = CallbackData("main_menu", 'select')8admin_callback = CallbackData("admin", 'action', 'param')9seller_callback = CallbackData("seller", 'action', 'shop')10back_to_main_menu_keyboard = InlineKeyboardMarkup(11 inline_keyboard=[12 [13 InlineKeyboardButton(text=MAIN_MENU_BUTTON, callback_data=start_callback.new(select='main')),14 ]15 ]16)17support_keyboard = InlineKeyboardMarkup(18 inline_keyboard=[19 [20 InlineKeyboardButton(text=ADMIN_MESSAGE_BUTTON, callback_data=start_callback.new(select='admin_message')),21 ],22 [23 InlineKeyboardButton(text=MAIN_MENU_BUTTON, callback_data=start_callback.new(select='main'))24 ]25 ]26)27create_type_shop_keyboard = InlineKeyboardMarkup(28 inline_keyboard=[29 [30 InlineKeyboardButton(text=CREATE_CATALOG_SHOP_BUTTON, callback_data=seller_callback.new(31 action='create_catalog', shop=''32 )),33 InlineKeyboardButton(text=CREATE_BID_SHOP_BUTTON, callback_data=seller_callback.new(34 action='create_bid', shop=''35 ))36 ],37 [38 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(39 action='main', shop=''40 ))41 ]42 ]43)44seller_main_menu_keyboard = InlineKeyboardMarkup(45 inline_keyboard=[46 [47 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(48 action='main', shop=''49 ))50 ]51 ]52)53def get_reviews_keyboard(product: Product, review: Review):54 keyboard = InlineKeyboardMarkup(55 inline_keyboard=[56 [57 InlineKeyboardButton(text='<', callback_data=start_callback.new(58 select=f'{review.id}_>reviews_prod_{product.id}'59 )),60 InlineKeyboardButton(text='>', callback_data=start_callback.new(61 select=f'{review.id}_<reviews_prod_{product.id}'62 ))63 ],64 [65 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(66 select=f'shop_prod_{product.id}'67 ))68 ]69 ]70 )71 return keyboard72def get_back_to_prod_keyboard(product: Product):73 keyboard = InlineKeyboardMarkup(74 inline_keyboard=[75 [76 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(77 select=f'shop_prod_{product.id}'78 ))79 ]80 ]81 )82 return keyboard83def get_review_keyboard(product: Product):84 keyboard = InlineKeyboardMarkup(85 inline_keyboard=[86 [87 InlineKeyboardButton(text='1', callback_data=start_callback.new(88 select=f'rate_prod_1_{product.id}'89 )),90 InlineKeyboardButton(text='2', callback_data=start_callback.new(91 select=f'rate_prod_2_{product.id}'92 )),93 InlineKeyboardButton(text='3', callback_data=start_callback.new(94 select=f'rate_prod_3_{product.id}'95 )),96 InlineKeyboardButton(text='4', callback_data=start_callback.new(97 select=f'rate_prod_4_{product.id}'98 )),99 InlineKeyboardButton(text='5', callback_data=start_callback.new(100 select=f'rate_prod_5_{product.id}'101 )),102 ],103 [104 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(105 select=f'shop_prod_{product.id}'106 ))107 ]108 ]109 )110 return keyboard111def get_form_shop_keyboard(shop: Shop):112 keyboard = InlineKeyboardMarkup(113 inline_keyboard=[114 [115 InlineKeyboardButton(text=FREE_DEAL_BUTTON, callback_data=start_callback.new(116 select=f'shop_free_{shop.id}'117 )),118 InlineKeyboardButton(text=FORM_DEAL_BUTTON, callback_data=start_callback.new(119 select=f'shop_field_{shop.id}'120 ))121 ],122 [123 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(select=f'shop_{shop.id}'))124 ]125 ]126 )127 return keyboard128def get_back_service_keyboard(service: Service):129 keyboard = InlineKeyboardMarkup(130 inline_keyboard=[131 [132 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(select=f'subservice_{service.id}'))133 ]134 ]135 )136 return keyboard137def get_back_shop_keyboard(shop: Shop):138 keyboard = InlineKeyboardMarkup(139 inline_keyboard=[140 [141 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(select=f'shop_{shop.id}'))142 ]143 ]144 )145 return keyboard146async def get_prod_keyboard(product: Product):147 category = await product.category148 keyboard = InlineKeyboardMarkup(149 inline_keyboard=[150 [151 InlineKeyboardButton(text=DEAL_BUTTON, callback_data=start_callback.new(152 select=f'deal_prod_{product.id}'153 )),154 ],155 [156 InlineKeyboardButton(text=REVIEWS_BUTTON, callback_data=start_callback.new(157 select=f'reviews_prod_{product.id}'158 )),159 InlineKeyboardButton(text=REVIEW_BUTTON, callback_data=start_callback.new(160 select=f'review_prod_{product.id}'161 )),162 ],163 [164 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(select=f'shop_cat_{category.id}'))165 ]166 ]167 )168 return keyboard169async def get_shop_keyboard(shop: Shop):170 category = await shop.category171 if shop.catalog:172 text = CATEGORIES_BUTTON173 select = 'shop_cats_'174 else:175 text = DEAL_BUTTON176 select = 'shop_deal_'177 keyboard = InlineKeyboardMarkup(178 inline_keyboard=[179 [180 InlineKeyboardButton(text=text, callback_data=start_callback.new(181 select=f'{select}{shop.id}'182 )),183 ],184 [185 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(select=f'cat_{category.id}'))186 ]187 ]188 )189 return keyboard190async def get_shops_cats_keyboard(shop: Shop):191 categories = await shop.categories.all()192 inline_keyboard = []193 for i in range(0, len(categories), 2):194 if i != len(categories) - 1:195 inline_keyboard.append(196 [197 InlineKeyboardButton(text=categories[i].name, callback_data=start_callback.new(198 select=f'shop_cat_{categories[i].id}'199 )),200 InlineKeyboardButton(text=categories[i + 1].name, callback_data=start_callback.new(201 select=f'shop_cat_{categories[i + 1].id}'202 )),203 ]204 )205 else:206 inline_keyboard.append(207 [208 InlineKeyboardButton(text=categories[i].name, callback_data=start_callback.new(209 select=f'shop_cat_{categories[i].id}'210 ))211 ]212 )213 inline_keyboard.append(214 [215 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(216 select=f'shop_{shop.id}'217 ))218 ]219 )220 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)221 return keyboard222async def get_service_keyboard(service: Service):223 category = await service.service_category224 keyboard = InlineKeyboardMarkup(225 inline_keyboard=[226 [227 InlineKeyboardButton(text=DEAL_BUTTON, callback_data=start_callback.new(228 select=f'deal_service_{service.id}'229 )),230 ],231 [232 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(select=f'service_{category.id}'))233 ]234 ]235 )236 return keyboard237async def get_services_keyboard(category: ServiceCategory):238 services = await category.services.all()239 inline_keyboard = []240 for i in range(0, len(services), 2):241 if i != len(services) - 1:242 inline_keyboard.append(243 [244 InlineKeyboardButton(text=services[i].name, callback_data=start_callback.new(245 select=f'subservice_{services[i].id}'246 )),247 InlineKeyboardButton(text=services[i + 1].name, callback_data=start_callback.new(248 select=f'subservice_{services[i + 1].id}'249 )),250 ]251 )252 else:253 inline_keyboard.append(254 [255 InlineKeyboardButton(text=services[i].name, callback_data=start_callback.new(256 select=f'subservice_{services[i].id}'257 ))258 ]259 )260 inline_keyboard.append(261 [262 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(263 select=f'services'264 ))265 ]266 )267 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)268 return keyboard269async def get_shops_prods_keyboard(category: CategoryShop):270 prods = await category.products.all()271 shop = await category.shop272 inline_keyboard = []273 for i in range(0, len(prods), 2):274 if i != len(prods) - 1:275 inline_keyboard.append(276 [277 InlineKeyboardButton(text=prods[i].name, callback_data=start_callback.new(278 select=f'shop_prod_{prods[i].id}'279 )),280 InlineKeyboardButton(text=prods[i + 1].name, callback_data=start_callback.new(281 select=f'shop_prod_{prods[i + 1].id}'282 )),283 ]284 )285 else:286 inline_keyboard.append(287 [288 InlineKeyboardButton(text=prods[i].name, callback_data=start_callback.new(289 select=f'shop_prod_{prods[i].id}'290 ))291 ]292 )293 inline_keyboard.append(294 [295 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(296 select=f'shop_cats_{shop.id}'297 ))298 ]299 )300 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)301 return keyboard302async def get_shops_keyboard(category: Category):303 shops = await Shop.filter(category=category)304 inline_keyboard = []305 for i in range(0, len(shops), 2):306 if i != len(shops) - 1:307 inline_keyboard.append(308 [309 InlineKeyboardButton(text=shops[i].name, callback_data=start_callback.new(310 select=f'shop_{shops[i].id}'311 )),312 InlineKeyboardButton(text=shops[i + 1].name, callback_data=start_callback.new(313 select=f'shop_{shops[i + 1].id}'314 )),315 ]316 )317 else:318 inline_keyboard.append(319 [320 InlineKeyboardButton(text=shops[i].name, callback_data=start_callback.new(321 select=f'shop_{shops[i].id}'322 ))323 ]324 )325 inline_keyboard.append(326 [327 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(328 select='shops'329 ))330 ]331 )332 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)333 return keyboard334async def get_categories_keyboard():335 categories = await Category.all()336 inline_keyboard = []337 for i in range(0, len(categories), 2):338 if i != len(categories) - 1:339 inline_keyboard.append(340 [341 InlineKeyboardButton(text=categories[i].name, callback_data=start_callback.new(342 select=f'cat_{categories[i].id}'343 )),344 InlineKeyboardButton(text=categories[i + 1].name, callback_data=start_callback.new(345 select=f'cat_{categories[i + 1].id}'346 )),347 ]348 )349 else:350 inline_keyboard.append(351 [352 InlineKeyboardButton(text=categories[i].name, callback_data=start_callback.new(353 select=f'cat_{categories[i].id}'354 ))355 ]356 )357 inline_keyboard.append(358 [359 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(360 select='main'361 ))362 ]363 )364 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)365 return keyboard366async def get_service_categories_keyboard():367 categories = await ServiceCategory.all()368 inline_keyboard = []369 for i in range(0, len(categories), 2):370 if i != len(categories) - 1:371 inline_keyboard.append(372 [373 InlineKeyboardButton(text=categories[i].name, callback_data=start_callback.new(374 select=f'service_{categories[i].id}'375 )),376 InlineKeyboardButton(text=categories[i + 1].name, callback_data=start_callback.new(377 select=f'service_{categories[i + 1].id}'378 )),379 ]380 )381 else:382 inline_keyboard.append(383 [384 InlineKeyboardButton(text=categories[i].name, callback_data=start_callback.new(385 select=f'service_{categories[i].id}'386 ))387 ]388 )389 inline_keyboard.append(390 [391 InlineKeyboardButton(text=BACK_BUTTON, callback_data=start_callback.new(392 select='main'393 ))394 ]395 )396 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)397 return keyboard398def get_admin_answer_keyboard(from_user: TelegramUser):399 keyboard = InlineKeyboardMarkup(400 inline_keyboard=[401 [402 InlineKeyboardButton(text=ANSWER_BUTTON, callback_data=admin_callback.new(403 action='answer', param=str(from_user.telegram_id)404 )),405 ]406 ]407 )408 return keyboard409def get_admin_shop_keyboard(shop: Shop):410 keyboard = InlineKeyboardMarkup(411 inline_keyboard=[412 [413 InlineKeyboardButton(text=APPLY_BUTTON, callback_data=admin_callback.new(414 action='apply', param=str(shop.id)415 )),416 InlineKeyboardButton(text=DECLINE_BUTTON, callback_data=admin_callback.new(417 action='decline', param=str(shop.id)418 )),419 ]420 ]421 )422 return keyboard423def get_admin_edit_shop_keyboard(shop: Shop, field: str):424 keyboard = InlineKeyboardMarkup(425 inline_keyboard=[426 [427 InlineKeyboardButton(text=APPLY_BUTTON, callback_data=admin_callback.new(428 action=f'apply_{field}', param=str(shop.id)429 )),430 InlineKeyboardButton(text=DECLINE_BUTTON, callback_data=admin_callback.new(431 action=f'decline_{field}', param=str(shop.id)432 )),433 ]434 ]435 )436 return keyboard437async def get_start_keyboard(user: TelegramUser):438 if not await user.shops.all():439 button = SELLER_BUTTON440 else:441 button = MY_SHOP_BUTTON442 keyboard = InlineKeyboardMarkup(443 inline_keyboard=[444 [445 InlineKeyboardButton(text=SHOPS_BUTTON, callback_data=start_callback.new(select='shops')),446 InlineKeyboardButton(text=SERVICES_BUTTON, callback_data=start_callback.new(select='services')),447 ],448 [449 InlineKeyboardButton(text=button, callback_data=start_callback.new(select='seller')),450 ],451 [452 InlineKeyboardButton(text=ABOUT_BUTTON, callback_data=start_callback.new(select='about')),453 InlineKeyboardButton(text=SUPPORT_BUTTON, callback_data=start_callback.new(select='support')),454 ]455 ]456 )457 return keyboard458def get_go_seller_shop_info_keyboard(shop: Shop):459 keyboard = InlineKeyboardMarkup(460 inline_keyboard=[461 [462 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(463 action='info', shop=str(shop.id)464 ))465 ]466 ]467 )468 return keyboard469def get_confirm_delete_form_keyboard(shop: Shop):470 keyboard = InlineKeyboardMarkup(471 inline_keyboard=[472 [473 InlineKeyboardButton(text=DELETE_BUTTON, callback_data=seller_callback.new(474 action='confirm_delete_form', shop=str(shop.id)475 ))476 ],477 [478 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(479 action='info', shop=str(shop.id)480 ))481 ]482 ]483 )484 return keyboard485def get_create_form_keyboard(shop: Shop, form: Form):486 keyboard = InlineKeyboardMarkup(487 inline_keyboard=[488 [489 InlineKeyboardButton(text=SAVE_FORM_BUTTON, callback_data=seller_callback.new(490 action=f'save_form_{form.id}', shop=str(shop.id)491 ))492 ],493 [494 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(495 action='info', shop=str(shop.id)496 ))497 ]498 ]499 )500 return keyboard501def get_seller_category_keyboard(shop: Shop, category: CategoryShop):502 keyboard = InlineKeyboardMarkup(503 inline_keyboard=[504 [505 InlineKeyboardButton(text=PRODUCTS_BUTTON, callback_data=seller_callback.new(506 action=f'products_{category.id}', shop=str(shop.id)507 ))508 ],509 [510 InlineKeyboardButton(text=EDIT_NAME_BUTTON, callback_data=seller_callback.new(511 action=f'edit_cat_{category.id}', shop=str(shop.id)512 ))513 ],514 [515 InlineKeyboardButton(text=DELETE_CATEGORY_BUTTON, callback_data=seller_callback.new(516 action=f'delete_cat_{category.id}', shop=str(shop.id)517 ))518 ],519 [520 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(521 action='categories', shop=str(shop.id)522 ))523 ]524 ]525 )526 return keyboard527def get_seller_delete_category_keyboard(shop: Shop, category: CategoryShop):528 keyboard = InlineKeyboardMarkup(529 inline_keyboard=[530 [531 InlineKeyboardButton(text=DELETE_BUTTON, callback_data=seller_callback.new(532 action=f'confirm_delete_cat_{category.id}', shop=str(shop.id)533 ))534 ]535 ]536 )537 return keyboard538def get_go_seller_category_keyboard(shop: Shop, category: CategoryShop):539 keyboard = InlineKeyboardMarkup(540 inline_keyboard=[541 [542 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(543 action=f'shop_cat_{category.id}', shop=str(shop.id)544 ))545 ]546 ]547 )548 return keyboard549def get_go_seller_categories_keyboard(shop: Shop):550 keyboard = InlineKeyboardMarkup(551 inline_keyboard=[552 [553 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(554 action='categories', shop=str(shop.id)555 ))556 ]557 ]558 )559 return keyboard560def get_go_seller_products_keyboard(shop: Shop, category: CategoryShop):561 keyboard = InlineKeyboardMarkup(562 inline_keyboard=[563 [564 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(565 action=f'products_{category.id}', shop=str(shop.id)566 ))567 ]568 ]569 )570 return keyboard571def get_go_seller_product_keyboard(product: Product, shop: Shop):572 keyboard = InlineKeyboardMarkup(573 inline_keyboard=[574 [575 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(576 action=f'product_{product.id}', shop=str(shop.id)577 ))578 ]579 ]580 )581 return keyboard582def get_delete_product_keyboard(product: Product, shop: Shop):583 keyboard = InlineKeyboardMarkup(584 inline_keyboard=[585 [586 InlineKeyboardButton(text=DELETE_BUTTON, callback_data=seller_callback.new(587 action=f'confirmdelete_product_{product.id}', shop=str(shop.id)588 ))589 ],590 [591 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(592 action=f'product_{product.id}', shop=str(shop.id)593 ))594 ]595 ]596 )597 return keyboard598def get_seller_add_photo_product_keyboard(product: Product, shop: Shop):599 keyboard = InlineKeyboardMarkup(600 inline_keyboard=[601 [602 InlineKeyboardButton(text=DELETE_BUTTON, callback_data=seller_callback.new(603 action=f'deletephoto_product_{product.id}', shop=str(shop.id)604 ))605 ],606 [607 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(608 action=f'product_{product.id}', shop=str(shop.id)609 ))610 ]611 ]612 )613 return keyboard614def get_seller_shop_delete_keyboard(shop: Shop):615 keyboard = InlineKeyboardMarkup(616 inline_keyboard=[617 [618 InlineKeyboardButton(text=DELETE_BUTTON, callback_data=seller_callback.new(619 action='delete_confirm', shop=str(shop.id)620 ))621 ],622 [623 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(624 action='main', shop=str(shop.id)625 ))626 ]627 ]628 )629 return keyboard630async def get_seller_shop_info_keyboard(shop: Shop):631 inline_keyboard = [632 [633 InlineKeyboardButton(text=EDIT_NAME_BUTTON, callback_data=seller_callback.new(634 action='change_shop_name', shop=str(shop.id)635 )),636 InlineKeyboardButton(text=EDIT_DESCRIPTION_BUTTON, callback_data=seller_callback.new(637 action='change_shop_description', shop=str(shop.id)638 )),639 InlineKeyboardButton(text=EDIT_PHOTO_BUTTON, callback_data=seller_callback.new(640 action='change_shop_photo', shop=str(shop.id)641 )),642 ],643 [644 InlineKeyboardButton(text=DELETE_BUTTON, callback_data=seller_callback.new(645 action='delete', shop=str(shop.id)646 ))647 ],648 [649 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(650 action='main', shop=''651 ))652 ]653 ]654 if shop.catalog:655 inline_keyboard.insert(0, [656 InlineKeyboardButton(text=CATEGORIES_BUTTON, callback_data=seller_callback.new(657 action='categories', shop=str(shop.id)658 )),659 ])660 elif not await shop.form:661 inline_keyboard.insert(0, [662 InlineKeyboardButton(text=CREATE_FORM_BUTTON, callback_data=seller_callback.new(663 action='create_form', shop=str(shop.id)664 )),665 ])666 else:667 inline_keyboard.insert(0, [668 InlineKeyboardButton(text=DELETE_FORM_BUTTON, callback_data=seller_callback.new(669 action='del_form', shop=str(shop.id)670 )),671 ])672 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)673 return keyboard674async def get_seller_product_info_keyboard(product: Product, shop: Shop):675 inline_keyboard = [676 [677 InlineKeyboardButton(text=EDIT_NAME_BUTTON, callback_data=seller_callback.new(678 action=f'name_product_{product.id}', shop=str(shop.id)679 )),680 InlineKeyboardButton(text=EDIT_DESCRIPTION_BUTTON, callback_data=seller_callback.new(681 action=f'description_product_{product.id}', shop=str(shop.id)682 )),683 ],684 [685 InlineKeyboardButton(text=EDIT_PRICE_BUTTON, callback_data=seller_callback.new(686 action=f'price_product_{product.id}', shop=str(shop.id)687 )),688 InlineKeyboardButton(text=EDIT_PHOTO_BUTTON, callback_data=seller_callback.new(689 action=f'photo_product_{product.id}', shop=str(shop.id)690 )),691 ],692 [693 InlineKeyboardButton(text=DELETE_BUTTON, callback_data=seller_callback.new(694 action=f'delete_product_{product.id}', shop=str(shop.id)695 ))696 ],697 [698 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(699 action=f'shop_cat_{(await product.category).id}', shop=str(shop.id)700 ))701 ]702 ]703 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)704 return keyboard705async def get_seller_keyboard(user):706 shops = await user.shops.filter(active=True)707 shops_buttons = [708 [InlineKeyboardButton(709 text=shop.name, callback_data=seller_callback.new(action='info', shop=str(shop.id))710 )] for shop in shops711 ]712 if len(shops_buttons) < 5:713 shops_buttons.append(714 [InlineKeyboardButton(text=OPEN_SHOP_BUTTON, callback_data=seller_callback.new(715 action='open_shop', shop=''716 ))]717 )718 shops_buttons.append(719 [720 InlineKeyboardButton(text=MAIN_MENU_BUTTON, callback_data=start_callback.new(select='main')),721 ]722 )723 keyboard = InlineKeyboardMarkup(inline_keyboard=shops_buttons)724 return keyboard725async def get_seller_products_keyboard(shop: Shop, category: CategoryShop):726 products = await category.products.filter(active=True)727 inline_keyboard = []728 for product in products:729 inline_keyboard.append(730 [731 InlineKeyboardButton(text=product.name, callback_data=seller_callback.new(732 action=f'product_{product.id}', shop=str(shop.id)),733 )734 ]735 )736 if len(products) < 99:737 inline_keyboard.append(738 [InlineKeyboardButton(text=ADD_PRODUCT_BUTTON, callback_data=seller_callback.new(739 action=f'new_product_{category.id}', shop=str(shop.id)740 ))]741 )742 inline_keyboard.append(743 [744 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(745 action=f'shop_cat_{category.id}', shop=str(shop.id)746 ))747 ]748 )749 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)750 return keyboard751async def get_seller_categories_keyboard(shop_id):752 categories = await Category.all()753 inline_keyboard = []754 for i in range(0, len(categories), 2):755 if i != len(categories) - 1:756 inline_keyboard.append(757 [758 InlineKeyboardButton(text=categories[i].name, callback_data=seller_callback.new(759 action=f'select_cat_{categories[i].id}', shop=str(shop_id)760 )),761 InlineKeyboardButton(text=categories[i + 1].name, callback_data=seller_callback.new(762 action=f'select_cat_{categories[i + 1].id}', shop=str(shop_id)763 )),764 ]765 )766 else:767 inline_keyboard.append(768 [769 InlineKeyboardButton(text=categories[i].name, callback_data=seller_callback.new(770 action=f'select_cat_{categories[i].id}', shop=str(shop_id)771 ))772 ]773 )774 inline_keyboard.append(775 [776 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(777 action='main', shop=''778 ))779 ]780 )781 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)782 return keyboard783async def get_seller_shop_categories_keyboard(shop: Shop):784 categories = await shop.categories785 inline_keyboard = []786 for i in range(0, len(categories), 2):787 if i != len(categories) - 1:788 inline_keyboard.append(789 [790 InlineKeyboardButton(text=categories[i].name, callback_data=seller_callback.new(791 action=f'shop_cat_{categories[i].id}', shop=str(shop.id)792 )),793 InlineKeyboardButton(text=categories[i + 1].name, callback_data=seller_callback.new(794 action=f'shop_cat_{categories[i + 1].id}', shop=str(shop.id)795 )),796 ]797 )798 else:799 inline_keyboard.append(800 [801 InlineKeyboardButton(text=categories[i].name, callback_data=seller_callback.new(802 action=f'shop_cat_{categories[i].id}', shop=str(shop.id)803 ))804 ]805 )806 if len(categories) < 100:807 inline_keyboard.append(808 [809 InlineKeyboardButton(text=ADD_CATEGORY_BUTTON, callback_data=seller_callback.new(810 action='add_cat', shop=str(shop.id)811 ))812 ]813 )814 inline_keyboard.append(815 [816 InlineKeyboardButton(text=BACK_BUTTON, callback_data=seller_callback.new(817 action='info', shop=str(shop.id)818 ))819 ]820 )821 keyboard = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)...
main.py
Source:main.py
...79 @dp.message_handler(text=['ÐоеÑ
али ððð', 'веÑнÑÑÑÑÑ Ð² главное менÑ'])80 async def main_menu(message: aiogram.types.message):81 await message.answer("ÐÑбеÑи далÑнейÑее дейÑвÑие", reply_markup=temp_button_keyboard)82 @dp.callback_query_handler(text="Start")83 async def start_callback(query: CallbackQuery):84 await query.message.edit_text(text="СоÑ
Ñанено!")85 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=temp_button_keyboard)86 @dp.message_handler(text='СоздаÑÑ Ð·Ð°ÑвкÑ')87 async def cmd_start(message: types.Message):88 await Form.tag.set()89 await message.answer("ÐÑбиÑай Ñег!", reply_markup=subjects_keyboard)90 @dp.message_handler(lambda message: (not message.text == "#маÑемаÑика"91 and not message.text == "#Ñизика"92 and not message.text == "#пÑогÑаммиÑование"93 and not message.text == "#ÐÐÐ"94 and not message.text == "#диÑкÑеÑка"95 and not message.text == "#английÑкий"96 and not message.text == "#ÑилоÑоÑиÑ"97 and not message.text == "#Ð¥ÐÐ"98 and not message.text == "#пÑоÑее"),state=Form.tag)99 async def process_tag_invalid(message: types.Message):100 return await message.reply("ÐÑполÑзÑй Ñеги Ñ ÐºÐ»Ð°Ð²Ð¸Ð°ÑÑÑÑ, не жÑлÑниÑай!.\n Ðавай попÑобÑем еÑе Ñаз!",101 reply_markup=subjects_keyboard)102 @dp.message_handler(state=Form.tag)103 async def process_name(message: types.Message, state: FSMContext):104 async with state.proxy() as data:105 data['tag'] = message.text106 await Form.next()107 await message.answer("Ðакое бÑÐ´ÐµÑ Ð¾Ð³Ð»Ð°Ð²Ð»ÐµÐ½Ð¸Ðµ?")108 @dp.message_handler(state=Form.title)109 async def process_age(message: types.Message, state: FSMContext):110 async with state.proxy() as data:111 data['title'] = message.text112 await Form.next()113 await message.answer("Ð ÑекÑÑ Ð·Ð°Ñвки ÑкажеÑÑ?")114 @dp.message_handler(state=Form.text)115 async def process_age(message: types.Message, state: FSMContext):116 async with state.proxy() as data:117 data['text'] = message.text118 await Form.next()119 await message.answer("ÐолодеÑ, заÑвка ÑÑпеÑно ÑоÑ
Ñанена!", reply_markup=temp_button_keyboard)120 print(md.text("my tag", md.bold(data['tag'])),121 md.text("my title", md.bold(data['title'])),122 md.text("my text", md.bold(data['text'])), sep="\n")123 @dp.message_handler(text='ÐоказаÑÑ Ð·Ð°Ñвки')124 async def main_menu(message: aiogram.types.message):125 await message.answer("Ðе Ð¿Ð¾ÐºÐ°Ð¶Ñ Ð·Ð°Ñвки", reply_markup=temp_button_keyboard)126 @dp.message_handler(text='ÐаÑÑÑойки')127 async def main_menu(message: aiogram.types.message):128 await message.answer("ÐÑбеÑи делÑнейÑее дейÑвÑие", reply_markup=settings_keyboard)129##+++ ADD SUBJECTS130 @dp.callback_query_handler(text=["add_math"])131 async def start_callback(query: CallbackQuery):132 await query.message.edit_text(text="#маÑемаÑика добавлена!")133 print("добавили #маÑемаÑика")134 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)135 @dp.callback_query_handler(text=["add_phis"])136 async def start_callback(query: CallbackQuery):137 await query.message.edit_text(text="#Ñизика добавлена!")138 print("добавили #Ñизика")139 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)140 @dp.callback_query_handler(text=["add_proga"])141 async def start_callback(query: CallbackQuery):142 await query.message.edit_text(text="#пÑогÑаммиÑование добавлена!")143 print("добавили #пÑогÑаммиÑование")144 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)145 @dp.callback_query_handler(text=["add_OPD"])146 async def start_callback(query: CallbackQuery):147 await query.message.edit_text(text="#ÐÐРдобавлена!")148 print("добавили #ÐÐÐ")149 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)150 @dp.callback_query_handler(text=["add_discr"])151 async def start_callback(query: CallbackQuery):152 await query.message.edit_text(text="#диÑкÑеÑка добавлена!")153 print("добавили #диÑкÑеÑка")154 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)155 @dp.callback_query_handler(text=["add_eng"])156 async def start_callback(query: CallbackQuery):157 await query.message.edit_text(text="#английÑкий добавлена!")158 print("добавили #английÑкий")159 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)160 @dp.callback_query_handler(text=["add_filosof"])161 async def start_callback(query: CallbackQuery):162 await query.message.edit_text(text="#ÑилоÑоÑÐ¸Ñ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð°!")163 print("добавили #ÑилоÑоÑиÑ")164 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)165 @dp.callback_query_handler(text=["add_XOD"])166 async def start_callback(query: CallbackQuery):167 await query.message.edit_text(text="#Ð¥ÐРдобавлена!")168 print("добавили #Ð¥ÐÐ")169 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)170 @dp.callback_query_handler(text=["add_another"])171 async def start_callback(query: CallbackQuery):172 await query.message.edit_text(text="#пÑоÑее добавлена!")173 print("добавили #пÑоÑее")174 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_add)175##--- ADD SUBJECTS176##+++ REMOVE SUBJECTS177 @dp.callback_query_handler(text=["remove_math"])178 async def start_callback(query: CallbackQuery):179 await query.message.edit_text(text="#маÑемаÑика Ñдалена!")180 print("Ñдалили #маÑемаÑика")181 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)182 @dp.callback_query_handler(text=["remove_phis"])183 async def start_callback(query: CallbackQuery):184 await query.message.edit_text(text="#Ñизика Ñдалена!")185 print("Ñдалили #Ñизика")186 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)187 @dp.callback_query_handler(text=["remove_proga"])188 async def start_callback(query: CallbackQuery):189 await query.message.edit_text(text="#пÑогÑаммиÑование Ñдалена!")190 print("Ñдалили #пÑогÑаммиÑование")191 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)192 @dp.callback_query_handler(text=["remove_OPD"])193 async def start_callback(query: CallbackQuery):194 await query.message.edit_text(text="#ÐÐÐ Ñдалена!")195 print("Ñдалили #ÐÐÐ")196 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)197 @dp.callback_query_handler(text=["remove_discr"])198 async def start_callback(query: CallbackQuery):199 await query.message.edit_text(text="#диÑкÑеÑка Ñдалена!")200 print("Ñдалили #диÑкÑеÑка")201 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)202 @dp.callback_query_handler(text=["remove_eng"])203 async def start_callback(query: CallbackQuery):204 await query.message.edit_text(text="#английÑкий Ñдалена!")205 print("Ñдалили #английÑкий")206 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)207 @dp.callback_query_handler(text=["remove_filosof"])208 async def start_callback(query: CallbackQuery):209 await query.message.edit_text(text="#ÑилоÑоÑÐ¸Ñ Ñдалена!")210 print("Ñдалили #ÑилоÑоÑиÑ")211 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)212 @dp.callback_query_handler(text=["remove_XOD"])213 async def start_callback(query: CallbackQuery):214 await query.message.edit_text(text="#Ð¥ÐÐ Ñдалена!")215 print("Ñдалили #Ð¥ÐÐ")216 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)217 @dp.callback_query_handler(text=["remove_another"])218 async def start_callback(query: CallbackQuery):219 await query.message.edit_text(text="#пÑоÑее Ñдалена!")220 print("Ñдалили #пÑоÑее")221 await query.message.answer("ÐÑбеÑи далÑнейÑее дейÑÑвие", reply_markup=select_subjects_keyboard_remove)222##--- REMOVE SUBJECTS223 @dp.message_handler(text='добавиÑÑ Ñеги в избÑанное')224 async def main_menu(message: aiogram.types.message):225 await message.answer("ÐÐ¾Ð±Ð°Ð²Ñ Ñеги в лÑбимÑе!", reply_markup=select_subjects_keyboard_add)226 @dp.message_handler(text='ÑдалиÑÑ Ñеги из избÑаннÑÑ
')227 async def main_menu(message: aiogram.types.message):228 await message.answer("УбеÑи Ñеги из лÑбимÑÑ
!", reply_markup=select_subjects_keyboard_remove)229 @dp.message_handler(text='ÑпиÑок заÑвок')230 async def main_menu(message: aiogram.types.message):231 await message.answer("ÐÑвод ÑпиÑок заÑвок")232if __name__ == "__main__":...
test_sequences.py
Source:test_sequences.py
...16 vector = (0, 54, 17, -3, 1023)17 expected = (54, 17, -3)18 # for assignment in callbacks19 vrs = {"start_called" : False, "end_called": False}20 def start_callback(x):21 vrs["start_called"] = True22 self.assertEqual(x, 0)23 def end_callback(x):24 vrs["end_called"] = True25 self.assertEqual(x, 1023)26 j = 027 for i in seq_notify(vector, lambda x: isinstance(x, int),28 start_callback ,29 end_callback ):30 self.assertEqual(i, expected[j])31 j += 132 self.assertTrue(vrs["start_called"])33 self.assertTrue(vrs["end_called"])34 35 def test_seq_full_start_callbacks(self):36 '''Tests full match with 1 callback'''37 vector = (0, 54, 17, -3, 1023)38 expected = (54, 17, -3, 1023)39 # for assignment in callbacks40 vrs = {"start_called" : False}41 def start_callback(x):42 vrs["start_called"] = True43 self.assertEqual(x, 0)44 j = 045 for i in seq_notify(vector, lambda x: isinstance(x, int),46 start_callback,47 None):48 self.assertEqual(i, expected[j])49 j += 150 self.assertTrue(vrs["start_called"])51 52 def test_seq_full_end_callbacks(self):53 '''Tests full match with 1 callback'''54 vector = (0, 54, 17, -3, 1023)55 expected = (0, 54, 17, -3)56 # for assignment in callbacks57 vrs = {"end_called": False}58 def end_callback(x):59 vrs["end_called"] = True60 self.assertEqual(x, 1023)61 j = 062 for i in seq_notify(vector, lambda x: isinstance(x, int),63 None,64 end_callback):65 self.assertEqual(i, expected[j])66 j += 167 self.assertTrue(vrs["end_called"])68 69 def test_seq_only_callbacks(self):70 '''Tests no iterations'''71 vector = (0, 1023)72 # for assignment in callbacks73 vrs = {"start_called" : False, "end_called": False}74 def start_callback(x):75 vrs["start_called"] = True76 self.assertEqual(x, 0)77 def end_callback(x):78 vrs["end_called"] = True79 self.assertEqual(x, 1023)80 isiterate = False81 for _ in seq_notify(vector, lambda x: isinstance(x, int),82 start_callback,83 end_callback):84 isiterate = True85 self.assertFalse(isiterate)86 self.assertTrue(vrs["start_called"])87 self.assertTrue(vrs["end_called"])88 def test_seq_sub_first(self):89 '''Tests part match'''90 vector = (0, 54, 17, 3, 1023)91 expected = (54, 17, 3, 1023)92 j = 093 for i in seq_notify(vector, lambda x: x>0, None, None):94 self.assertEqual(i, expected[j])95 j += 196 def test_seq_sub_last(self):97 '''Tests part match'''98 vector = (54, 17, 3, 1023, 0)99 expected = (54, 17, 3, 1023)100 j = 0101 for i in seq_notify(vector, lambda x: x>0, None, None):102 self.assertEqual(i, expected[j])103 j += 1104 def test_seq_sub_callbacks(self):105 '''Tests part match with callbacks'''106 vector = (0, 54, 17, 3, 1023, 0)107 expected = (17, 3)108 # for assignment in callbacks109 vrs = {"start_called" : False, "end_called": False}110 def start_callback(x):111 vrs["start_called"] = True112 self.assertEqual(x, 54)113 def end_callback(x):114 vrs["end_called"] = True115 self.assertEqual(x, 1023)116 j = 0117 for i in seq_notify(vector, lambda x: x>0,118 start_callback,119 end_callback):120 self.assertEqual(i, expected[j])121 j += 1122 self.assertTrue(vrs["start_called"])123 self.assertTrue(vrs["end_called"])124 def test_seq_sub_start_callbacks(self):125 '''Tests part match with 1 callback'''126 vector = (0, 54, 17, 3, 1023)127 expected = (17, 3, 1023)128 # for assignment in callbacks129 vrs = {"start_called" : False}130 def start_callback(x):131 vrs["start_called"] = True132 self.assertEqual(x, 54)133 j = 0134 for i in seq_notify(vector, lambda x: x>0,135 start_callback,136 None):137 self.assertEqual(i, expected[j])138 j += 1139 self.assertTrue(vrs["start_called"])140 def test_seq_sub_end_callbacks(self):141 '''Tests part match with 1 callback'''142 vector = (54, 17, 3, 1023, 0)143 expected = (54, 17, 3)144 # for assignment in callbacks145 vrs = {"end_called": False}146 def end_callback(x):147 vrs["end_called"] = True148 self.assertEqual(x, 1023)149 j = 0150 for i in seq_notify(vector, lambda x: x>0,151 None,152 end_callback):153 self.assertEqual(i, expected[j])154 j += 1155 self.assertTrue(vrs["end_called"])156 def test_seq_sub_both_callbacks(self):157 '''Tests part match callbacks without body'''158 vector = (0, 54, 1023, 0)159 # for assignment in callbacks160 vrs = {"start_called" : False, "end_called": False}161 def start_callback(x):162 vrs["start_called"] = True163 self.assertEqual(x, 54)164 def end_callback(x):165 vrs["end_called"] = True166 self.assertEqual(x, 1023)167 isiterate = False168 for _ in seq_notify(vector, lambda x: x>0,169 start_callback,170 end_callback):171 isiterate = True172 self.assertFalse(isiterate)173 self.assertTrue(vrs["start_called"])174 self.assertTrue(vrs["end_called"])175 def test_seq_sub_empty(self):176 '''Tests no match'''177 vector = (0, -54, -17, -3, -1023)178 # for assignment in callbacks179 vrs = {"start_called" : False, "end_called": False}180 isiterate = False181 def start_callback(x):182 vrs["start_called"] = True183 def end_callback(x):184 vrs["end_called"] = True185 for _ in seq_notify(vector, lambda x: x>0,186 start_callback,187 end_callback ):188 isiterate = True189 self.assertFalse(isiterate)190 self.assertFalse(vrs["start_called"])191 self.assertFalse(vrs["end_called"])192 def test_seq_sub_oneitem(self):193 '''Tests one match'''194 vector = (0.0, 54, -1.7, 3.0, "1023")195 for i in seq_notify(vector, lambda x: isinstance(x, int),196 None,197 None):198 self.assertEqual(i, 54)199 def test_seq_sub_oneitem_callbacks(self):200 '''Tests one match callbacks'''201 vector = (0.0, 54, -1.7, 3.0, "1023")202 # for assignment in callbacks203 vrs = {"start_called" : False, "end_called": False}204 def start_callback(x):205 vrs["start_called"] = True206 self.assertEqual(x, 54)207 def end_callback(x):208 vrs["end_called"] = True209 self.assertEqual(x, 54)210 isiterate = False211 for _ in seq_notify(vector, lambda x: isinstance(x, int),212 start_callback,213 end_callback):214 isiterate = True215 self.assertTrue(vrs["start_called"])216 self.assertTrue(vrs["end_called"])217 self.assertFalse(isiterate)218 def test_seq_sub_oneitem_start_callback(self):219 '''Tests one match first callback'''220 vector = (0.0, 54, -1.7, 3.0, "1023")221 vrs = {"start_called" : False}222 def start_callback(x):223 vrs["start_called"] = True224 self.assertEqual(x, 54)225 isiterate = False226 for _ in seq_notify(vector, lambda x: isinstance(x, int),227 start_callback,228 None):229 isiterate = True230 self.assertTrue(vrs["start_called"])231 self.assertFalse(isiterate)232 def test_seq_sub_oneitem_end_callback(self):233 '''Tests one match second callback'''234 vector = (0.0, 54, -1.7, 3.0, "1023")235 # for assignment in callbacks236 vrs = {"end_called": False}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!