Esempio n. 1
0
def merge_from_native_poll(poll: Poll, native_poll: NativePoll,
                           session: Session) -> None:
    """Fills information in a pollbot `Poll` with data extracted from a native Telegram poll"""
    poll.created_from_native = True
    poll.poll_type = convert_poll_type(native_poll).name
    poll.name = native_poll.question
    poll.anonymous = native_poll.is_anonymous
    add_text_options_from_list(session, poll,
                               [o.text for o in native_poll.options])
Esempio n. 2
0
def merge_from_native_poll(poll: Poll, native_poll: NativePoll,
                           session: Session) -> None:
    """Fills information in a pollbot `Poll` with data extracted from a native Telegram poll"""
    poll.created_from_native = True
    poll.poll_type = convert_poll_type(native_poll).name
    poll.name = native_poll.question
    poll.anonymous = native_poll.is_anonymous

    # Get all options, strip them and add them
    options = [o.text for o in native_poll.options]
    options_to_add = list(map(str.strip, options))
    add_multiple_options(session, poll, options_to_add)
Esempio n. 3
0
def create_from_native_poll(bot: Bot, update: Update, session: Session,
                            user: User):
    native_poll: NativePoll = update.message.poll

    if user.current_poll is not None and not user.current_poll.created:
        update.effective_chat.send_message(
            i18n.t("creation.already_creating_ask_replace",
                   locale=user.locale),
            reply_markup=get_replace_current_creation_keyboard(
                user.current_poll),
        )
        return

    poll = Poll.create(user, session)
    merge_from_native_poll(poll, native_poll, session)

    text = get_native_poll_merged_text(poll)
    keyboard = get_native_poll_merged_keyboard(poll)

    update.effective_chat.send_message(
        text,
        parse_mode="markdown",
        reply_markup=keyboard,
        disable_web_page_preview=True,
    )
Esempio n. 4
0
def initialize_poll(session, user, chat):
    """Initialize a new poll and send the user the poll creation message.

    This function also prevents users from:
        - Having too many polls.
        - Creating a new poll, when they're already in the middle of
            creating another poll
    """
    user.started = True

    # Early return, if the user owns too many polls
    if len(user.polls) > config["telegram"]["max_polls_per_user"]:
        chat.send_message(
            i18n.t("creation.too_many_polls", locale=user.locale, count=len(user.polls))
        )
        return

    # Early return, if the user is already in the middle of creating a poll
    if user.current_poll is not None and not user.current_poll.created:
        chat.send_message(
            i18n.t("creation.already_creating", locale=user.locale),
            reply_markup=get_cancel_creation_keyboard(user.current_poll),
        )
        return

    poll = Poll.create(user, session)
    text = get_init_text(poll)
    keyboard = get_init_keyboard(poll)

    chat.send_message(
        text,
        parse_mode="markdown",
        reply_markup=keyboard,
        disable_web_page_preview=True,
    )
Esempio n. 5
0
def init_votes_for_new_options(session, poll: Poll, added_options: List[str]):
    """
    When a new option is added, we need to create new votes
    for all users that have already voted for this poll.
    """
    if not poll.is_priority():
        return

    # Get all newly added options
    new_options = (
        session.query(Option)
        .filter(Option.poll == poll)
        .filter(Option.name.in_(added_options))
        .all()
    )

    # The new options are already flushed.
    # Subtract the amount of new options to get the proper index.
    existing_options_count = len(poll.options) - len(new_options)

    users_that_voted = (
        session.query(User).join(User.votes).filter(Vote.poll == poll).all()
    )

    for user in users_that_voted:
        for index, option in enumerate(new_options):
            vote = Vote(user, option)
            vote.priority = existing_options_count + index
            user.votes.append(vote)
Esempio n. 6
0
def create_poll(bot, update, session, user):
    """Create a new poll."""
    # The previous unfinished poll will be removed
    if user.current_poll is not None and not user.current_poll.created:
        update.message.chat.send_message(
            i18n.t('creation.already_creating', locale=user.locale),
            reply_markup=get_cancel_creation_keyboard(user.current_poll))
        return

    poll = Poll(user)
    poll.european_date_format = user.european_date_format
    poll.locale = user.locale
    user.current_poll = poll
    user.expected_input = ExpectedInput.name.name
    session.add(poll)
    session.commit()

    text = get_init_text(poll)
    keyboard = get_init_keyboard(poll)

    update.message.chat.send_message(text, parse_mode='markdown', reply_markup=keyboard)
Esempio n. 7
0
def create_poll(bot, update, session, user):
    """Create a new poll."""
    # The previous unfinished poll will be removed
    user.started = True
    if user.current_poll is not None and not user.current_poll.created:
        update.message.chat.send_message(
            i18n.t('creation.already_creating', locale=user.locale),
            reply_markup=get_cancel_creation_keyboard(user.current_poll))
        return

    poll = Poll.create(user, session)
    text = get_init_text(poll)
    keyboard = get_init_keyboard(poll)

    update.message.chat.send_message(
        text,
        parse_mode='markdown',
        reply_markup=keyboard,
        disable_web_page_preview=True,
    )
Esempio n. 8
0
def init_votes(session, poll: Poll, user: User):
    """
    Since Priority votes always need priorities, call this to create a vote
    for every option in the poll with a random priority for the given user.
    """
    assert poll.is_priority()

    # Don't init votes, if there already is a vote
    any_vote = (session.query(Vote).filter(Vote.user == user).filter(
        Vote.poll == poll).first())
    if any_vote is not None:
        return

    votes = []
    for index, option in enumerate(
            random.sample(poll.options, len(poll.options))):
        vote = Vote(user, option)
        vote.priority = index
        votes.append(vote)
    session.add_all(votes)
Esempio n. 9
0
def init_poll(session, context):
    """Start the creation of a new poll."""
    user = context.user
    chat = context.query.message.chat
    if user.current_poll is not None and not user.current_poll.created:
        chat.send_message(
            i18n.t("creation.already_creating", locale=user.locale),
            reply_markup=get_cancel_creation_keyboard(user.current_poll),
        )
        return

    poll = Poll.create(user, session)
    text = get_init_text(poll)
    keyboard = get_init_keyboard(poll)

    chat.send_message(
        text,
        parse_mode="markdown",
        reply_markup=keyboard,
        disable_web_page_preview=True,
    )
Esempio n. 10
0
def clone_poll(session, original_poll: Poll):
    """Create a clone from the current poll."""
    new_poll = Poll(original_poll.user)
    new_poll.created = True
    session.add(new_poll)

    new_poll.name = original_poll.name
    new_poll.description = original_poll.description
    new_poll.poll_type = original_poll.poll_type
    new_poll.anonymous = original_poll.anonymous
    new_poll.number_of_votes = original_poll.number_of_votes
    new_poll.allow_new_options = original_poll.allow_new_options
    new_poll.option_sorting = original_poll.option_sorting
    new_poll.user_sorting = original_poll.user_sorting
    new_poll.results_visible = original_poll.results_visible
    new_poll.show_percentage = original_poll.show_percentage

    for option in original_poll.options:
        new_option = Option(new_poll, option.name)
        new_option.description = option.description
        new_option.is_date = option.is_date
        new_option.index = option.index
        session.add(new_option)

    return new_poll