manage tags
This commit is contained in:
parent
8c68489390
commit
9e18882c5e
3 changed files with 146 additions and 10 deletions
40
model.py
40
model.py
|
@ -20,9 +20,7 @@ class Model:
|
|||
return self.conn.execute(query, tag_name)
|
||||
|
||||
def get_all_tasks(self):
|
||||
query = '''
|
||||
SELECT t.* FROM task t
|
||||
'''
|
||||
query = 'SELECT t.* FROM task t'
|
||||
|
||||
return self.conn.execute(query)
|
||||
|
||||
|
@ -59,17 +57,47 @@ class Model:
|
|||
def save_content(self, aid, content):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('UPDATE task SET content=? WHERE aid=?', (content, aid))
|
||||
|
||||
self.conn.commit()
|
||||
|
||||
def toggle_long_term(self, aid):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('UPDATE task SET long_term=NOT long_term WHERE aid=?', (aid))
|
||||
|
||||
self.conn.commit()
|
||||
|
||||
def toggle_done(self, aid):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute('UPDATE task SET done=NOT done, finished_at=DATETIME(\'now\') WHERE aid=?', (aid))
|
||||
|
||||
self.conn.commit()
|
||||
|
||||
def create_tag(self, name):
|
||||
query = 'INSERT INTO tag (name) VALUES (?)'
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute(query, (name,))
|
||||
self.conn.commit()
|
||||
|
||||
return cursor.lastrowid
|
||||
|
||||
def get_all_tags(self):
|
||||
query = 'SELECT t.* FROM tag t'
|
||||
|
||||
return self.conn.execute(query)
|
||||
|
||||
def get_tag(self, tid):
|
||||
query = 'SELECT t.* FROM tag t WHERE t.id = ?'
|
||||
cursor = self.conn.cursor()
|
||||
results = cursor.execute(query, (tid,))
|
||||
|
||||
return results.fetchone()
|
||||
|
||||
def get_tag_by_name(self, name):
|
||||
query = 'SELECT t.* FROM tag t WHERE t.name = ?'
|
||||
cursor = self.conn.cursor()
|
||||
results = cursor.execute(query, (name,))
|
||||
|
||||
return results.fetchone()
|
||||
|
||||
def remove_tag_by_name(self, name):
|
||||
query = 'DELETE FROM tag WHERE name = ?'
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute(query, (name,))
|
||||
self.conn.commit()
|
||||
|
|
9
task.py
9
task.py
|
@ -21,7 +21,7 @@ class Task:
|
|||
self.add()
|
||||
tasksData = self.model.get_all_tasks()
|
||||
|
||||
for t in self.model.get_all_tasks():
|
||||
for t in tasksData:
|
||||
tasks.append('%s: %s' % (t['aid'], t['description']))
|
||||
|
||||
selected = self.fzf.prompt(tasks)
|
||||
|
@ -37,11 +37,12 @@ class Task:
|
|||
print('\n' + bcolors.HEADER + 'Adding task\n' + bcolors.ENDC)
|
||||
description = input('Description: ')
|
||||
aid = self.model.create_task_draft(description)
|
||||
print(bcolors.OKBLUE + '\n[task has been created]' + bcolors.ENDC)
|
||||
self.manage_task(aid)
|
||||
|
||||
def manage_task(self, aid):
|
||||
task = self.model.get_task(aid)
|
||||
print('\n' + bcolors.HEADER + 'Managing: [' + task['aid'] + '] ' + task['description'] + bcolors.ENDC)
|
||||
print('\n' + bcolors.HEADER + 'Managing task: [' + task['aid'] + '] ' + task['description'] + bcolors.ENDC)
|
||||
|
||||
long_term = ' '
|
||||
if task['long_term'] and task['long_term'] != 'FALSE':
|
||||
|
@ -61,7 +62,7 @@ Created: %s
|
|||
self.manage_task_menu(aid)
|
||||
|
||||
def manage_task_menu(self, aid):
|
||||
menu = input(bcolors.OKGREEN + 'What you want to do? (?e*+-v&><q) ' + bcolors.ENDC)
|
||||
menu = input(bcolors.OKBLUE + '~task: ' + bcolors.OKGREEN + 'What you want to do? (?e*+-v&><q) ' + bcolors.ENDC)
|
||||
|
||||
if menu == 'q':
|
||||
self.bye()
|
||||
|
@ -150,10 +151,12 @@ Created: %s ''' % (task['aid'], '', long_term, task['created_at'])
|
|||
|
||||
def toggle_long_term(self, aid):
|
||||
self.model.toggle_long_term(aid)
|
||||
print(bcolors.OKBLUE + '\n[task has been updated]' + bcolors.ENDC)
|
||||
self.manage_task(aid)
|
||||
|
||||
def toggle_done(self, aid):
|
||||
self.model.toggle_done(aid)
|
||||
print(bcolors.OKBLUE + '\n[task has been updated]' + bcolors.ENDC)
|
||||
self.manage_task(aid)
|
||||
|
||||
def bye(self):
|
||||
|
|
107
ztm.py
107
ztm.py
|
@ -33,7 +33,7 @@ Task manager from Zordsdavini (2018)
|
|||
self.menu()
|
||||
|
||||
def menu(self):
|
||||
menu = input(bcolors.OKGREEN + 'What you want to do? (?+/tcq) ' + bcolors.ENDC)
|
||||
menu = input(bcolors.OKBLUE + '~: ' + bcolors.OKGREEN + 'What you want to do? (?+/tcq) ' + bcolors.ENDC)
|
||||
|
||||
if menu == '?':
|
||||
self.about()
|
||||
|
@ -46,6 +46,10 @@ Task manager from Zordsdavini (2018)
|
|||
self.task.search()
|
||||
self.menu()
|
||||
|
||||
elif menu == 't':
|
||||
self.manage_tag()
|
||||
self.menu()
|
||||
|
||||
elif menu == 'q':
|
||||
self.bye()
|
||||
|
||||
|
@ -70,6 +74,107 @@ q - exit
|
|||
print(bcolors.FAIL + 'bye o/' + bcolors.ENDC)
|
||||
sys.exit(0)
|
||||
|
||||
def manage_tag(self, tid=None):
|
||||
if tid:
|
||||
tag = self.model.get_tag(tid)
|
||||
print(bcolors.HEADER + '\nManaging tag: ' + tag['name'] + '\n' + bcolors.ENDC)
|
||||
else:
|
||||
print(bcolors.HEADER + '\nManaging tags\n' + bcolors.ENDC)
|
||||
|
||||
tagsData = self.model.get_all_tags()
|
||||
if not tagsData:
|
||||
self.add_tag()
|
||||
tagsData = self.model.get_all_tags()
|
||||
|
||||
tags = bcolors.WARNING
|
||||
for t in tagsData:
|
||||
tags += t['name'] + ' '
|
||||
|
||||
print(tags + '\n' + bcolors.ENDC)
|
||||
|
||||
self.manage_tag_menu(tid)
|
||||
|
||||
def manage_tag_menu(self, tid=None):
|
||||
menu = input(bcolors.OKBLUE + '~tag: ' + bcolors.OKGREEN + 'What you want to do? (?/+-<q) ' + bcolors.ENDC)
|
||||
|
||||
if menu == 'q':
|
||||
self.bye()
|
||||
|
||||
elif menu == '/':
|
||||
self.search_tag(tid)
|
||||
self.manage_tag_menu(tid)
|
||||
|
||||
elif menu == '?':
|
||||
self.manage_tag_about(tid)
|
||||
|
||||
elif menu == '+':
|
||||
self.add_tag()
|
||||
|
||||
elif menu == '-':
|
||||
self.remove_tag(tid)
|
||||
|
||||
elif menu == '<':
|
||||
return
|
||||
|
||||
else:
|
||||
print(bcolors.FAIL + 'This is not implemented...\n' + bcolors.ENDC)
|
||||
self.manage_tag_menu(tid)
|
||||
|
||||
def manage_tag_about(self, tid=None):
|
||||
print(bcolors.WARNING + '''
|
||||
Short instruction
|
||||
-----------------
|
||||
? - help (this dialog)
|
||||
/ - search tag
|
||||
+ - add tag
|
||||
- - remove tag
|
||||
< - back
|
||||
q - exit
|
||||
''' + bcolors.ENDC)
|
||||
self.manage_tag(tid)
|
||||
|
||||
def add_tag(self):
|
||||
print('\n' + bcolors.HEADER + 'Adding tag\n' + bcolors.ENDC)
|
||||
name = input('Name: ')
|
||||
tid = self.model.create_tag(name)
|
||||
print(bcolors.OKBLUE + '\n[tag has been created]' + bcolors.ENDC)
|
||||
self.manage_tag(tid)
|
||||
|
||||
def search_tag(self, tid=None):
|
||||
tagsData = self.model.get_all_tags()
|
||||
if not tagsData:
|
||||
self.add_tag()
|
||||
tagsData = self.model.get_all_tags()
|
||||
|
||||
tags = []
|
||||
for t in tagsData:
|
||||
tags.append(t['name'])
|
||||
|
||||
selected = self.fzf.prompt(tags)
|
||||
if selected:
|
||||
tag = self.model.get_tag_by_name(selected[0])
|
||||
self.manage_tag(tag['id'])
|
||||
else:
|
||||
print(bcolors.FAIL + 'Tag was not selected...\n' + bcolors.ENDC)
|
||||
|
||||
def remove_tag(self, tid=None):
|
||||
tagsData = self.model.get_all_tags()
|
||||
if not tagsData:
|
||||
print(bcolors.FAIL + 'Where is no tags...\n' + bcolors.ENDC)
|
||||
self.manage_tag_menu()
|
||||
|
||||
tags = []
|
||||
for t in tagsData:
|
||||
tags.append(t['name'])
|
||||
|
||||
selected = self.fzf.prompt(tags)
|
||||
if selected:
|
||||
self.model.remove_tag_by_name(selected[0])
|
||||
print(bcolors.OKBLUE + '\n[tag has been deleted]' + bcolors.ENDC)
|
||||
self.manage_tag()
|
||||
else:
|
||||
print(bcolors.FAIL + 'Tag was not selected...\n' + bcolors.ENDC)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
a = Main()
|
||||
|
|
Reference in a new issue