From 039e6739b8cfcb9ed151114c81db362494dd9802 Mon Sep 17 00:00:00 2001 From: lcfd <9001053+fedriz@users.noreply.github.com> Date: Wed, 13 Dec 2023 13:26:11 +0100 Subject: [PATCH] Add works done command --- cli/trakcli/works/__init__.py | 2 + cli/trakcli/works/commands/delete.py | 2 +- cli/trakcli/works/commands/done.py | 73 ++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 cli/trakcli/works/commands/done.py diff --git a/cli/trakcli/works/__init__.py b/cli/trakcli/works/__init__.py index 1c6376d..2c63bf3 100644 --- a/cli/trakcli/works/__init__.py +++ b/cli/trakcli/works/__init__.py @@ -1,6 +1,7 @@ import typer from trakcli.works.commands.delete import delete_work +from trakcli.works.commands.done import done_work from trakcli.works.commands.list import list_works app = typer.Typer() @@ -8,3 +9,4 @@ app.command("list")(list_works) app.command("delete")(delete_work) +app.command("done")(done_work) diff --git a/cli/trakcli/works/commands/delete.py b/cli/trakcli/works/commands/delete.py index 15d0348..cbca720 100644 --- a/cli/trakcli/works/commands/delete.py +++ b/cli/trakcli/works/commands/delete.py @@ -19,7 +19,7 @@ def delete_work( project_id: Annotated[ str, typer.Option( - "--from", "-f", "-p", help="The project's id in which the work is located." + "--in", "--of", "-p", help="The project's id in which the work is located." ), ], ): diff --git a/cli/trakcli/works/commands/done.py b/cli/trakcli/works/commands/done.py new file mode 100644 index 0000000..18548d6 --- /dev/null +++ b/cli/trakcli/works/commands/done.py @@ -0,0 +1,73 @@ +from typing import Annotated + +import typer +from rich import print as rprint +from rich.panel import Panel +from rich.prompt import Confirm + +from trakcli.projects.database import get_projects_from_config +from trakcli.projects.utils.print_missing_project import print_missing_project +from trakcli.utils.print_with_padding import print_with_padding +from trakcli.works.database import ( + get_project_works_from_config, + set_project_works_in_config, +) + + +def done_work( + work_id: Annotated[str, typer.Argument()], + project_id: Annotated[ + str, + typer.Option( + "--in", "--of", "-p", help="The project's id in which the work is located." + ), + ], +): + """Mark as done a work of a project.""" + + projects = get_projects_from_config() + + if project_id in projects: + confirm_done = Confirm.ask( + f"Are you sure you want to mark the [green]{work_id}[/green] work from [green]{project_id}[/green] project as done?", + default=False, + ) + if not confirm_done: + rprint("") + rprint("[yellow]Not marked as done.[/yellow]") + raise typer.Abort() + + works = get_project_works_from_config(project_id) + if works is not None: + works_ids = [w["id"] for w in works] + if work_id in works_ids: + filtered_works = [ + {**w, "done": True} if w["id"] == work_id else w for w in works + ] + + set_project_works_in_config(project_id, filtered_works) + + rprint("") + rprint( + Panel.fit( + title="[green]Success[/green]", + renderable=f"Work {work_id} successfully from {project_id} project marked as done.", + ) + ) + else: + rprint("") + rprint( + Panel.fit( + title="[red]The work doesn't exist[/red]", + renderable=print_with_padding( + ( + "You can create a new work with the command:\n" + "trak create work -p -n -t --from 2024-01-01 --to 2024-02-01" + ) + ), + ) + ) + + return + else: + print_missing_project(projects)