예제 #1
0
def respond_to_vote(session,
                    line,
                    context,
                    poll,
                    remaining_votes=None,
                    limited=False):
    """Get the formatted response for a user."""
    locale = poll.locale
    votes = (session.query(Vote).filter(Vote.user == context.user).filter(
        Vote.poll == poll).all())

    if limited:
        line += i18n.t("callback.vote.votes_left",
                       locale=locale,
                       count=remaining_votes)

    lines = [line]
    lines.append(i18n.t("callback.vote.your_votes", locale=locale))
    for vote in votes:
        if poll_allows_cumulative_votes(poll):
            lines.append(f" {vote.option.name} ({vote.vote_count}), ")
        else:
            lines.append(f" {vote.option.name}")

    message = "".join(lines)

    # Inline query responses cannot be longer than 200 characters
    # Restrict it, since we get an MessageTooLong error otherwise
    if len(message) > 190:
        message = message[0:190]

    context.query.answer(message)
예제 #2
0
def calculate_percentage(option, total_user_count):
    """Calculate the percentage for this option."""
    # Return 0 if:
    # - No voted on this poll yet
    # - This option has no votes
    if total_user_count == 0:
        return 0
    if len(option.votes) == 0:
        return 0

    poll_vote_count = sum([vote.vote_count for vote in option.poll.votes])
    if poll_vote_count == 0:
        return 0

    if poll_allows_cumulative_votes(option.poll):
        option_vote_count = sum([vote.vote_count for vote in option.votes])

        percentage = round(option_vote_count / poll_vote_count * 100)

    elif option.poll.poll_type == PollType.doodle.name:
        score = 0
        for vote in option.votes:
            if vote.type == VoteResultType.yes.name:
                score += 1
            elif vote.type == VoteResultType.maybe.name:
                score += 0.5

        return score / total_user_count * 100
    else:
        percentage = len(option.votes) / total_user_count * 100

    return percentage
예제 #3
0
def get_vote_buttons(poll, user=None, show_back=False):
    """Get the keyboard for actual voting."""
    if poll_allows_cumulative_votes(poll):
        buttons = get_cumulative_buttons(poll)
    elif poll.poll_type == PollType.doodle.name:
        buttons = get_doodle_buttons(poll)
    elif poll.is_priority():
        buttons = get_priority_buttons(poll, user)
    else:
        buttons = get_normal_buttons(poll)

    return buttons
예제 #4
0
def get_vote_line(poll, option, vote, index):
    """Get the line showing an actual vote."""
    if vote.user is None:
        user_mention = "GDPR removed user"
    else:
        user_mention = f"[{vote.user.name}](tg://user?id={vote.user.id})"

    if index == (len(option.votes) - 1):
        vote_line = f"└ {user_mention}"
    else:
        vote_line = f"├ {user_mention}"

    if poll_allows_cumulative_votes(poll):
        vote_line += f" ({vote.vote_count} votes)"
    elif poll.poll_type == PollType.doodle.name:
        vote_line += f" ({vote.type})"

    return vote_line
예제 #5
0
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}*"