Example #1
0
def edit_spend_cost_handler(bot: DialogBot, params):
    peer = params[0].peer

    try:
        cost = int(params[0].message.textMessage.text)
    except ValueError:
        bot.messaging.send_message(
            peer,
            "Не могу тебя понять. Пожалуйста, отправь положительное число без дополнительных символов, только цифры"
        )
        return

    if cost < 0:
        bot.messaging.send_message(
            peer,
            "Не могу тебя понять. Пожалуйста, отправь положительное число без дополнительных символов, только цифры"
        )
        return

    balance_change = BalanceChange.select().where(BalanceChange.id == COST_EDIT_CACHE[params[0].sender_uid]).get()
    balance_change.cost = cost
    balance_change.save()

    bot.messaging.send_message(
        peer,
        "Данные обновлены"
    )

    utils.cancel_handler(bot, params)
Example #2
0
def menu_handler(bot: DialogBot, params):
    peer = bot.users.get_user_peer_by_id(params[0].uid)

    if params[0].value == "update_budget":
        bot.messaging.send_message(
            peer,
            "Укажите новый бюджет"
        )

        utils.set_state_by_uid(params[0].uid, "MONTHLY_BALANCE_SET")

    elif params[0].value == "create_spend":
        bot.messaging.send_message(
            peer,
            "Как назвать трату?"
        )

        utils.set_state_by_uid(params[0].uid, "NEW_SPEND_NAME")

    elif params[0].value == "list_spends":
        bot.messaging.send_message(
            peer,
            "Список трат",
            utils.get_spends_list(params[0].uid)
        )


    elif params[0].value == "export_xlsx":
        bot.messaging.send_message(
            peer,
            "Ваша таблица:"
        )

        wb = Workbook()
        ws = wb.active

        ws.append(["Название", "Цена", "Дата добавления"])

        for spend in BalanceChange.select().where(BalanceChange.owner == params[0].uid):
            ws.append([spend.name, spend.cost, spend.added])

        wb.save(f"sheets/{params[0].uid}.xlsx")

        bot.messaging.send_file(
            peer,
            f"sheets/{params[0].uid}.xlsx"
        )

        object = Path(f"sheets/{params[0].uid}.xlsx")
        object.unlink()

        utils.cancel_handler(bot, params)
Example #3
0
def delete_all_handler(bot: DialogBot, params):
    peer = bot.users.get_user_peer_by_id(params[0].uid)

    for cost in BalanceChange.select().where(BalanceChange.owner == params[0].uid):
        cost.delete_instance()

    bot.messaging.send_message(
        peer,
        "Все траты были успешно удалены"
    )

    bot.messaging.delete(
        params[0]
    )

    utils.cancel_handler(bot, params)
Example #4
0
def cost_manager_handler(bot: DialogBot, params):
    task, cost_id = params[0].value.split("_")
    cost_id = int(cost_id)
    peer = bot.users.get_user_peer_by_id(params[0].uid)

    if task == "delete":
        bot.messaging.delete(
            params[0]
        )

        cost = BalanceChange.select().where(BalanceChange.id == cost_id).get()
        cost.delete_instance()

        if params[0].uid in EVENT_LIST_MESSAGE_CACHE:
            bot.messaging.update_message(
                EVENT_LIST_MESSAGE_CACHE[params[0].uid],
                "Список трат",
                utils.get_spends_list(params[0].uid)
            )

        bot.messaging.send_message(
            peer,
            "Событие удалено"
        )

        utils.cancel_handler(bot, params)

    elif task == "name":
        bot.messaging.send_message(
            peer,
            "Укажите новое имя траты"
        )

        COST_EDIT_CACHE[params[0].uid] = cost_id

        utils.set_state_by_uid(params[0].uid, "EDIT_SPEND_NAME")

    elif task == "cost":
        bot.messaging.send_message(
            peer,
            "Укажите новую цену траты"
        )

        COST_EDIT_CACHE[params[0].uid] = cost_id

        utils.set_state_by_uid(params[0].uid, "EDIT_SPEND_COST")
Example #5
0
def edit_spend_name_handler(bot: DialogBot, params):
    peer = params[0].peer
    new_name = params[0].message.textMessage.text

    balance_change = BalanceChange.select().where(BalanceChange.id == COST_EDIT_CACHE[params[0].sender_uid]).get()
    balance_change.name = new_name
    balance_change.save()

    bot.messaging.send_message(
        peer,
        "Данные обновлены"
    )

    if params[0].sender_uid in EVENT_LIST_MESSAGE_CACHE:
        bot.messaging.update_message(
            EVENT_LIST_MESSAGE_CACHE[params[0].sender_uid],
            "Список трат",
            utils.get_spends_list(params[0].sender_uid)
        )

    utils.cancel_handler(bot, params)
Example #6
0
def get_spends_list(uid: int):
    costs = {}

    for cost in BalanceChange.select().where(
            BalanceChange.owner == uid).order_by(BalanceChange.name):
        costs[str(cost.get_id())] = f"{cost.name}"

    return [
        interactive_media.InteractiveMediaGroup([
            interactive_media.InteractiveMedia(
                "cost_list",
                interactive_media.InteractiveMediaSelect(costs,
                                                         label="Список трат")),
            interactive_media.InteractiveMedia(
                "cancel",
                interactive_media.InteractiveMediaButton(
                    "cancel", "В главное меню")),
            interactive_media.InteractiveMedia(
                "delete_all",
                interactive_media.InteractiveMediaButton(
                    "delete_all", "Удалить ВСЕ траты"))
        ])
    ]
Example #7
0
def get_spend_sum(uid: int):
    sum = 0
    for spend in BalanceChange.select().where(BalanceChange.owner == uid):
        sum += spend.cost
    return sum