Exemplo n.º 1
0
def get_priority_buttons(poll, user):
    """Create the keyboard for priority poll. Only show the deeplink, if not in a direct conversation."""
    if user is None:
        bot_name = config["telegram"]["bot_name"]
        payload = get_start_button_payload(poll, StartAction.vote)
        url = f"http://t.me/{bot_name}?start={payload}"
        buttons = [[
            InlineKeyboardButton(i18n.t("keyboard.vote", locale=poll.locale),
                                 url=url)
        ]]

        return buttons

    buttons = []
    options = get_sorted_options(poll)
    vote_button_type = CallbackType.vote.value
    vote_increase = CallbackResult.increase_priority.value
    vote_decrease = CallbackResult.decrease_priority.value
    session = get_session()
    votes = (session.query(Vote).filter(Vote.poll == poll).filter(
        Vote.user == user).order_by(Vote.priority.asc()).options(
            joinedload(Vote.option)).all())

    indices = get_option_indices(options)
    for index, vote in enumerate(votes):
        option = vote.option
        if not poll.compact_buttons:
            name_row = [
                InlineKeyboardButton(f"{option.name}",
                                     callback_data=IGNORE_PAYLOAD)
            ]
            buttons.append(name_row)
        name_hint_payload = (
            f"{CallbackType.show_option_name.value}:{poll.id}:{option.id}")
        increase_payload = f"{vote_button_type}:{option.id}:{vote_increase}"
        decrease_payload = f"{vote_button_type}:{option.id}:{vote_decrease}"
        ignore_payload = f"{CallbackType.ignore.value}:0:0"

        vote_row = []
        if poll.compact_buttons:
            vote_row.append(
                InlineKeyboardButton(f"{indices[index]})",
                                     callback_data=name_hint_payload))

        if index != len(votes) - 1:
            vote_row.append(
                InlineKeyboardButton("▼", callback_data=decrease_payload))
        else:
            vote_row.append(
                InlineKeyboardButton(" ", callback_data=ignore_payload))

        if index != 0:
            vote_row.append(
                InlineKeyboardButton("▲", callback_data=increase_payload))
        else:
            vote_row.append(
                InlineKeyboardButton(" ", callback_data=ignore_payload))

        buttons.append(vote_row)
    return buttons
Exemplo n.º 2
0
def get_option_information(session, poll, context, summarize):
    """Compile all information about a poll option."""
    lines = []
    # Sort the options accordingly to the polls settings
    options = get_sorted_options(poll, context.total_user_count)

    # All options with their respective people percentage
    for index, option in enumerate(options):
        lines.append("")
        lines.append(get_option_line(session, option, index))
        if option.description is not None:
            lines.append(f"┆ _{option.description}_")

        if context.show_results and context.show_percentage:
            lines.append(get_percentage_line(option, context))

        # Add the names of the voters to the respective options
        if (context.show_results and not context.anonymous
                and len(option.votes) > 0 and not poll.is_priority()):
            # Sort the votes accordingly to the poll's settings
            if poll.poll_type == PollType.doodle.name:
                lines += get_doodle_vote_lines(poll, option, summarize)
            else:
                lines += get_vote_lines(poll, option, summarize)

    return lines
Exemplo n.º 3
0
def get_doodle_buttons(poll):
    """Get the doodle keyboard with yes, maybe and no button per option."""
    ignore_button_type = CallbackType.ignore.value
    vote_button_type = CallbackType.vote.value
    vote_yes = CallbackResult.yes.value
    vote_maybe = CallbackResult.maybe.value
    vote_no = CallbackResult.no.value

    options = poll.options
    if poll.option_sorting == OptionSorting.option_name:
        options = get_sorted_options(poll)

    buttons = []
    letters = string.ascii_lowercase
    for index, option in enumerate(options):
        ignore_payload = f'{ignore_button_type}:0:0'
        yes_payload = f'{vote_button_type}:{option.id}:{vote_yes}'
        maybe_payload = f'{vote_button_type}:{option.id}:{vote_maybe}'
        no_payload = f'{vote_button_type}:{option.id}:{vote_no}'
        buttons.append([
            InlineKeyboardButton(f'{letters[index]})', callback_data=ignore_payload),
            InlineKeyboardButton('✅', callback_data=yes_payload),
            InlineKeyboardButton('❔', callback_data=maybe_payload),
            InlineKeyboardButton('❌', callback_data=no_payload),
        ])

    return buttons
