diff options
author | Changjin Lee <changjin9792@gmail.com> | 2023-04-03 19:20:25 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-03 19:20:25 +0800 |
commit | f68c48520eb3c9c5c1c9b9b5a7772f2b77680946 (patch) | |
tree | 898ffe5ff4802308f459bdb27700db6c6d63c33f | |
parent | c9a526292e9cc190fa02511034b15bca25f785ce (diff) | |
parent | 07e2ef901e5faf1b9e76d68772fa1488b2f51e5c (diff) |
Merge pull request #38 from noisrucer/feat/guest-mode
Feat/guest mode
36 files changed, 1827 insertions, 372 deletions
diff --git a/girok/api/category.py b/girok/api/category.py index f01d274..36dffbf 100644 --- a/girok/api/category.py +++ b/girok/api/category.py @@ -7,60 +7,127 @@ import girok.utils.general as general_utils import girok.utils.display as display_utils import girok.constants as constants +# Guest mode imports +import girok.server.src.category.router as category_router + console = Console() cfg = get_config() def get_categories(): - resp = requests.get( - cfg.base_url + "/categories", - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - if resp.status_code == 200: - return general_utils.bytes2dict(resp.content) - + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.get( + cfg.base_url + "/categories", + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 200: + return general_utils.bytes2dict(resp.content) + elif mode == "guest": + resp = category_router.get_all_categories() + return resp def add_category(cat_str: str, color=None): + mode = auth_utils.get_mode(cfg.config_path) cats = cat_str.split('/') - resp = requests.post( - cfg.base_url + "/categories", - json={ - "names": cats, - "color": color - }, - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - return resp + if mode == "user": + resp = requests.post( + cfg.base_url + "/categories", + json={ + "names": cats, + "color": color + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 201: + display_utils.center_print("Category added successfully!", type="success") + cats_dict = get_categories() + display_utils.display_categories(cats_dict, highlight_cat=cat_str) + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, type="error") + else: + print(resp) + return resp + elif mode == "guest": + resp = category_router.create_category({"names": cats, "color": color}) + if resp['success']: + display_utils.center_print("Category added successfully!", type="success") + cats_dict = get_categories() + display_utils.display_categories(cats_dict, highlight_cat=cat_str) + else: + display_utils.center_print(resp['detail'], type="error") def remove_category(cat_str: str): + mode = auth_utils.get_mode(cfg.config_path) cats = cat_str.split('/') - resp = requests.delete( - cfg.base_url + "/categories", - json={ - "cats": cats - }, - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - - return resp - - + if mode == "user": + resp = requests.delete( + cfg.base_url + "/categories", + json={ + "cats": cats + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 204: + display_utils.center_print(f"Deleted {cat_str} successfully.", type="success") + cats_dict = get_categories() + display_utils.display_categories(cats_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(resp.content, type="error") + elif mode == "guest": + resp = category_router.delete_category({"cats": cats}) + if resp['success']: + display_utils.center_print(f"Deleted {cat_str} successfully.", type="success") + cats_dict = get_categories() + display_utils.display_categories(cats_dict) + else: + display_utils.center_print(resp['detail'], type="error") + + def rename_category(cat_str: str, new_name: str): + mode = auth_utils.get_mode(cfg.config_path) cats = cat_str.split('/') - resp = requests.patch( - cfg.base_url + "/categories/name", - json={ + + if mode == "user": + resp = requests.patch( + cfg.base_url + "/categories/name", + json={ + "cats": cats, + "new_name": new_name + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 204: + new_cat = '/'.join(cat_str.split('/')[:-1] + [new_name]) + display_utils.center_print(f"Successfully renamed {cat_str} to {new_cat}.", type="success") + cats_dict = get_categories() + display_utils.display_categories(cats_dict, highlight_cat=new_cat) + 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 = category_router.rename_category({ "cats": cats, "new_name": new_name - }, - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - - return resp + }) + if resp['success']: + new_cat = '/'.join(cat_str.split('/')[:-1] + [new_name]) + display_utils.center_print(f"Successfully renamed {cat_str} to {new_cat}.", type="success") + cats_dict = get_categories() + display_utils.display_categories(cats_dict, highlight_cat=new_cat) + else: + display_utils.center_print(resp['detail'], type="error") def move_category(cat_str: str, new_parent_cat_str: str): + mode = auth_utils.get_mode(cfg.config_path) if cat_str.endswith('/'): cat_str = cat_str[:-1] if new_parent_cat_str.endswith('/'): @@ -68,64 +135,115 @@ def move_category(cat_str: str, new_parent_cat_str: str): cats = cat_str.split('/') if cat_str else [] new_parent_cats = new_parent_cat_str.split('/') if new_parent_cat_str else [] - resp = requests.patch( - cfg.base_url + "/categories/parent", - json={ + + if mode == "user": + resp = requests.patch( + cfg.base_url + "/categories/parent", + json={ + "cats": cats, + "new_parent_cats": new_parent_cats + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 200: + new_cat = '/'.join(new_parent_cat_str.split('/') + [cat_str.split('/')[-1]]) + display_utils.center_print(f"Successfully moved {cat_str} to {new_parent_cat_str}/.", type="success") + cats_dict = get_categories() + display_utils.display_categories(cats_dict, highlight_cat=new_cat) + 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") + return resp + elif mode == "guest": + resp = category_router.move_category({ "cats": cats, "new_parent_cats": new_parent_cats - }, - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - return resp + }) + if resp['success']: + new_cat = '/'.join(new_parent_cat_str.split('/') + [cat_str.split('/')[-1]]) + display_utils.center_print(f"Successfully moved {cat_str} to {new_parent_cat_str}/.", type="success") + cats_dict = get_categories() + display_utils.display_categories(cats_dict, highlight_cat=new_cat) + else: + display_utils.center_print(resp['detail'], type="error") 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 5a3c911..32d9180 100644 --- a/girok/api/task.py +++ b/girok/api/task.py @@ -10,39 +10,64 @@ import girok.utils.general as general_utils import girok.utils.task as task_utils import girok.constants as constants +import girok.server.src.task.router as task_router + cfg = get_config() def create_task(task_data: dict): - resp = requests.post( - cfg.base_url + "/tasks", - json=task_data, - headers=auth_utils.build_jwt_header(cfg.config_path) - ) - if resp.status_code == 201: - task = general_utils.bytes2dict(resp.content) - task_id = task['task_id'] - return task_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) - else: - display_utils.center_print("Error occurred.", constants.DISPLAY_TERMINAL_COLOR_ERROR) - + mode = auth_utils.get_mode(cfg.config_path) + if mode == "user": + resp = requests.post( + cfg.base_url + "/tasks", + json=task_data, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 201: + task = general_utils.bytes2dict(resp.content) + task_id = task['task_id'] + return task_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("Error occurred.", constants.DISPLAY_TERMINAL_COLOR_ERROR) + exit(0) + elif mode == "guest": + resp = task_router.create_task(task_data) + if resp['success']: + task = resp['new_task'] + task_id = task['task_id'] + return task_id + else: + display_utils.center_print(resp['detail'], type="error") + exit(0) + exit(0) + def get_single_task(task_id: int): - resp = requests.get( - cfg.base_url + f"/tasks/{task_id}", - headers=auth_utils.build_jwt_header(cfg.config_path), - ) - if resp.status_code == 200: - task = general_utils.bytes2dict(resp.content) - return task - 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.get( + cfg.base_url + f"/tasks/{task_id}", + headers=auth_utils.build_jwt_header(cfg.config_path), + ) + if resp.status_code == 200: + task = general_utils.bytes2dict(resp.content) + return task + 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.get_single_task(task_id) + if resp['success']: + task = resp['task'] + return task + else: + display_utils.center_print(resp['detail'], type="error") + exit(0) def get_tasks( @@ -54,6 +79,7 @@ def get_tasks( no_priority: bool = False, view: str = "category" ): + mode = auth_utils.get_mode(cfg.config_path) query_str_obj = { "category": cats, "start_date": start_date, @@ -63,109 +89,203 @@ def get_tasks( "no_priority": no_priority, "view": view } - resp = requests.get( - cfg.base_url + "/tasks", - headers=auth_utils.build_jwt_header(cfg.config_path), - params=query_str_obj - ) - return resp + if mode == "user": + resp = requests.get( + cfg.base_url + "/tasks", + headers=auth_utils.build_jwt_header(cfg.config_path), + params=query_str_obj + ) + 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']: + return resp['tasks'] + else: + display_utils.center_print(resp['detail'], type="error") + + exit(0) 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") |