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
def get_task(self, aid):
query = '''
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 = ?
'''
query = 'SELECT t.* FROM task t WHERE t.aid = ?'
cursor = self.conn.cursor()
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):
cursor = self.conn.cursor()
@ -101,3 +119,21 @@ class Model:
cursor = self.conn.cursor()
cursor.execute(query, (name,))
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:
self.edit_task(aid)
else:
print(bcolors.FAIL + 'Task was not selected...\n' + bcolors.ENDC)
print(bcolors.FAIL + 'Task was not selected...' + bcolors.ENDC)
def add(self):
print('\n' + bcolors.HEADER + 'Adding task\n' + bcolors.ENDC)
print(bcolors.HEADER + 'Adding task' + bcolors.ENDC)
description = input('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)
def manage_task(self, 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 = ' '
if task['long_term'] and task['long_term'] != 'FALSE':
long_term = 'x'
print('''
%s
tags = ''
if task['tags']:
tags = ' '.join([t['name'] for t in task['tags']])
print('''%s
Description: %s
Tags: [%s]
Long Term: [%s]
Created: %s
''' % (task['aid'], task['description'], '', long_term, task['created_at']))
Created: %s ''' % (task['aid'], task['description'], tags, long_term, task['created_at']))
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)
@ -76,6 +78,12 @@ Created: %s
elif menu == '*':
self.toggle_long_term(aid)
elif menu == '+':
self.add_tags(aid)
elif menu == '-':
self.remove_tags(aid)
elif menu == 'v':
self.toggle_done(aid)
@ -83,7 +91,7 @@ Created: %s
return
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)
def manage_task_about(self, aid):
@ -110,10 +118,14 @@ q - exit
if task['long_term'] and task['long_term'] != 'FALSE':
long_term = 'x'
tags = ''
if task['tags']:
tags = ' '.join([t['name'] for t in task['tags']])
content = '''%s
Tags: [%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':
content += '\nFinished: ' + task['finished_at']
@ -146,19 +158,52 @@ Created: %s ''' % (task['aid'], '', long_term, task['created_at'])
found = True
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)
def toggle_long_term(self, 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)
def toggle_done(self, 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)
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):
print(bcolors.FAIL + 'bye o/' + bcolors.ENDC)
sys.exit(0)

110
ztm.py
View file

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