kopia lustrzana https://github.com/ihabunek/toot
Implement voting on a poll
rodzic
91ef87e75f
commit
07170c21fa
|
@ -735,8 +735,13 @@ def get_lists(app, user):
|
||||||
return http.get(app, user, "/api/v1/lists").json()
|
return http.get(app, user, "/api/v1/lists").json()
|
||||||
|
|
||||||
|
|
||||||
def get_poll(app, user, poll_id):
|
def get_poll(app, user, poll_id) -> Response:
|
||||||
return http.get(app, user, f"/api/v1/polls/{poll_id}").json()
|
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):
|
def get_list_accounts(app, user, list_id):
|
||||||
|
|
|
@ -169,11 +169,12 @@ json_option = click.option(
|
||||||
@click.group(context_settings=CONTEXT)
|
@click.group(context_settings=CONTEXT)
|
||||||
@click.option("-w", "--max-width", type=int, default=80, help="Maximum width for content rendered by toot")
|
@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("--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("--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.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.version_option(__version__, message="%(prog)s v%(version)s")
|
||||||
@click.pass_context
|
@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"""
|
"""Toot is a Mastodon CLI"""
|
||||||
ctx.obj = TootObj(color, debug, as_user)
|
ctx.obj = TootObj(color, debug, as_user)
|
||||||
ctx.color = color
|
ctx.color = color
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import json as pyjson
|
import json as pyjson
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
from toot import api
|
from toot import api
|
||||||
from toot.cli import Context, cli, json_option, pass_context
|
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
|
from toot.output import print_poll
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,16 +14,41 @@ def polls():
|
||||||
"""Show and vote on polls"""
|
"""Show and vote on polls"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@polls.command()
|
@polls.command()
|
||||||
@click.argument("poll_id")
|
@click.argument("poll_id")
|
||||||
@json_option
|
@json_option
|
||||||
@pass_context
|
@pass_context
|
||||||
def show(ctx: Context, poll_id: str, json: bool):
|
def show(ctx: Context, poll_id: str, json: bool):
|
||||||
"""Show a poll by ID"""
|
"""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:
|
if json:
|
||||||
click.echo(pyjson.dumps(data))
|
click.echo(response.text)
|
||||||
else:
|
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)
|
print_poll(poll)
|
||||||
|
|
Ładowanie…
Reference in New Issue