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
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
def get_option_line(session, option, index): """Get the line with vote count for this option.""" # Special formating for polls with European date format option_name = option.get_formatted_name() prefix = "" if option.poll.poll_type in [PollType.doodle.name, PollType.priority.name]: indices = get_option_indices(option.poll.options) prefix = f"{indices[index]}) " if (len(option.votes) > 0 and option.poll.should_show_result() and option.poll.show_option_votes and not option.poll.is_priority()): if poll_allows_cumulative_votes(option.poll): vote_count = sum([vote.vote_count for vote in option.votes]) else: vote_count = len(option.votes) return f"┌ {prefix}*{option_name}* ({vote_count} votes)" else: return f"┌ {prefix}*{option_name}*"