Skip to content

Commit f1c636e

Browse files
author
Sahil
committed
Implement todos functionality
1 parent 743d21f commit f1c636e

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

todo_cli/app.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def main():
4646
else:
4747
print('Successfuly chosen this list...')
4848
current_list = file_name
49-
elif (command[0] == 'todo'):
49+
elif (command.split()[0] == 'todo'):
50+
# todo type of command
5051
command_args.insert(0, current_list)
5152
commands_dict[command_name](command_args)
5253
else:

todo_cli/commands/todos.py

+97-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,112 @@
1+
import json
2+
from datetime import datetime
3+
14
def set_list(list_name):
25
if (list_name == ''):
36
print('Please select a given list before this action')
4-
return
7+
return list_name
8+
9+
def get_data(list_file_name):
10+
"""
11+
Get the deserialized data from the todo list json file
12+
"""
13+
with open(f'lists/{list_file_name}', 'r') as json_file:
14+
data = json.load(json_file)
15+
return data
16+
17+
def update_data(list_file_name, new_data):
18+
"""
19+
Update the content of the todo list json file
20+
with the serialized version of 'new_data'
21+
"""
22+
with open(f'lists/{list_file_name}', 'w') as json_file:
23+
json.dump(new_data, json_file, sort_keys=True, indent=True)
524

625
def add_item(args):
7-
set_list(args[0])
26+
"""
27+
Adds a todo item to the todo list
28+
"""
29+
list_name = set_list(args[0])
30+
if (not list_name):
31+
return
32+
title = args[1]
33+
data = get_data(list_name)
34+
new_todo = {
35+
'title': title,
36+
'created_at': datetime.now().strftime("%d/%m/%Y %H:%M:%S"),
37+
'completed': False
38+
}
39+
data.append(new_todo)
40+
update_data(list_name, data)
841

942
def show_items(args):
10-
set_list(args[0])
43+
"""
44+
Prints all the todo items in the currently chosen todo list
45+
"""
46+
list_name = set_list(args[0])
47+
if (not list_name):
48+
return
49+
data = get_data(list_name)
50+
complete = 0
51+
if (len(data) == 0):
52+
print('No todos in the list, why dont you add one?')
53+
else:
54+
for index, todo_item in enumerate(data):
55+
print(index + 1, todo_item['title'])
56+
if (todo_item['completed']):
57+
complete += 1
58+
print(f'{complete}/{len(data)} completed!')
1159

1260
def edit_item(args):
13-
set_list(args[0])
61+
"""
62+
Edit a particular todo item
63+
"""
64+
list_name = set_list(args[0])
65+
if (not list_name):
66+
return
67+
item_id = int(args[1])
68+
new_title = args[2]
69+
data = get_data(list_name)
70+
updated_todo = {
71+
'title': new_title,
72+
'created_at': datetime.now().strftime("%d/%m/%Y %H:%M:%S"),
73+
'completed': False
74+
}
75+
data[item_id - 1] = updated_todo
76+
update_data(list_name, data)
1477

1578
def remove_item(args):
16-
set_list(args[0])
79+
"""
80+
Remove a todo item
81+
"""
82+
list_name = set_list(args[0])
83+
if (not list_name):
84+
return
85+
item_id = int(args[1])
86+
data = get_data(list_name)
87+
data.pop(item_id - 1)
88+
update_data(list_name, data)
1789

1890
def complete_item(args):
19-
set_list(args[0])
91+
"""
92+
Mark a todo item as completed
93+
"""
94+
list_name = set_list(args[0])
95+
if (not list_name):
96+
return
97+
item_id = int(args[1])
98+
data = get_data(list_name)
99+
data[item_id - 1]['completed'] = True
100+
update_data(list_name, data)
20101

21102
def incomplete_item(args):
22-
set_list(args[0])
103+
"""
104+
Mark a todo item as incomplete
105+
"""
106+
list_name = set_list(args[0])
107+
if (not list_name):
108+
return
109+
item_id = int(args[1])
110+
data = get_data(list_name)
111+
data[item_id - 1]['completed'] = False
112+
update_data(list_name, data)

0 commit comments

Comments
 (0)