mirror of
https://github.com/ihabunek/toot.git
synced 2025-02-02 15:07:51 -05:00
Fix matching list by ID
This commit is contained in:
parent
a568980f9f
commit
89ea4604e5
@ -1,9 +1,11 @@
|
|||||||
import click
|
|
||||||
import json as pyjson
|
import json as pyjson
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
from toot import api, config
|
from toot import api, config
|
||||||
from toot.cli import Context, cli, pass_context, json_option
|
from toot.cli import Context, cli, json_option, pass_context
|
||||||
from toot.entities import from_dict_list, List
|
from toot.entities import List, from_dict_list
|
||||||
from toot.output import print_list_accounts, print_lists, print_warning
|
from toot.output import print_list_accounts, print_lists, print_warning
|
||||||
|
|
||||||
|
|
||||||
@ -49,7 +51,7 @@ def list(ctx: Context, json: bool):
|
|||||||
@click.option("--id", help="List ID if not title is given")
|
@click.option("--id", help="List ID if not title is given")
|
||||||
@json_option
|
@json_option
|
||||||
@pass_context
|
@pass_context
|
||||||
def accounts(ctx: Context, title: str, id: str, json: bool):
|
def accounts(ctx: Context, title: str, id: Optional[str], json: bool):
|
||||||
"""List the accounts in a list"""
|
"""List the accounts in a list"""
|
||||||
list_id = _get_list_id(ctx, title, id)
|
list_id = _get_list_id(ctx, title, id)
|
||||||
response = api.get_list_accounts(ctx.app, ctx.user, list_id)
|
response = api.get_list_accounts(ctx.app, ctx.user, list_id)
|
||||||
@ -84,7 +86,7 @@ def create(ctx: Context, title: str, replies_policy: str, json: bool):
|
|||||||
@click.option("--id", help="List ID if not title is given")
|
@click.option("--id", help="List ID if not title is given")
|
||||||
@json_option
|
@json_option
|
||||||
@pass_context
|
@pass_context
|
||||||
def delete(ctx: Context, title: str, id: str, json: bool):
|
def delete(ctx: Context, title: str, id: Optional[str], json: bool):
|
||||||
"""Delete a list"""
|
"""Delete a list"""
|
||||||
list_id = _get_list_id(ctx, title, id)
|
list_id = _get_list_id(ctx, title, id)
|
||||||
response = api.delete_list(ctx.app, ctx.user, list_id)
|
response = api.delete_list(ctx.app, ctx.user, list_id)
|
||||||
@ -100,7 +102,7 @@ def delete(ctx: Context, title: str, id: str, json: bool):
|
|||||||
@click.option("--id", help="List ID if not title is given")
|
@click.option("--id", help="List ID if not title is given")
|
||||||
@json_option
|
@json_option
|
||||||
@pass_context
|
@pass_context
|
||||||
def add(ctx: Context, title: str, account: str, id: str, json: bool):
|
def add(ctx: Context, title: str, account: str, id: Optional[str], json: bool):
|
||||||
"""Add an account to a list"""
|
"""Add an account to a list"""
|
||||||
list_id = _get_list_id(ctx, title, id)
|
list_id = _get_list_id(ctx, title, id)
|
||||||
found_account = api.find_account(ctx.app, ctx.user, account)
|
found_account = api.find_account(ctx.app, ctx.user, account)
|
||||||
@ -133,7 +135,7 @@ def add(ctx: Context, title: str, account: str, id: str, json: bool):
|
|||||||
@click.option("--id", help="List ID if not title is given")
|
@click.option("--id", help="List ID if not title is given")
|
||||||
@json_option
|
@json_option
|
||||||
@pass_context
|
@pass_context
|
||||||
def remove(ctx: Context, title: str, account: str, id: str, json: bool):
|
def remove(ctx: Context, title: str, account: str, id: Optional[str], json: bool):
|
||||||
"""Remove an account from a list"""
|
"""Remove an account from a list"""
|
||||||
list_id = _get_list_id(ctx, title, id)
|
list_id = _get_list_id(ctx, title, id)
|
||||||
found_account = api.find_account(ctx.app, ctx.user, account)
|
found_account = api.find_account(ctx.app, ctx.user, account)
|
||||||
@ -151,7 +153,7 @@ def remove(ctx: Context, title: str, account: str, id: str, json: bool):
|
|||||||
@click.argument("title", required=False)
|
@click.argument("title", required=False)
|
||||||
@click.option("--id", help="List ID if not title is given")
|
@click.option("--id", help="List ID if not title is given")
|
||||||
@pass_context
|
@pass_context
|
||||||
def list_accounts(ctx: Context, title: str, id: str):
|
def list_accounts(ctx: Context, title: str, id: Optional[str]):
|
||||||
"""List the accounts in a list"""
|
"""List the accounts in a list"""
|
||||||
print_warning("`toot list_accounts` is deprecated in favour of `toot lists accounts`")
|
print_warning("`toot list_accounts` is deprecated in favour of `toot lists accounts`")
|
||||||
list_id = _get_list_id(ctx, title, id)
|
list_id = _get_list_id(ctx, title, id)
|
||||||
@ -179,7 +181,7 @@ def list_create(ctx: Context, title: str, replies_policy: str):
|
|||||||
@click.argument("title", required=False)
|
@click.argument("title", required=False)
|
||||||
@click.option("--id", help="List ID if not title is given")
|
@click.option("--id", help="List ID if not title is given")
|
||||||
@pass_context
|
@pass_context
|
||||||
def list_delete(ctx: Context, title: str, id: str):
|
def list_delete(ctx: Context, title: str, id: Optional[str]):
|
||||||
"""Delete a list"""
|
"""Delete a list"""
|
||||||
print_warning("`toot list_delete` is deprecated in favour of `toot lists delete`")
|
print_warning("`toot list_delete` is deprecated in favour of `toot lists delete`")
|
||||||
list_id = _get_list_id(ctx, title, id)
|
list_id = _get_list_id(ctx, title, id)
|
||||||
@ -192,7 +194,7 @@ def list_delete(ctx: Context, title: str, id: str):
|
|||||||
@click.argument("account")
|
@click.argument("account")
|
||||||
@click.option("--id", help="List ID if not title is given")
|
@click.option("--id", help="List ID if not title is given")
|
||||||
@pass_context
|
@pass_context
|
||||||
def list_add(ctx: Context, title: str, account: str, id: str):
|
def list_add(ctx: Context, title: str, account: str, id: Optional[str]):
|
||||||
"""Add an account to a list"""
|
"""Add an account to a list"""
|
||||||
print_warning("`toot list_add` is deprecated in favour of `toot lists add`")
|
print_warning("`toot list_add` is deprecated in favour of `toot lists add`")
|
||||||
list_id = _get_list_id(ctx, title, id)
|
list_id = _get_list_id(ctx, title, id)
|
||||||
@ -222,7 +224,7 @@ def list_add(ctx: Context, title: str, account: str, id: str):
|
|||||||
@click.argument("account")
|
@click.argument("account")
|
||||||
@click.option("--id", help="List ID if not title is given")
|
@click.option("--id", help="List ID if not title is given")
|
||||||
@pass_context
|
@pass_context
|
||||||
def list_remove(ctx: Context, title: str, account: str, id: str):
|
def list_remove(ctx: Context, title: Optional[str], account: str, id: Optional[str]):
|
||||||
"""Remove an account from a list"""
|
"""Remove an account from a list"""
|
||||||
print_warning("`toot list_remove` is deprecated in favour of `toot lists remove`")
|
print_warning("`toot list_remove` is deprecated in favour of `toot lists remove`")
|
||||||
list_id = _get_list_id(ctx, title, id)
|
list_id = _get_list_id(ctx, title, id)
|
||||||
@ -231,15 +233,12 @@ def list_remove(ctx: Context, title: str, account: str, id: str):
|
|||||||
click.secho(f"✓ Removed account \"{account}\"", fg="green")
|
click.secho(f"✓ Removed account \"{account}\"", fg="green")
|
||||||
|
|
||||||
|
|
||||||
def _get_list_id(ctx: Context, title, list_id):
|
def _get_list_id(ctx: Context, title: Optional[str], list_id: Optional[str]):
|
||||||
if not list_id and not title:
|
if not list_id and not title:
|
||||||
raise click.ClickException("Please specify list title or ID")
|
raise click.ClickException("Please specify list title or ID")
|
||||||
|
|
||||||
lists = api.get_lists(ctx.app, ctx.user)
|
lists = api.get_lists(ctx.app, ctx.user)
|
||||||
matched_ids = [
|
matched_ids = [list["id"] for list in lists if _matches(list, title, list_id)]
|
||||||
list["id"] for list in lists
|
|
||||||
if list["title"].lower() == title.lower() or list["id"] == list_id
|
|
||||||
]
|
|
||||||
|
|
||||||
if not matched_ids:
|
if not matched_ids:
|
||||||
raise click.ClickException("List not found")
|
raise click.ClickException("List not found")
|
||||||
@ -248,3 +247,7 @@ def _get_list_id(ctx: Context, title, list_id):
|
|||||||
raise click.ClickException("Found multiple lists with the same title, please specify the ID instead")
|
raise click.ClickException("Found multiple lists with the same title, please specify the ID instead")
|
||||||
|
|
||||||
return matched_ids[0]
|
return matched_ids[0]
|
||||||
|
|
||||||
|
|
||||||
|
def _matches(list, title, list_id):
|
||||||
|
return (title and title.lower() == list["title"].lower()) or (list_id and list_id == list["id"])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user