Exemplo n.º 4
0
def get_doodle_buttons(poll):
    """Get the doodle keyboard with yes, maybe and no button per option."""
    show_option_name = CallbackType.show_option_name.value
    vote_button_type = CallbackType.vote.value
    vote_yes = CallbackResult.yes.value
    vote_maybe = CallbackResult.maybe.value
    vote_no = CallbackResult.no.value

    options = get_sorted_options(poll)

    buttons = []
    indices = get_option_indices(options)

    for index, option in enumerate(options):
        name_hint_payload = f'{show_option_name}:{poll.id}:{option.id}'
        yes_payload = f'{vote_button_type}:{option.id}:{vote_yes}'
        maybe_payload = f'{vote_button_type}:{option.id}:{vote_maybe}'
        no_payload = f'{vote_button_type}:{option.id}:{vote_no}'

        # If we don't have the compact button view, display the option name on it's own button row
        if not poll.compact_buttons:
            option_row = [
                InlineKeyboardButton(option.get_formatted_name(),
                                     callback_data=name_hint_payload)
            ]
            buttons.append(option_row)
            option_row = []
        else:
            option_row = [
                InlineKeyboardButton(f'{indices[index]})',
                                     callback_data=name_hint_payload)
            ]

        vote_row = [
            InlineKeyboardButton('✅', callback_data=yes_payload),
            InlineKeyboardButton('❔', callback_data=maybe_payload),
            InlineKeyboardButton('❌', callback_data=no_payload),
        ]

        buttons.append(option_row + vote_row)

    return buttons
Exemplo n.º 5
0
def get_option_information(session, poll, context, summarize):
    lines = []
    # Sort the options accordingly to the polls settings
    options = get_sorted_options(poll, context.total_user_count)

    # All options with their respective people percentage
    for index, option in enumerate(options):
        lines.append('')
        lines.append(get_option_line(session, option, index))
        if option.description is not None:
            lines.append(f'┆ _{option.description}_')

        if context.show_results and context.show_percentage:
            lines.append(get_percentage_line(option, context))

        # Add the names of the voters to the respective options
        if context.show_results and not context.anonymous and len(option.votes) > 0:
            # Sort the votes accordingly to the poll's settings
            lines += get_vote_lines(poll, option, summarize)

    return lines
Exemplo n.º 6
0
def get_cumulative_buttons(poll):
    """Get the cumulative keyboard with two buttons per option."""
    vote_button_type = CallbackType.vote.value
    vote_yes = CallbackResult.yes.value
    vote_no = CallbackResult.no.value

    options = poll.options
    if poll.option_sorting == OptionSorting.option_name:
        options = get_sorted_options(poll)

    buttons = []
    for option in options:
        option_name = option.get_formatted_name()

        yes_payload = f'{vote_button_type}:{option.id}:{vote_yes}'
        no_payload = f'{vote_button_type}:{option.id}:{vote_no}'
        buttons.append([
            InlineKeyboardButton(f'- {option_name}', callback_data=no_payload),
            InlineKeyboardButton(f'+ {option_name}', callback_data=yes_payload),
        ])

    return buttons
Exemplo n.º 7
0
def get_normal_buttons(poll):
    """Get the normal keyboard with one vote button per option."""
    buttons = []
    vote_button_type = CallbackType.vote.value

    options = poll.options
    if poll.option_sorting == OptionSorting.option_name.name:
        options = get_sorted_options(poll)

    for option in options:
        option_name = option.get_formatted_name()

        result = CallbackResult.vote.value
        payload = f'{vote_button_type}:{option.id}:{result}'
        if poll.should_show_result() and poll.show_option_votes:
            text = i18n.t('keyboard.vote_with_count',
                          option_name=option_name,
                          count=len(option.votes),
                          locale=poll.locale)
        else:
            text = option_name
        buttons.append([InlineKeyboardButton(text, callback_data=payload)])

    return buttons