summaryrefslogtreecommitdiffstats
path: root/girok/api/task.py
blob: 04e972bb3f796421eafafef5d7aae1fa463ca09f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from typing import Union
from textual import log
from datetime import datetime
import requests

from girok.config import get_config
import girok.utils.auth as auth_utils
import girok.utils.display as display_utils
import girok.utils.general as general_utils
import girok.utils.task as task_utils
import girok.constants as constants

cfg = get_config()

def create_task(task_data: dict):
    print(task_data)
    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)
        

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)
 


def get_tasks(
    cats: Union[list, None] = None,
    start_date: Union[str, None] = None,
    end_date: Union[str, None] = None,
    tag: Union[str, None] = None,
    priority: Union[int, None] = None,
    no_priority: bool = False,
    view: str = "category"
):
    query_str_obj = {
        "category": cats,
        "start_date": start_date,
        "end_date": end_date,
        "tag": tag,
        "priority": priority,
        "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


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
    

def get_tags():
    resp = requests.get(
        cfg.base_url + "/tasks/tags",
        headers=auth_utils.build_jwt_header(cfg.config_path)
    )
    
    return resp
    
    
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 == 204:
        display_utils.center_print(f"Successfully renamed [ID: {task_id}] tag to {new_tag_name}.", "black on green")
    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)
        
        
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 == 204:
        display_utils.center_print(f"Successfully change [ID: {task_id}] priority to {new_priority}.", "black on green")
    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)
        

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 == 204:
        display_utils.center_print(f"Successfully change [ID: {task_id}] date to {new_date}.", "black on green")
    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)