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_vote_keyboard(poll, user, show_back=False, summary=False): """Get a plain vote keyboard.""" buttons = [] # If the poll is not closed yet, add the vote buttons and the button # to add new options for new users (if enabled) if not poll.closed: buttons = get_vote_buttons(poll, user, show_back) bot_name = config["telegram"]["bot_name"] if poll.allow_new_options: payload = get_start_button_payload(poll, StartAction.new_option) url = f"http://t.me/{bot_name}?start={payload}" buttons.append([ InlineKeyboardButton(i18n.t("keyboard.new_option", locale=poll.locale), url=url) ]) if poll.allow_sharing: payload = get_start_button_payload(poll, StartAction.share_poll) url = f"http://t.me/{bot_name}?start={payload}" buttons.append([ InlineKeyboardButton(i18n.t("keyboard.share", locale=poll.locale), url=url) ]) # Add a button for to showing the summary, if the poll is too long for a single message if summary: payload = get_start_button_payload(poll, StartAction.show_results) bot_name = config["telegram"]["bot_name"] url = f"http://t.me/{bot_name}?start={payload}" row = [ InlineKeyboardButton(i18n.t("keyboard.show_results", locale=poll.locale), url=url) ] buttons.append(row) # Add a button to go back to the management interface (admin overview) if show_back: buttons.append([get_back_to_management_button(poll)]) return InlineKeyboardMarkup(buttons)
def get_vote_keyboard_with_summary(poll, show_back=False): """In case the poll has been summarized, add a deeplink to the bot.""" buttons = get_vote_buttons(poll, show_back) payload = get_start_button_payload(poll, StartAction.show_results) bot_name = config['telegram']['bot_name'] url = f'http://t.me/{bot_name}?start={payload}' buttons.append([InlineKeyboardButton( i18n.t('keyboard.show_results', locale=poll.locale), url=url)]) return InlineKeyboardMarkup(buttons)
def get_vote_buttons(poll, show_back=False): """Get the keyboard for actual voting.""" locale = poll.locale if poll_allows_cumulative_votes(poll): buttons = get_cumulative_buttons(poll) elif poll.poll_type == PollType.doodle.name: buttons = get_doodle_buttons(poll) else: buttons = get_normal_buttons(poll) if poll.allow_new_options: bot_name = config['telegram']['bot_name'] payload = get_start_button_payload(poll, StartAction.new_option) url = f'http://t.me/{bot_name}?start={payload}' buttons.append([ InlineKeyboardButton(i18n.t('keyboard.new_option', locale=locale), url=url) ]) return buttons
def get_vote_keyboard_with_summary(poll, show_back=False): """In case the poll has been summarized, add a deeplink to the bot.""" payload = get_start_button_payload(poll, StartAction.show_results) bot_name = config['telegram']['bot_name'] url = f'http://t.me/{bot_name}?start={payload}' row = [ InlineKeyboardButton(i18n.t('keyboard.show_results', locale=poll.locale), url=url) ] # If the poll is closed, only show the show results button if poll.closed and not poll.anonymous: buttons = [row] return InlineKeyboardMarkup(buttons) # Compile the keyboard from vote_buttons, back button and show summary button buttons = get_vote_buttons(poll, show_back) buttons.append(row) if show_back: buttons.append([get_back_to_management_button(poll)]) return InlineKeyboardMarkup(buttons)
def get_settings_text(poll): """Compile the options text for this poll.""" text = [] locale = poll.user.locale text.append( i18n.t('settings.poll_type', locale=locale, poll_type=translate_poll_type(poll.poll_type, locale))) text.append( i18n.t('settings.language', locale=locale, language=poll.locale)) if poll.anonymous: text.append(i18n.t('settings.anonymous', locale=locale)) else: text.append(i18n.t('settings.not_anonymous', locale=locale)) if poll.results_visible: text.append(i18n.t('settings.results_visible', locale=locale)) else: text.append(i18n.t('settings.results_not_visible', locale=locale)) if poll.due_date: text.append( i18n.t('settings.due_date', locale=locale, date=poll.get_formatted_due_date())) else: text.append(i18n.t('settings.no_due_date', locale=locale)) text.append('') text.append('*------- Styling -------*') text.append('') if poll.allow_new_options: text.append(i18n.t('settings.user_options', locale=locale)) else: text.append(i18n.t('settings.no_user_options', locale=locale)) if poll.results_visible: if poll.show_percentage: text.append(i18n.t('settings.percentage', locale=locale)) else: text.append(i18n.t('settings.no_percentage', locale=locale)) if poll.permanently_summarized: text.append( i18n.t('settings.permanently_summarized', locale=locale)) elif poll.summarize: text.append(i18n.t('settings.summarize', locale=locale)) else: text.append(i18n.t('settings.dont_summarize', locale=locale)) if poll.has_date_option(): if poll.european_date_format: text.append(i18n.t('settings.euro_date_format', locale=locale)) else: text.append(i18n.t('settings.us_date_format', locale=locale)) text.append('') # Sorting of user names if poll.poll_type == PollType.doodle.name: sorting_name = i18n.t(f'sorting.doodle_sorting', locale=locale) text.append( i18n.t('settings.user_sorting', locale=locale, name=sorting_name)) elif not poll.anonymous: sorting_name = i18n.t(f'sorting.{poll.user_sorting}', locale=locale) text.append( i18n.t('settings.user_sorting', locale=locale, name=sorting_name)) sorting_name = i18n.t(f'sorting.{poll.option_sorting}', locale=locale) text.append( i18n.t('settings.option_sorting', locale=locale, name=sorting_name)) text.append('') text.append( i18n.t('settings.sharing_link', locale=locale, name=sorting_name)) payload = get_start_button_payload(poll, StartAction.share_poll) bot_name = get_escaped_bot_name() text.append(f'https://t.me/{bot_name}?start={payload}') return '\n'.join(text)
def get_settings_text(poll): """Compile the options text for this poll.""" text = [] locale = poll.user.locale text.append( i18n.t( "settings.poll_type", locale=locale, poll_type=translate_poll_type(poll.poll_type, locale), )) text.append( i18n.t("settings.language", locale=locale, language=poll.locale)) if poll.anonymous: text.append(i18n.t("settings.anonymous", locale=locale)) elif not poll.is_priority(): text.append(i18n.t("settings.not_anonymous", locale=locale)) if poll.results_visible: text.append(i18n.t("settings.results_visible", locale=locale)) else: text.append(i18n.t("settings.results_not_visible", locale=locale)) if poll.due_date: text.append( i18n.t("settings.due_date", locale=locale, date=poll.get_formatted_due_date())) else: text.append(i18n.t("settings.no_due_date", locale=locale)) text.append("") text.append("*------- Styling -------*") text.append("") if poll.allow_new_options: text.append(i18n.t("settings.user_options", locale=locale)) else: text.append(i18n.t("settings.no_user_options", locale=locale)) if poll.results_visible: if poll.show_percentage: text.append(i18n.t("settings.percentage", locale=locale)) else: text.append(i18n.t("settings.no_percentage", locale=locale)) if poll.permanently_summarized: text.append( i18n.t("settings.permanently_summarized", locale=locale)) elif poll.summarize: text.append(i18n.t("settings.summarize", locale=locale)) else: text.append(i18n.t("settings.dont_summarize", locale=locale)) if poll.has_date_option(): if poll.european_date_format: text.append(i18n.t("settings.euro_date_format", locale=locale)) else: text.append(i18n.t("settings.us_date_format", locale=locale)) text.append("") # Sorting of user names if poll.poll_type == PollType.doodle.name: sorting_name = i18n.t("sorting.doodle", locale=locale) text.append( i18n.t("settings.user_sorting", locale=locale, name=sorting_name)) elif not poll.anonymous: sorting_name = i18n.t(f"sorting.{poll.user_sorting}", locale=locale) text.append( i18n.t("settings.user_sorting", locale=locale, name=sorting_name)) sorting_name = i18n.t(f"sorting.{poll.option_sorting}", locale=locale) text.append( i18n.t("settings.option_sorting", locale=locale, name=sorting_name)) bot_name = get_escaped_bot_name() if poll.allow_sharing: text.append("") text.append( i18n.t("settings.sharing_link", locale=locale, name=sorting_name)) payload = get_start_button_payload(poll, StartAction.share_poll) text.append(f"https://t.me/{bot_name}?start={payload}") if config["telegram"]["allow_private_vote"]: text.append("") text.append(i18n.t("settings.private_vote_link", locale=locale)) payload = get_start_button_payload(poll, StartAction.vote) text.append(f"https://t.me/{bot_name}?start={payload}") if config["telegram"]["allow_private_vote"] or poll.allow_sharing: text.append("") text.append(i18n.t("settings.link_warning", locale=locale)) return "\n".join(text)