Exemple #1
0
def callback_phenomenon(call):
    """
    handle phenomenon db
    add phenomenon to db
    """
    user = User.query.filter_by(chat_id=call.from_user.id).first()

    phenomenon_data = call.data[11:]
    phenomenon = Phenomenon.query.filter_by(phenomenon=phenomenon_data,
                                            user_id=user.id).first()

    try:
        db.session.delete(phenomenon)
    except UnmappedInstanceError as e:
        logger.error(
            f'The phenomenon has not been found. Creating new one\n{e}')
        new_phenomenon = Phenomenon(phenomenon=phenomenon_data,
                                    user_id=user.id)
        db.session.add(new_phenomenon)
        text = hints['phenomenon set'][user.language]
    else:
        text = hints['phenomenon delete'][user.language]

    db.session.commit()
    bot.edit_message_text(chat_id=call.message.chat.id,
                          message_id=call.message.message_id,
                          text=hints['phenomena intro'][user.language],
                          reply_markup=gen_markup_phenomena(
                              user.id, user.language))
    bot.answer_callback_query(
        callback_query_id=call.id,
        show_alert=False,
        text=f"{hints['phenomenon set del'][user.language]} "
        f"{phenomenon_button_names[phenomenon_data][user.language]} {text}")
Exemple #2
0
def callback_all_phenomena(call):
    """
    handle inline button 'all phenomena'
    add all phenomena to db
    """
    user = User.query.filter_by(chat_id=call.from_user.id).first()

    all_ph_from_db = Phenomenon.query.filter_by(user_id=user.id,
                                                value=None).all()
    if len(all_ph_from_db) == 7:
        for ph in all_ph_from_db:
            db.session.delete(ph)
        text = hints['all untick'][user.language]
    else:
        for ph in phenomena_list:
            new_phenomenon = Phenomenon.query.filter_by(
                phenomenon=ph, user_id=user.id).first()
            if new_phenomenon is None:
                new_phenomenon = Phenomenon(user_id=user.id, phenomenon=ph)
                db.session.add(new_phenomenon)
        text = hints['all tick'][user.language]
    db.session.commit()

    bot.edit_message_text(chat_id=call.message.chat.id,
                          message_id=call.message.message_id,
                          text=hints['phenomena intro'][user.language],
                          reply_markup=gen_markup_phenomena(
                              user.id, user.language))
    bot.answer_callback_query(callback_query_id=call.id,
                              show_alert=False,
                              text=text)
Exemple #3
0
def button_phenomena(message, data):
    """Handle button 'phenomena'"""
    response = hints['phenomena intro'][data['lang']]
    bot.send_message(data['chat_id'],
                     text=response,
                     reply_markup=gen_markup_phenomena(data['user'].id,
                                                       data['lang']))
Exemple #4
0
def callback_inline_back_ph(call):
    """handle back to phenomenon button"""
    user = User.query.filter_by(chat_id=call.message.chat.id).first()
    bot.edit_message_text(chat_id=call.message.chat.id,
                          message_id=call.message.message_id,
                          text=hints['phenomena intro'][user.language],
                          reply_markup=gen_markup_phenomena(
                              user.id, user.language))
Exemple #5
0
def callback_back_manually_phenomena(call):
    """
    handle all manually phenomena db
    add all man phenomena to db
    """
    user = User.query.filter_by(chat_id=call.from_user.id).first()
    bot.edit_message_text(chat_id=call.message.chat.id,
                          message_id=call.message.message_id,
                          text=hints['phenomena intro'][user.language],
                          reply_markup=gen_markup_phenomena(
                              user.id, user.language))