MyZik

Untitled

Apr 11th, 2021
623
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class OrderSG(StatesGroup):
  2.     city = State()
  3.     operator = State()
  4.     tariff = State()
  5.  
  6.  
  7. async def get_data(dialog_manager: DialogManager, **kwargs):
  8.     selected_city = dialog_manager.context.data("city_id", None)
  9.     selected_operator = dialog_manager.context.data("operator_id", None)
  10.     selected_tariff = dialog_manager.context.data("tariff_id", None)
  11.  
  12.     cities = [(city.name, city.id) for city in await City.query.gino.all()]
  13.     operators = [(t_operator.name, t_operator.id) for t_operator in await Operator.query.gino.all()]
  14.     if selected_operator is not None:
  15.         tariffs = await Tariff.query.where(Tariff.operator_id == selected_operator).gino.all()
  16.         tariffs_list = [(tariff.name, tariff.id) for tariff in tariffs]
  17.     else:
  18.         tariffs_list = []
  19.  
  20.     return {
  21.         "cities": cities,
  22.         "operators": operators,
  23.         "tariffs": tariffs_list
  24.     }
  25.  
  26.  
  27. async def on_cancel(query: CallbackQuery, button: Button, manager: DialogManager):
  28.     ...
  29.  
  30.  
  31. async def on_city_selected(c: ChatEvent, select: Select, manager: DialogManager, item_id: str):
  32.     ...
  33.  
  34.  
  35. async def on_operator_selected(c: ChatEvent, select: Select, manager: DialogManager, item_id: str):
  36.     ...
  37.  
  38.  
  39. async def on_tariff_changed(c: ChatEvent, select: Select, manager: DialogManager, item_id: str):
  40.     await c.message.edit_text(f"Selected: {item_id}")
  41.  
  42.  
  43. async def on_tariff_selected(c: ChatEvent, select: Select, manager: DialogManager, item_id: str):
  44.     ...
  45.  
  46.  
  47. back_button = Back(Const(_("⬅️ Назад")))
  48. cancel_button = Button(Const(_("❌ Отмена")), id="cancel", on_click=on_cancel)
  49.  
  50. select_city = Select(
  51.     Format("{item[0]}"),
  52.     id="s_cities",
  53.     item_id_getter=operator.itemgetter(1),
  54.     items="cities",
  55.     on_click=on_city_selected,
  56. )
  57.  
  58. select_operator = Select(
  59.     Format("{item[0]}"),
  60.     id="s_operators",
  61.     item_id_getter=operator.itemgetter(1),
  62.     items="operators",
  63.     on_click=on_operator_selected,
  64. )
  65.  
  66. select_tariff = Radio(
  67.     Format("🔘 {item[0]}"),
  68.     Format("⚪️ {item[0]}"),
  69.     id="s_tariffs",
  70.     item_id_getter=operator.itemgetter(1),
  71.     items="tariffs",
  72.     on_state_changed=on_tariff_changed,
  73. )
  74.  
  75. window_city = Window(
  76.     Const(_("Выберите ваш город")),
  77.     Column(
  78.         select_city
  79.     ),
  80.     Row(
  81.         cancel_button
  82.     ),
  83.     state=OrderSG.city,
  84.     getter=get_data,
  85. )
  86.  
  87. window_operator = Window(
  88.     Const(_("Выберите оператора")),
  89.     Column(
  90.         select_operator
  91.     ),
  92.     Row(
  93.         back_button,
  94.         cancel_button,
  95.     ),
  96.     state=OrderSG.operator,
  97.     getter=get_data,
  98. )
  99.  
  100. window_tariffs = Window(
  101.     Const(_("Выберите тариф")),
  102.     Column(
  103.         select_tariff
  104.     ),
  105.     Row(
  106.         back_button,
  107.         cancel_button
  108.     ),
  109.     state=OrderSG.tariff,
  110.     getter=get_data,
  111. )
  112.  
  113. dialog = Dialog(
  114.     window_city,
  115.     window_operator,
  116.     window_tariffs
  117. )
  118.  
  119. registry.register(dialog)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×