def complete_alt_login(update, context):
    session = Session()
    role = context.user_data.get('role', '-')

    name = context.user_data.get('name', '-')
    patronymic = context.user_data.get('patronymic', '-')
    surname = context.user_data.get('surname', '-')

    group = '-'
    post = '-'
    directions = '-'
    if role == 'student':
        group = context.user_data.get('group', '-')

    elif role == 'mentor':
        post = context.user_data.get('post', '-')
        directions = context.user_data.get('directions', '-')

    user = User(tg_chat_id=update.message.chat_id,
                role=role,
                name=name,
                surname=surname,
                patronymic=patronymic,
                group=group,
                post=post,
                directions=directions)

    try:
        schedule_notifications_job(context, user)
        context.job_queue.run_daily(schedule_notifications_job,
                                    time=datetime.time(
                                        hour=1,
                                        minute=0,
                                        tzinfo=pytz.timezone('Etc/GMT-3')),
                                    days=(0, 1, 2, 3, 4, 5),
                                    context=user,
                                    name='Обновить расписание')
        user.is_notified = True
    except (NoEntriesException, WentWrongException, ChoicesException):
        context.bot.send_message(chat_id=user.tg_chat_id,
                                 text=alt_login_replicas['notification_error'])

    session.add(user)
    context.bot.send_message(
        chat_id=update.message.chat_id,
        text=f"Здравствуйте, {user.name} {user.patronymic}. Вы авторизованы.")

    context.user_data.pop('role', None)
    context.user_data.pop('name', None)
    context.user_data.pop('patronymic', None)
    context.user_data.pop('surname', None)
    context.user_data.pop('group', None)
    context.user_data.pop('post', None)
    context.user_data.pop('directions', None)

    session.commit()
    session.close()

    return user
Example #2
0
def do_login_auth(update, context):
    login_data = {
        'email': context.user_data['email'],
        'password': context.user_data['password']
    }

    login_result = requests.post(url=LOGIN_URL, data=login_data).json()

    if login_result['auth']:
        session = Session()
        user_info = login_result['user']

        if user_info['role'] == 'student':
            user = User(tg_chat_id=update.message.chat_id,
                        role=user_info['role'],
                        surname=user_info['surname'],
                        name=user_info['name'],
                        patronymic=user_info['patronymic'],
                        group=user_info['group'],
                        email=user_info['email'],
                        phone=user_info['phone'],
                        post='',
                        directions='',
                        is_notified=True,
                        is_admin=False)

        elif user_info['role'] == 'mentor':
            user = User(tg_chat_id=update.message.chat_id,
                        role=user_info['role'],
                        surname=user_info['surname'],
                        name=user_info['name'],
                        patronymic=user_info['patronymic'],
                        group='',
                        email=user_info['email'],
                        phone=user_info['phone'],
                        post=user_info['post'],
                        directions=user_info['directions'])

        else:
            context.bot.send_message(
                chat_id=update.message.chat_id,
                text=auth_replicas['something_gone_wrong'])
            return ConversationHandler.END

        try:
            schedule_notifications_job(context, user)
            context.job_queue.run_daily(schedule_notifications_job,
                                        time=datetime.time(
                                            hour=1,
                                            minute=0,
                                            tzinfo=pytz.timezone('Etc/GMT-3')),
                                        days=(0, 1, 2, 3, 4, 5),
                                        context=user,
                                        name='Обновить расписание')
            user.is_notified = True
        except (NoEntriesException, WentWrongException, ChoicesException):
            context.bot.send_message(chat_id=user.tg_chat_id,
                                     text=auth_replicas['notification_error'])

        session.add(user)
        session.commit()

        context.bot.send_message(
            chat_id=update.message.chat_id,
            text=
            f"Здравствуйте, { user.name } { user.patronymic }. Вы авторизованы."
        )

        session.close()

    else:

        if 'message' in context.user_data.keys():
            if 'message' == "User didn't activated account.":
                context.bot.sendMessage(
                    chat_id=update.message.chat_id,
                    text=auth_replicas['not_activated_account'])

        else:
            context.bot.sendMessage(chat_id=update.message.chat_id,
                                    text=auth_replicas['something_gone_wrong'])

    del context.user_data['email']
    del context.user_data['password']

    return ConversationHandler.END