Implement voting on a poll

pull/493/merge
Ivan Habunek 2025-08-05 15:41:06 +02:00
rodzic 91ef87e75f
commit 07170c21fa
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 01DB3DD0D824504C
3 zmienionych plików z 39 dodań i 7 usunięć

Wyświetl plik

@ -735,8 +735,13 @@ def get_lists(app, user):
return http.get(app, user, "/api/v1/lists").json()
def get_poll(app, user, poll_id):
return http.get(app, user, f"/api/v1/polls/{poll_id}").json()
def get_poll(app, user, poll_id) -> Response:
return http.get(app, user, f"/api/v1/polls/{poll_id}")
def vote_poll(app, user, poll_id, choices) -> Response:
json = {"choices": choices}
return http.post(app, user, f"/api/v1/polls/{poll_id}/votes", json=json)
def get_list_accounts(app, user, list_id):

Wyświetl plik

@ -169,11 +169,12 @@ json_option = click.option(
@click.group(context_settings=CONTEXT)
@click.option("-w", "--max-width", type=int, default=80, help="Maximum width for content rendered by toot")
@click.option("--debug/--no-debug", default=False, help="Log debug info to stderr")
@click.option("--verbose", is_flag=True, help="Log verbose info")
@click.option("--color/--no-color", default=sys.stdout.isatty(), help="Use ANSI color in output")
@click.option("--as", "as_user", type=AccountParamType(), help="The account to use, overrides the active account.")
@click.version_option(__version__, message="%(prog)s v%(version)s")
@click.pass_context
def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, as_user: str):
def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, verbose: bool, as_user: str):
"""Toot is a Mastodon CLI"""
ctx.obj = TootObj(color, debug, as_user)
ctx.color = color

Wyświetl plik

@ -1,10 +1,11 @@
import json as pyjson
from typing import Tuple
import click
from toot import api
from toot.cli import Context, cli, json_option, pass_context
from toot.entities import Poll, from_dict
from toot.entities import Poll, from_response
from toot.output import print_poll
@ -13,16 +14,41 @@ def polls():
"""Show and vote on polls"""
pass
@polls.command()
@click.argument("poll_id")
@json_option
@pass_context
def show(ctx: Context, poll_id: str, json: bool):
"""Show a poll by ID"""
data = api.get_poll(ctx.app, ctx.user, poll_id)
response = api.get_poll(ctx.app, ctx.user, poll_id)
if json:
click.echo(pyjson.dumps(data))
click.echo(response.text)
else:
poll = from_dict(Poll, data)
poll = from_response(Poll, response)
print_poll(poll)
@polls.command()
@click.argument("poll_id")
@click.argument("choices", type=int, nargs=-1, required=True)
@json_option
@pass_context
def vote(ctx: Context, poll_id: str, choices: Tuple[int], json: bool):
"""Vote on a poll
CHOICES is one or more zero-indexed integers corresponding to the desired poll choices.
e.g. to vote for the first and third options use:
toot polls vote <poll_id> 0 2
"""
response = api.vote_poll(ctx.app, ctx.user, poll_id, choices)
if json:
click.echo(response.text)
else:
poll = from_response(Poll, response)
click.echo("You voted!\n")
print_poll(poll)