From 90e596706c5961b2660ea48ac12c6b63481ee677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Bu=C3=9Fmann?= Date: Mon, 23 Sep 2024 12:01:06 +0200 Subject: [PATCH] added a way to remove deleted tasks in reclaim --- things2reclaim/main.py | 46 ++++++++++++++++++++++++++++++- things2reclaim/reclaim_handler.py | 20 ++++++++++++++ things2reclaim/things_handler.py | 8 ++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/things2reclaim/main.py b/things2reclaim/main.py index 47ef616..a0c9b56 100755 --- a/things2reclaim/main.py +++ b/things2reclaim/main.py @@ -307,6 +307,23 @@ def print_time_needed(subject: Annotated[Optional[str], typer.Argument()] = None ) +@app.command("remove") +def remove_task( + task_name_parts: Annotated[List[str], typer.Argument(help="Task to start")] + ): + task_name = (" ").join(task_name_parts) + task = reclaim_handler.get_reclaim_task_fuzzy(task_name) + if task is None: + things_task = things_handler.get_task_by_name(task_name) + if things_task is None: + utils.perror(f"No task with name {task_name} found in things") + return + else: + task = things_task["uuid"] + finish_task(task) + utils.pinfo("Removed task") + + @app.command("finished") def remove_finished_tasks_from_things(dry_run: bool = False): """ @@ -387,12 +404,39 @@ def display_current_task(): .astimezone(tz.gettz()).strftime("%H:%M")}", ) +@app.command("removeDeleted") +def removeDeletedTasks(dry_run: bool = False): + """ + Removes all tasks from reclaim that were deleted in things + """ + with UploadedTasksDB(DATABASE_PATH) as db: + uploaded_task_ids = db.get_all_uploaded_tasks() + things_task_ids = [task["uuid"] for task in things_handler.get_all_things_tasks()] + ids_to_be_removed = [ + id + for id in uploaded_task_ids + if id not in things_task_ids + ] + if len(ids_to_be_removed) == 0: + utils.pinfo("No deleted tasks found") + else: + utils.pinfo(f"Delting {len(ids_to_be_removed)} removed tasks in reclaim") + with UploadedTasksDB(DATABASE_PATH) as db: + for task_id in ids_to_be_removed: + reclaim_task = reclaim_handler.get_by_things_id(task_id) + if reclaim_task is None: + continue + utils.pinfo(f"Removing {reclaim_task.name}") + if not dry_run: + reclaim_handler.finish_task(reclaim_task) + db.remove_uploaded_task(task_id) + @app.command("sync") def sync_things_and_reclaim(dry_run: bool = False): """ Sync tasks between things and reclaim - First updated all finished tasks in reclaim to completed in things + First update all finished tasks in reclaim to completed in things Then upload all new tasks from things to reclaim """ utils.pinfo("Pulling from Reclaim") diff --git a/things2reclaim/reclaim_handler.py b/things2reclaim/reclaim_handler.py index 1be4837..77e4c69 100644 --- a/things2reclaim/reclaim_handler.py +++ b/things2reclaim/reclaim_handler.py @@ -36,6 +36,16 @@ def get_reclaim_task(name: str) -> Optional[ReclaimTask]: return res[0] +def get_reclaim_task_fuzzy(task_name: str) -> Optional[ReclaimTask]: + tasks: Dict[str, ReclaimTask] = { + task.name: task for task in get_reclaim_tasks() + } + if task_name not in tasks.keys(): + return utils.get_closest_match(task_name, tasks) + else: + return tasks[task_name] + + def get_reclaim_tasks() -> List[ReclaimTask]: return ReclaimTask.search() @@ -133,6 +143,16 @@ def finish_task(task: ReclaimTask): task.mark_complete() +def get_by_things_id(id: str): + tasks = [task for task in ReclaimTask.search() if get_things_id(task) == id] + if len(tasks) == 0: + return None + elif len(tasks) == 1: + return tasks[0] + else: + raise ValueError("multiple reclaims tasks are mapped to the same things id") + + def get_things_id(task: ReclaimTask): things_id_match = things_id_pattern.match(task.description) if things_id_match is None: diff --git a/things2reclaim/things_handler.py b/things2reclaim/things_handler.py index c64fa0e..b6a062c 100644 --- a/things2reclaim/things_handler.py +++ b/things2reclaim/things_handler.py @@ -24,6 +24,14 @@ def get_task(task_id: str): return things.get(task_id) +def get_task_by_name(task_name: str): + tasks = {task["name"] : task for task in get_all_things_tasks()} + if task_name not in tasks.keys(): + return utils.get_closest_match(task_name, tasks) + else: + return tasks[task_name] + + def complete(task_id: str): things.complete(task_id)