tag add/delete; link/unlink task to tag

This commit is contained in:
Arnas Udovicius 2018-02-16 15:52:58 +02:00
parent 9e18882c5e
commit c6eda66e1b
4 changed files with 226 additions and 126 deletions

View file

@ -43,16 +43,34 @@ class Model:
return aid return aid
def get_task(self, aid): def get_task(self, aid):
query = ''' query = 'SELECT t.* FROM task t WHERE t.aid = ?'
SELECT t.* FROM task t
LEFT JOIN tag_task tgt ON t.id = tgt.task_id
LEFT JOIN tag tg ON tg.id = tgt.tag_id
WHERE t.aid = ?
'''
cursor = self.conn.cursor() cursor = self.conn.cursor()
results = cursor.execute(query, (aid,)) results = cursor.execute(query, (aid,))
task = dict(results.fetchone())
return results.fetchone() tquery = '''
SELECT t.* from tag t
LEFT JOIN tag_task tt ON t.id = tt.tag_id
WHERE tt.task_id = ?
'''
tags = cursor.execute(tquery, (task['id'],))
task['tags'] = []
for tag in tags:
task['tags'].append(tag)
return task
def get_tags_not_in_task(self, task_id):
query = '''
SELECT t.* from tag t
WHERE t.id NOT IN (
SELECT t2.id from tag t2
LEFT JOIN tag_task tt ON t2.id = tt.tag_id
WHERE tt.task_id = ?
)
'''
cursor = self.conn.cursor()
return cursor.execute(query, (task_id,))
def save_content(self, aid, content): def save_content(self, aid, content):
cursor = self.conn.cursor() cursor = self.conn.cursor()
@ -101,3 +119,21 @@ class Model:
cursor = self.conn.cursor() cursor = self.conn.cursor()
cursor.execute(query, (name,)) cursor.execute(query, (name,))
self.conn.commit() self.conn.commit()
def link_tags_to_task(self, task_id, tag_names):
query = 'INSERT INTO tag_task (task_id, tag_id) VALUES (?, ?)'
cursor = self.conn.cursor()
for name in tag_names:
tag = self.get_tag_by_name(name)
cursor.execute(query, (task_id, tag['id']))
self.conn.commit()
def unlink_tags_from_task(self, task_id, tag_names):
query = 'DELETE FROM tag_task WHERE task_id = ? AND tag_id = ?'
cursor = self.conn.cursor()
for name in tag_names:
tag = self.get_tag_by_name(name)
cursor.execute(query, (task_id, tag['id']))
self.conn.commit()

119
tag.py Normal file
View file

