-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory.py
More file actions
215 lines (162 loc) · 4.8 KB
/
category.py
File metadata and controls
215 lines (162 loc) · 4.8 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import time
from support.backend.database import make_con, close_con, query_db, remove_id_from_recs
from support.tools.utils import clear_screen
from support import menu
from tabulate import tabulate
def get_all_cat():
""" Returns all the categories from the db """
con, cur = make_con('Password_manager')
cur.execute('select * from Category;')
rec = cur.fetchall()
return rec
def get_number_of_cats():
""" Returns the number of categories """
return len(get_all_cat())
def add_cat():
"""Adds a category record"""
number = get_number_of_cats() + 1
con, cur = make_con(db='Password_manager')
cat_name = menu.get_input(message='Enter Category name: ')
if not cat_name:
return False
cur.execute(f'insert into Category (Number, Category) values ({number}, "{cat_name}");')
con.commit()
close_con(con)
print()
print('Category has been successfully added')
time.sleep(2)
return True
def rename_cat(cat_no, name):
""""Renames an existing category"""
con, cur = make_con('Password_manager')
cur.execute(f'update Category set Category="{name}" where Number="{cat_no}"')
con.commit()
close_con(con)
def del_cat(cat_to_del):
"""Deletes the selected category from db when category is not there in username table"""
con, cur = make_con(db='Password_manager')
cur.execute(f'delete from category where Number="{cat_to_del}";')
con.commit()
close_con(con)
def to_table(rows=[]):
rows = remove_id_from_recs(rows)
""" Print the rows as table """
if len(rows) > 0:
print()
print(tabulate(rows, ['Item', 'Category']))
else:
print()
print('No categories added yet. Add some!')
print()
def show_cat():
"""Displays all the content of table Category"""
recs = get_all_cat()
to_table(recs)
print()
def exists(id_):
""" Check if id exists """
all_cat = get_all_cat()
there = False
for rec in all_cat:
if rec[1] == id_:
there = True
if there:
return True
return False
def get_cat_key(id_):
""" Returns the primary key associated with the id """
rec = query_db(f"Number = {id_}", table='Category')
return rec[0][0]
def select(message='Select a category: ', optional=False, get_key=False):
""" Asks user to choose a category """
all_cats = get_all_cat()
if len(all_cats) == 0:
print()
print("You did not create a category yet. Please create one.")
return False
show_cat()
id_ = menu.get_input(message)
if id_ is False:
return False
try:
id_int = int(id_)
if id_int and exists(id_int):
if get_key:
return get_cat_key(id_int)
return id_int
except ValueError:
pass
if optional and id_ == '':
return None
print()
print("Invalid Category number!")
time.sleep(2)
return False
def rename():
""" Rename a category """
id_ = select()
if not id_:
return False
name = menu.get_input("New name: ")
if not name:
return False
rename_cat(id_, name)
print()
print("The category has been renamed.")
time.sleep(2)
return True
def update_order(_id):
""" Updates the order number for all records above given id """
con, cur = make_con(db='Password_manager')
cur.execute(f"UPDATE Category SET Number = Number - 1 WHERE Number > {_id}")
con.commit()
close_con(con)
def delete():
""" Delete a category """
id_ = select()
if not id_:
return False
if is_used(id_):
print()
print('Sorry, can\'t delete this category.. records still exist')
time.sleep(2)
return False
confirm = menu.get_input("Confirm deletion (y/n): ")
if confirm is False or confirm != 'y':
print()
print("Operation Cancelled!")
time.sleep(2)
return False
del_cat(id_)
update_order(id_)
print()
print('Category deleted successfully')
time.sleep(2)
return True
def is_used(id_):
""" Check if category is being used """
con, cur = make_con(db='Password_manager')
cur.execute(f'select * from Usernames where Category="{id_}";')
recs = cur.fetchall()
if recs:
return True
return False
def cat():
"""Runs the entire category function"""
while True:
clear_screen()
show_cat()
choice = menu.get_input(message='Choose an option [(a)dd / (r)ename / (d)elete / (b)ack]: ', lower=True)
if choice is False:
print()
if choice == 'a':
add_cat()
return
elif choice == 'r':
rename()
return
elif choice == 'd':
delete()
return
elif choice == 'b':
return