diff options
author | Jason Lee <changjin9792@gmail.com> | 2023-04-03 19:19:11 +0800 |
---|---|---|
committer | Jason Lee <changjin9792@gmail.com> | 2023-04-03 19:19:11 +0800 |
commit | 07e2ef901e5faf1b9e76d68772fa1488b2f51e5c (patch) | |
tree | 8493e4baec928eefa70efdfc0744d5d2cac6a618 | |
parent | 3acf96c72e926f60109c2ea5d29208e7b28a76e3 (diff) |
[Feat] Guest modefeat/guest-mode
issue #28
-rw-r--r-- | girok/api/category.py | 115 | ||||
-rw-r--r-- | girok/api/task.py | 257 | ||||
-rw-r--r-- | girok/calendar_cli/calendar_container.py | 78 | ||||
-rw-r--r-- | girok/calendar_cli/sidebar.py | 10 | ||||
-rw-r--r-- | girok/commands/task.py | 67 | ||||
-rw-r--r-- | girok/girok.py | 30 | ||||
-rw-r--r-- | girok/server/src/category/router.py | 24 | ||||
-rw-r--r-- | girok/server/src/category/service.py | 2 | ||||
-rw-r--r-- | girok/server/src/task/router.py | 20 | ||||
-rw-r--r-- | girok/server/src/task/schemas.py | 23 | ||||
-rw-r--r-- | girok/utils/calendar.py | 4 | ||||
-rw-r--r-- | girok/utils/display.py | 4 | ||||
-rw-r--r-- | girok/utils/general.py | 5 |
13 files changed, 366 insertions, 273 deletions
diff --git a/girok/api/category.py b/girok/api/category.py index 910bf8d..36dffbf 100644 --- a/girok/api/category.py +++ b/girok/api/category.py @@ -171,52 +171,79 @@ def move_category(cat_str: str, new_parent_cat_str: str): def get_last_cat_id(cats: list): - resp = requests.get( - cfg.base_url + "/categories/last-cat-id", - json={ - "cats": cats - }, - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - if resp.status_code == 200: - return general_utils.bytes2dict(resp.content)['cat_id'] - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) - exit(0) - else: - display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) - exit(0) + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.get( + cfg.base_url + "/categories/last-cat-id", + json={ + "cats": cats + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 200: + return general_utils.bytes2dict(resp.content)['cat_id'] + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + exit(0) + else: + display_utils.center_print(resp.content, type="error") + exit(0) + elif mode == "guest": + resp = category_router.get_last_cat_id({"cats": cats}) + if resp['success']: + return resp['cat_id'] + else: + display_utils.center_print(resp['detail'], type="error") + exit(0) + def get_category_color(cat_id: int): - resp = requests.get( - cfg.base_url + f"/categories/{cat_id}/color", - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - - if resp.status_code == 200: - return general_utils.bytes2dict(resp.content)['color'] - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) - exit(0) - else: - display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) - exit(0) - + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.get( + cfg.base_url + f"/categories/{cat_id}/color", + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + + if resp.status_code == 200: + return general_utils.bytes2dict(resp.content)['color'] + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + exit(0) + else: + display_utils.center_print(resp.content, type="error") + exit(0) + elif mode == "guest": + resp = category_router.get_category_color(cat_id) + if resp['success']: + return resp['color'] + else: + display_utils.center_print(resp.content, type="error") + exit(0) def get_color_dict(): - resp = requests.get( - cfg.base_url + "/categories/color", - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - if resp.status_code == 200: - color_dict = general_utils.bytes2dict(resp.content) - return color_dict - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) - else: - display_utils.center_print("Error occurred.", constants.DISPLAY_TERMINAL_COLOR_ERROR) -
\ No newline at end of file + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.get( + cfg.base_url + "/categories/color", + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 200: + color_dict = general_utils.bytes2dict(resp.content) + return color_dict + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + else: + display_utils.center_print("Error occurred.", type="error") + elif mode == "guest": + resp = category_router.get_category_colors_dict() + if resp['success']: + return resp['colors'] + else: + display_utils.center_print("Error occurred.", type="error") + exit(0) +
\ No newline at end of file diff --git a/girok/api/task.py b/girok/api/task.py index ad5813b..32d9180 100644 --- a/girok/api/task.py +++ b/girok/api/task.py @@ -42,7 +42,8 @@ def create_task(task_data: dict): else: display_utils.center_print(resp['detail'], type="error") exit(0) - + exit(0) + def get_single_task(task_id: int): mode = auth_utils.get_mode(cfg.config_path) @@ -66,7 +67,6 @@ def get_single_task(task_id: int): return task else: display_utils.center_print(resp['detail'], type="error") - exit(0) @@ -95,7 +95,15 @@ def get_tasks( headers=auth_utils.build_jwt_header(cfg.config_path), params=query_str_obj ) - return general_utils.bytes2dict(resp.content)['tasks'] + if resp.status_code == 200: + return general_utils.bytes2dict(resp.content)['tasks'] + if resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + exit(0) + elif resp.status_code: + display_utils.center_print("Error occurred.", type="title") + exit(0) elif mode == "guest": resp = task_router.get_tasks(query_str_obj) if resp['success']: @@ -107,100 +115,177 @@ def get_tasks( def remove_task(task_id: int): - resp = requests.delete( - cfg.base_url + f"/tasks/{task_id}", - headers=auth_utils.build_jwt_header(cfg.config_path), - ) - return resp - + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.delete( + cfg.base_url + f"/tasks/{task_id}", + headers=auth_utils.build_jwt_header(cfg.config_path), + ) + if resp.status_code == 204: + return True + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + else: + display_utils.center_print(resp.content, type="error") + elif mode == "guest": + resp = task_router.delete_task(task_id) + if resp['success']: + return True + else: + display_utils.center_print(resp['detail'], type="error") + exit(0) def get_tags(): - resp = requests.get( - cfg.base_url + "/tasks/tags", - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - - return resp - + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.get( + cfg.base_url + "/tasks/tags", + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 200: + return general_utils.bytes2dict(resp.content)['tags'] + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + exit(0) + else: + exit(0) + elif mode == "guest": + resp = task_router.get_tags() + if resp['success']: + return resp['tags'] + else: + display_utils.center_print(resp['detail'], type="error") + exit(0) + def change_task_tag(task_id: int, new_tag_name: str): - resp = requests.patch( - cfg.base_url + f"/tasks/{task_id}/tag", - headers=auth_utils.build_jwt_header(cfg.config_path), - json={ - "new_tag_name": new_tag_name - } - ) - - if resp.status_code == 200: - task = general_utils.bytes2dict(resp.content) - task_name = task['name'] - display_utils.center_print(f"Successfully changed [{task_name}]'s tag to {new_tag_name}.", type="success") - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) - else: - display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.patch( + cfg.base_url + f"/tasks/{task_id}/tag", + headers=auth_utils.build_jwt_header(cfg.config_path), + json={ + "new_tag_name": new_tag_name + } + ) + + if resp.status_code == 200: + task = general_utils.bytes2dict(resp.content) + task_name = task['name'] + display_utils.center_print(f"Successfully changed [{task_name}]'s tag to {new_tag_name}.", type="success") + return + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + else: + display_utils.center_print(resp.content, type="error") + elif mode == "guest": + resp = task_router.change_task_tag(task_id, {"new_tag_name": new_tag_name}) + if resp['success']: + task = resp['updated_task'] + task_name = task['name'] + display_utils.center_print(f"Successfully changed [{task_name}]'s tag to {new_tag_name}.", type="success") + return + else: + display_utils.center_print(resp['detail'], constants.DISPLAY_TERMINAL_COLOR_ERROR) + exit(0) def change_task_priority(task_id: int, new_priority: int): - resp = requests.patch( - cfg.base_url + f"/tasks/{task_id}/priority", - headers=auth_utils.build_jwt_header(cfg.config_path), - json={ - "new_priority": new_priority - } - ) - - if resp.status_code == 200: - task = general_utils.bytes2dict(resp.content) - task_name = task['name'] - display_utils.center_print(f"Successfully changed [{task_name}]'s priority to {new_priority}.", type="success") - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) - else: - display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.patch( + cfg.base_url + f"/tasks/{task_id}/priority", + headers=auth_utils.build_jwt_header(cfg.config_path), + json={ + "new_priority": new_priority + } + ) + + if resp.status_code == 200: + task = general_utils.bytes2dict(resp.content) + task_name = task['name'] + display_utils.center_print(f"Successfully changed [{task_name}]'s priority to {new_priority}.", type="success") + return + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + else: + display_utils.center_print(resp.content, type="error") + elif mode == "guest": + resp = task_router.change_task_priority(task_id, {"new_priority": new_priority}) + if resp['success']: + task = resp['updated_task'] + task_name = task['name'] + display_utils.center_print(f"Successfully changed [{task_name}]'s priority to {new_priority}.", type="success") + return + else: + display_utils.center_print(resp['detail'], type="error") + exit(0) + def change_task_name(task_id: int, new_name: str): - resp = requests.patch( - cfg.base_url + f"/tasks/{task_id}/name", - headers=auth_utils.build_jwt_header(cfg.config_path), - json={ - "new_name": new_name - } - ) - - if resp.status_code == 200: - task = general_utils.bytes2dict(resp.content) - task_name = task['name'] - display_utils.center_print(f"Successfully changed [{task_name}]'s name to {new_name}.", type="success") - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) - else: - display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.patch( + cfg.base_url + f"/tasks/{task_id}/name", + headers=auth_utils.build_jwt_header(cfg.config_path), + json={ + "new_name": new_name + } + ) + + if resp.status_code == 200: + task = general_utils.bytes2dict(resp.content) + task_name = task['name'] + display_utils.center_print(f"Successfully changed [{task_name}]'s name to {new_name}.", type="success") + return + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + else: + display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + elif mode == "guest": + resp = task_router.change_task_name(task_id, {"new_name": new_name}) + if resp['success']: + task = resp['updated_task'] + task_name = task['name'] + display_utils.center_print(f"Successfully changed [{task_name}]'s name to {new_name}.", type="success") + return + else: + display_utils.center_print(resp['detail'], constants.DISPLAY_TERMINAL_COLOR_ERROR) + exit(0) def change_task_date(task_id: int, new_date: str): - resp = requests.patch( - cfg.base_url + f"/tasks/{task_id}/date", - headers=auth_utils.build_jwt_header(cfg.config_path), - json={ - "new_date": new_date - } - ) - - if resp.status_code == 200: - task = general_utils.bytes2dict(resp.content) - task_name = task['name'] - display_utils.center_print(f"Successfully changed [{task_name}]'s date to {new_date.split()[0]}.", type="success") - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) - else: - display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.patch( + cfg.base_url + f"/tasks/{task_id}/date", + headers=auth_utils.build_jwt_header(cfg.config_path), + json={ + "new_date": new_date + } + ) - - + if resp.status_code == 200: + task = general_utils.bytes2dict(resp.content) + task_name = task['name'] + display_utils.center_print(f"Successfully changed [{task_name}]'s date to {new_date.split()[0]}.", type="success") + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + else: + display_utils.center_print(resp.content, type="error") + elif mode == "guest": + resp = task_router.change_task_date(task_id, {"new_date": new_date}) + if resp['success']: + task = resp['updated_task'] + task_name = task['name'] + display_utils.center_print(f"Successfully changed [{task_name}]'s date to {new_date.split()[0]}.", type="success") + else: + display_utils.center_print(resp['detail'], type="error") + exit(0)
\ No newline at end of file diff --git a/girok/calendar_cli/calendar_container.py b/girok/calendar_cli/calendar_container.py index 2ece156..6003542 100644 --- a/girok/calendar_cli/calendar_container.py +++ b/girok/calendar_cli/calendar_container.py @@ -217,12 +217,9 @@ class Calendar(Container): tag = None else: tag = self.tag - - log("CATS", cat_list), - log("tag", tag) start_date, end_date = task_utils.build_time_window_by_year_and_month(self.year, self.month) - resp = task_api.get_tasks( + tasks = task_api.get_tasks( cats=cat_list, start_date=start_date, end_date=end_date, @@ -230,45 +227,40 @@ class Calendar(Container): view="list" ) - if resp.status_code == 200: - self.refresh_cell_days() - tasks = general_utils.bytes2dict(resp.content)['tasks'] - self.tasks = tasks - first_weekday, last_day = calendar.monthrange(self.year, self.month) - - if self.cur_focused_cell: - if show_arrow: - calendar_utils.remove_left_arrow(self.cur_focused_cell) - self.cur_focused_cell_cord = calendar_utils.convert_cell_num_to_coord(first_weekday) - self.cur_focused_cell = self.query_one(f"#cell{first_weekday}") # update - if show_arrow: - calendar_utils.add_left_arrow(self.cur_focused_cell) - - for idx, task in enumerate(tasks): - full_date = task['deadline'] - day = datetime.strptime(full_date, "%Y-%m-%dT%H:%M:%S").day - cell_num = calendar_utils.convert_day_to_cell_num(self.year, self.month, day) - cell = self.query_one(f"#cell{cell_num}") - color = task['color'] - name = task['name'] - # if len(name) > 13: - # name = name[:13] + ".." - task_item_name = Text() - task_item_name.append("●", style=constants.CIRCLE_COLOR[color]) - task_item_name.append(" " + name) - - task_item = Static(task_item_name, id=f"task-cell{cell_num}-{idx}", classes="task-item") - cell.mount(task_item) - - task_item = self.query_one(f"#task-cell{cell_num}-{idx}") - task_item.styles.overflow_x = "hidden" - task_item.styles.overflow_y = "hidden" - - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - exit(0) - else: - exit(0) + self.refresh_cell_days() + self.tasks = tasks + first_weekday, last_day = calendar.monthrange(self.year, self.month) + + if self.cur_focused_cell: + if show_arrow: + calendar_utils.remove_left_arrow(self.cur_focused_cell) + self.cur_focused_cell_cord = calendar_utils.convert_cell_num_to_coord(first_weekday) + self.cur_focused_cell = self.query_one(f"#cell{first_weekday}") # update + if show_arrow: + calendar_utils.add_left_arrow(self.cur_focused_cell) + + for idx, task in enumerate(tasks): + full_date = str(task['deadline']) + day = calendar_utils.get_date_obj_from_str_separated_by_T(full_date).day + # day = datetime.strptime(full_date, "%Y-%m-%dT%H:%M:%S").day + cell_num = calendar_utils.convert_day_to_cell_num(self.year, self.month, day) + cell = self.query_one(f"#cell{cell_num}") + color = task['color'] + name = task['name'] + # if len(name) > 13: + # name = name[:13] + ".." + task_item_name = Text() + task_item_name.append("●", style=constants.CIRCLE_COLOR[color]) + task_item_name.append(" " + name) + + task_item = Static(task_item_name, id=f"task-cell{cell_num}-{idx}", classes="task-item") + cell.mount(task_item) + + task_item = self.query_one(f"#task-cell{cell_num}-{idx}") + task_item.styles.overflow_x = "hidden" + task_item.styles.overflow_y = "hidden" + + class CalendarContainer(Vertical): diff --git a/girok/calendar_cli/sidebar.py b/girok/calendar_cli/sidebar.py index 111448e..19a9772 100644 --- a/girok/calendar_cli/sidebar.py +++ b/girok/calendar_cli/sidebar.py @@ -112,14 +112,8 @@ class TagTree(Tree): self.highlighted_node = self.root self.selected_node = self.root - resp = task_api.get_tags() - if resp.status_code == 200: - self.tags = general_utils.bytes2dict(resp.content)['tags'] - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - exit(0) - else: - exit(0) + tags = task_api.get_tags() + self.tags = tags self.root.expand() diff --git a/girok/commands/task.py b/girok/commands/task.py index 2e125d4..c96aef3 100644 --- a/girok/commands/task.py +++ b/girok/commands/task.py @@ -449,7 +449,7 @@ def show_task( priority = None if category_view: - resp = task_api.get_tasks( + tasks = task_api.get_tasks( cats=cats, start_date=start_date, end_date=end_date, @@ -459,7 +459,7 @@ def show_task( view="category" ) elif list_view: - resp = task_api.get_tasks( + tasks = task_api.get_tasks( cats=cats, start_date=start_date, end_date=end_date, @@ -469,24 +469,16 @@ def show_task( view="list" ) - if resp.status_code == 200: - tasks = general_utils.bytes2dict(resp.content)['tasks'] - print(tasks) - if category_view: - color_dict = category_api.get_color_dict() - task_tree = display_utils.display_tasks_by_category(tasks, color_dict=color_dict) - current_date = task_utils.build_date_info(datetime.now()) - display_utils.center_print(current_date, type='title') - print(task_tree) - elif list_view: - current_date = task_utils.build_date_info(datetime.now()) - display_utils.center_print(current_date, type='title') - display_utils.display_tasks_by_list(tasks) - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, type="error") - else: - display_utils.center_print("Error occurred.", type="title") + if category_view: + color_dict = category_api.get_color_dict() + task_tree = display_utils.display_tasks_by_category(tasks, color_dict=color_dict) + current_date = task_utils.build_date_info(datetime.now()) + display_utils.center_print(current_date, type='title') + print(task_tree) + elif list_view: + current_date = task_utils.build_date_info(datetime.now()) + display_utils.center_print(current_date, type='title') + display_utils.display_tasks_by_list(tasks) @app.command("done", help="[red]Delete[/red] a task", rich_help_panel=":fire: [bold yellow1]Task Commands[/bold yellow1]") @@ -503,25 +495,17 @@ def remove_task( target_task = task_api.get_single_task(target_task_id) target_task_name = target_task['name'] - tasks_resp = task_api.get_tasks() - tasks = general_utils.bytes2dict(tasks_resp.content)['tasks'] + tasks = task_api.get_tasks() if not force: done_confirm = typer.confirm(f"Are you sure to delete task [{target_task_name}]?") if not done_confirm: exit(0) - resp = task_api.remove_task(target_task_id) - - if resp.status_code == 204: - color_dict = category_api.get_color_dict() - task_tree = display_utils.display_tasks_by_category(tasks, color_dict=color_dict, marked_task_id=target_task_id) - current_date = task_utils.build_date_info(datetime.now()) - display_utils.center_print(current_date, type="title") - print(task_tree) - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, type="error") - else: - display_utils.center_print(resp.content, type="error") + task_api.remove_task(target_task_id) + color_dict = category_api.get_color_dict() + task_tree = display_utils.display_tasks_by_category(tasks, color_dict=color_dict, marked_task_id=target_task_id) + current_date = task_utils.build_date_info(datetime.now()) + display_utils.center_print(current_date, type="title") + print(task_tree) @app.command("chtag", help="[green]Change[/green] the tag of a task", rich_help_panel=":fire: [bold yellow1]Task Commands[/bold yellow1]") @@ -624,13 +608,6 @@ def change_date( @app.command("showtag", help="[yellow]Show[/yellow] all tags", rich_help_panel=":fire: [bold yellow1]Task Commands[/bold yellow1]") def show_tag(): - resp = task_api.get_tags() - if resp.status_code == 200: - tags = general_utils.bytes2dict(resp.content)['tags'] - for tag in tags: - print(tag) - elif resp.status_code == 400: - err_msg = general_utils.bytes2dict(resp.content)['detail'] - display_utils.center_print(err_msg, type="error") - else: - print(resp) + tags = task_api.get_tags() + for tag in tags: + print(tag)
\ No newline at end of file diff --git a/girok/girok.py b/girok/girok.py index a5e3ffa..876ecee 100644 --- a/girok/girok.py +++ b/girok/girok.py @@ -23,7 +23,7 @@ app.registered_commands.extend(auth_command.app.registered_commands) app.registered_commands.extend(category_command.app.registered_commands) app.registered_commands.extend(task_command.app.registered_commands) app.registered_commands.extend(calendar_command.app.registered_commands) -# app.registered_commands.extend(info_command.app.registered_commands) + cfg = get_config() def version_callback(value: bool): @@ -31,6 +31,8 @@ def version_callback(value: bool): print(f"[yellow]{cfg.version}[/yellow]") raise typer.Exit() + +####### Major refactoring needed -> minimize code duplications for local-storage feature @app.callback() def pre_command_callback(ctx: typer.Context, version: bool = typer.Option(None, "--version", callback=version_callback, is_eager=True),): cmd = ctx.invoked_subcommand @@ -43,14 +45,14 @@ def pre_command_callback(ctx: typer.Context, version: bool = typer.Option(None, # Check if logged-out mode = auth_utils.get_mode(cfg.config_path) if mode != "off": - print(f"You're already logged in as [green]{mode}[/green]. Please log out and try again.") + print(f"You're already logged in as [yellow]{mode}[/yellow]. Please log out and try again.") exit(0) return elif cmd == "login": # Check if logged-out mode = auth_utils.get_mode(cfg.config_path) if mode != "off": - print(f"You're already logged in as [green]{mode}[/green]. Please log out and try again.") + print(f"You're already logged in as [yellow]{mode}[/yellow]. Please log out and try again.") exit(0) return elif cmd == "logout": @@ -63,7 +65,7 @@ def pre_command_callback(ctx: typer.Context, version: bool = typer.Option(None, elif cmd == "register": mode = auth_utils.get_mode(cfg.config_path) if mode != "off": - print(f"You're already logged in as [green]{mode}[/green]. Please log out and try again.") + print(f"You're already logged in as [yellow]{mode}[/yellow]. Please log out and try again.") exit(0) return elif cmd == "mode": @@ -72,28 +74,14 @@ def pre_command_callback(ctx: typer.Context, version: bool = typer.Option(None, # Check if logged-in mode = auth_utils.get_mode(cfg.config_path) if mode == "off": - print("You're not logged in. Please login with [green]girok login[/green] or [green]girok guest[/green].") + print("You're not logged in. Please login with [yellow]girok login[/yellow] or [yellow]girok guest[/yellow].") exit(0) elif mode == "guest": pass elif mode == "user": if not auth_utils.is_logged_in_as_user(cfg.config_path): - print("You're not logged in. Please login with [green]girok login[/green or [green]girok guest[/green].") + print("You're not logged in. Please login with [yellow]girok login[/yellow] or [yellow]girok guest[/yellow].") exit(0) else: raise Exception("Unexpected Authentication") - - - # if ctx.invoked_subcommand in ['login', 'logout', 'register']: - # return - - # # Check if JWT is stored in config file - # stored_access_token = auth_utils.get_access_token_from_json(cfg.config_path) - # if stored_access_token: - # resp = auth_api.validate_access_token(stored_access_token) - # if resp.status_code != 200: # invalid(or expired) JWT -> login again - # print("You're not logged in. Please login with [green]girok login[/green].") - # exit(0) - # else: - # print("You're not logged in. Please login with [green]girok login[/green].") - # exit(0)
\ No newline at end of file +
\ No newline at end of file diff --git a/girok/server/src/category/router.py b/girok/server/src/category/router.py index 8e756d8..8ce5285 100644 --- a/girok/server/src/category/router.py +++ b/girok/server/src/category/router.py @@ -139,19 +139,27 @@ def move_category(category: schemas.CategoryMoveIn): def get_last_cat_id(cat_data: schemas.LastCategoryIdIn): db = next(get_db()) - cat_data = cat_data.dict() - cat_id, _ = service.get_last_cat_id(db, cat_data['cats']) - return {"cat_id": cat_id} + try: + cat_id, _ = service.get_last_cat_id(db, cat_data['cats']) + return {"success": True, "cat_id": cat_id} + except Exception as e: + return {"success": False, "detail": e.detail} def get_category_color(cat_id: int): db = next(get_db()) - color = service.get_category_color_by_id(db, cat_id) - return {"color": color} + try: + color = service.get_category_color_by_id(db, cat_id) + return {"success": True, "color": color} + except Exception as e: + return {"success": False, "detail": e.detail} def get_category_colors_dict(): db = next(get_db()) - colors = service.get_all_category_colors(db) - colors['No Category'] = "grey" - return colors
\ No newline at end of file + try: + colors = service.get_all_category_colors(db) + colors['No Category'] = "grey" + return {"success": True, "colors": colors} + except Exception as e: + return {"success": False, "detail": e.detail}
\ No newline at end of file diff --git a/girok/server/src/category/service.py b/girok/server/src/category/service.py index c467b51..e6f78f5 100644 --- a/girok/server/src/category/service.py +++ b/girok/server/src/category/service.py @@ -119,7 +119,7 @@ def get_last_cat_id(db: Session, cats: list): cumul_path = "" if not cats: return parent_id, cumul_path - + for cat_name in cats: cat_id = get_category_id_by_name_and_parent_id(db, cat_name, parent_id) if not cat_id: diff --git a/girok/server/src/task/router.py b/girok/server/src/task/router.py index 3f2bc27..9bb3979 100644 --- a/girok/server/src/task/router.py +++ b/girok/server/src/task/router.py @@ -30,8 +30,8 @@ def create_task(task: schemas.TaskCreateIn): # status_code=status.HTTP_200_OK # # response_model=schemas.TaskOut # ) -def get_tasks(data: schemas.TaskGetIn): - print("data", data) +def get_tasks(data): + data = schemas.TaskGetIn(**data).dict() category = data['category'] start_date = data['start_date'] end_date = data['end_date'] @@ -89,7 +89,7 @@ def get_tasks(data: schemas.TaskGetIn): # "/{task_id}", # status_code=status.HTTP_204_NO_CONTENT # ) -async def delete_task( +def delete_task( task_id: int ): db = next(get_db()) @@ -104,7 +104,7 @@ async def delete_task( # '/{task_id}/tag', # status_code=status.HTTP_200_OK, # ) -async def change_task_tag( +def change_task_tag( task_id: int, tag: schemas.ChangeTaskTagIn ): @@ -121,7 +121,7 @@ async def change_task_tag( # '/{task_id}/priority', # status_code=status.HTTP_200_OK, # ) -async def change_task_priority( +def change_task_priority( task_id: int, priority: schemas.ChangeTaskPriorityIn ): @@ -138,16 +138,18 @@ async def change_task_priority( # '/{task_id}/date', # status_code=status.HTTP_200_OK # ) -async def change_task_date( +def change_task_date( task_id: int, data: schemas.ChangeTaskDateIn ): db = next(get_db()) try: new_date = data['new_date'] + new_date = datetime.strptime(new_date, "%Y-%m-%d %H:%M:%S") updated_task = service.change_task_date(db, task_id, new_date) return {"success": True, "updated_task": updated_task} except Exception as e: + print(e) return {"success": False, "detail": e.detail} @@ -155,7 +157,7 @@ async def change_task_date( # '/{task_id}/name', # status_code=status.HTTP_200_OK # ) -async def change_ta |