@ -0,0 +1,119 @@
import sys
from pyfzf.pyfzf import FzfPrompt
from model import Model
from task import Task
from bcolors import bcolors
class Tag:
def __init__(self):
self.model = Model()
self.fzf = FzfPrompt()
def manage_tag(self, tid=None):
if tid:
tag = self.model.get_tag(tid)
print(bcolors.HEADER + 'Managing tag: ' + tag['name'] + bcolors.ENDC)
else:
print(bcolors.HEADER + 'Managing tags' + 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 + 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...' + 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
& - search linked tasks
< - back
q - exit
''' + bcolors.ENDC)
self.manage_tag(tid)
def add_tag(self):
print(bcolors.HEADER + 'Adding tag' + bcolors.ENDC)
name = input('Name: ')
tid = self.model.create_tag(name)
print(bcolors.OKBLUE + '[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...' + bcolors.ENDC)
def remove_tag(self, tid=None):
tagsData = self.model.get_all_tags()
if not tagsData:
print(bcolors.FAIL + 'Where is no tags...' + 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 + '[tag has been deleted]' + bcolors.ENDC)
self.manage_tag()
else:
print(bcolors.FAIL + 'Tag was not selected...' + bcolors.ENDC)
def bye(self):
print(bcolors.FAIL + 'bye o/' + bcolors.ENDC)
sys.exit(0)

73
task.py
View file

@ -31,33 +31,35 @@ class Task:
if aid: if aid:
self.edit_task(aid) self.edit_task(aid)
else: else:
print(bcolors.FAIL + 'Task was not selected...\n' + bcolors.ENDC) print(bcolors.FAIL + 'Task was not selected...' + bcolors.ENDC)
def add(self): def add(self):
print('\n' + bcolors.HEADER + 'Adding task\n' + bcolors.ENDC) print(bcolors.HEADER + 'Adding task' + bcolors.ENDC)
description = input('Description: ') description = input('Description: ')
aid = self.model.create_task_draft(description) aid = self.model.create_task_draft(description)
print(bcolors.OKBLUE + '\n[task has been created]' + bcolors.ENDC) print(bcolors.OKBLUE + '[task has been created]' + bcolors.ENDC)
self.manage_task(aid) self.manage_task(aid)
def manage_task(self, aid): def manage_task(self, aid):
task = self.model.get_task(aid) task = self.model.get_task(aid)
print('\n' + bcolors.HEADER + 'Managing task: [' + task['aid'] + '] ' + task['description'] + bcolors.ENDC) print(bcolors.HEADER + 'Managing task: [' + task['aid'] + '] ' + task['description'] + bcolors.ENDC)
long_term = ' ' long_term = ' '
if task['long_term'] and task['long_term'] != 'FALSE': if task['long_term'] and task['long_term'] != 'FALSE':
long_term = 'x' long_term = 'x'
print(''' tags = ''
%s if task['tags']:
tags = ' '.join([t['name'] for t in task['tags']])
print('''%s
Description: %s Description: %s
Tags: [%s] Tags: [%s]
Long Term: [%s] Long Term: [%s]
Created: %s Created: %s ''' % (task['aid'], task['description'], tags, long_term, task['created_at']))
''' % (task['aid'], task['description'], '', long_term, task['created_at']))
if task['done'] and task['done'] != 'FALSE': if task['done'] and task['done'] != 'FALSE':
print(bcolors.OKGREEN + 'Finished: ' + task['finished_at'] + '\n' + bcolors.ENDC) print(bcolors.OKGREEN + 'Finished: ' + task['finished_at'] + bcolors.ENDC)
self.manage_task_menu(aid) self.manage_task_menu(aid)
@ -76,6 +78,12 @@ Created: %s
elif menu == '*': elif menu == '*':
self.toggle_long_term(aid) self.toggle_long_term(aid)
elif menu == '+':
self.add_tags(aid)
elif menu == '-':
self.remove_tags(aid)
elif menu == 'v': elif menu == 'v':
self.toggle_done(aid) self.toggle_done(aid)
@ -83,7 +91,7 @@ Created: %s
return return
else: else:
print(bcolors.FAIL + 'This is not implemented...\n' + bcolors.ENDC) print(bcolors.FAIL + 'This is not implemented...' + bcolors.ENDC)
self.manage_task_menu(aid) self.manage_task_menu(aid)
def manage_task_about(self, aid): def manage_task_about(self, aid):
@ -110,10 +118,14 @@ q - exit
if task['long_term'] and task['long_term'] != 'FALSE': if task['long_term'] and task['long_term'] != 'FALSE':
long_term = 'x' long_term = 'x'
tags = ''
if task['tags']:
tags = ' '.join([t['name'] for t in task['tags']])
content = '''%s content = '''%s
Tags: [%s] Tags: [%s]
Long Term: [%s] Long Term: [%s]
Created: %s ''' % (task['aid'], '', long_term, task['created_at']) Created: %s ''' % (task['aid'], tags, long_term, task['created_at'])
if task['done'] and task['done'] != 'FALSE': if task['done'] and task['done'] != 'FALSE':
content += '\nFinished: ' + task['finished_at'] content += '\nFinished: ' + task['finished_at']
@ -146,19 +158,52 @@ Created: %s ''' % (task['aid'], '', long_term, task['created_at'])
found = True found = True
self.model.save_content(aid, content) self.model.save_content(aid, content)
print(bcolors.OKBLUE + '\n[content has been saved]' + bcolors.ENDC) print(bcolors.OKBLUE + '[content has been saved]' + bcolors.ENDC)
self.manage_task(aid) self.manage_task(aid)
def toggle_long_term(self, aid): def toggle_long_term(self, aid):
self.model.toggle_long_term(aid) self.model.toggle_long_term(aid)
print(bcolors.OKBLUE + '\n[task has been updated]' + bcolors.ENDC) print(bcolors.OKBLUE + '[task has been updated]' + bcolors.ENDC)
self.manage_task(aid) self.manage_task(aid)
def toggle_done(self, aid): def toggle_done(self, aid):
self.model.toggle_done(aid) self.model.toggle_done(aid)
print(bcolors.OKBLUE + '\n[task has been updated]' + bcolors.ENDC) print(bcolors.OKBLUE + '[task has been updated]' + bcolors.ENDC)
self.manage_task(aid) self.manage_task(aid)
def add_tags(self, aid):
task = self.model.get_task(aid)
unlinked_tags = self.model.get_tags_not_in_task(task['id'])
if len(unlinked_tags) == 0:
print(bcolors.FAIL + 'Where is no more unlinked tags left...' + bcolors.ENDC)
self.manage_task(aid)
tags = [t['name'] for t in unlinked_tags]
selected = self.fzf.prompt(tags, '--multi --cycle')
if selected:
self.model.link_tags_to_task(task['id'], selected)
print(bcolors.OKBLUE + '[tags have been linked]' + bcolors.ENDC)
self.manage_task(aid)
else:
print(bcolors.FAIL + 'Tag was not selected...' + bcolors.ENDC)
def remove_tags(self, aid):
task = self.model.get_task(aid)
if not task['tags']:
print(bcolors.FAIL + 'Where is no tags linked...' + bcolors.ENDC)
self.manage_task(aid)
tags = [t['name'] for t in task['tags']]
selected = self.fzf.prompt(tags, '--multi --cycle')
if selected:
self.model.unlink_tags_from_task(task['id'], selected)
print(bcolors.OKBLUE + '[tags have been unlinked]' + bcolors.ENDC)
self.manage_task(aid)
else:
print(bcolors.FAIL + 'Tag was not selected...' + bcolors.ENDC)
def bye(self): def bye(self):
print(bcolors.FAIL + 'bye o/' + bcolors.ENDC) print(bcolors.FAIL + 'bye o/' + bcolors.ENDC)
sys.exit(0) sys.exit(0)

110
ztm.py
View file

@ -4,6 +4,7 @@ from pyfzf.pyfzf import FzfPrompt
from model import Model from model import Model
from task import Task from task import Task
from tag import Tag
from bcolors import bcolors from bcolors import bcolors
@ -11,6 +12,7 @@ class Main:
def __init__(self): def __init__(self):
self.model = Model() self.model = Model()
self.task = Task() self.task = Task()
self.tag = Tag()
self.fzf = FzfPrompt() self.fzf = FzfPrompt()
def run(self): def run(self):
@ -27,8 +29,7 @@ class Main:
.fLLLLLLLLLLLLLLLLLi fE ... #G .. .fLLLLLLLLLLLLLLLLLi fE ... #G ..
: j : j
Task manager from Zordsdavini (2018) Task manager from Zordsdavini (2018) ''' + bcolors.ENDC)
''' + bcolors.ENDC)
self.menu() self.menu()
@ -47,14 +48,14 @@ Task manager from Zordsdavini (2018)
self.menu() self.menu()
elif menu == 't': elif menu == 't':
self.manage_tag() self.tag.manage_tag()
self.menu() self.menu()
elif menu == 'q': elif menu == 'q':
self.bye() self.bye()
else: else:
print(bcolors.FAIL + 'This is not implemented...\n' + bcolors.ENDC) print(bcolors.FAIL + 'This is not implemented...' + bcolors.ENDC)
self.menu() self.menu()
def about(self): def about(self):
@ -74,107 +75,6 @@ q - exit
print(bcolors.FAIL + 'bye o/' + bcolors.ENDC) print(bcolors.FAIL + 'bye o/' + bcolors.ENDC)
sys.exit(0) 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__': if __name__ == '__main__':
a = Main() a = Main()