From c6eda66e1bd1a8a6887e7bc15fbf2873eb5c5fc3 Mon Sep 17 00:00:00 2001 From: Arnas Udovicius Date: Fri, 16 Feb 2018 15:52:58 +0200 Subject: [PATCH] tag add/delete; link/unlink task to tag --- model.py | 50 +++++++++++++++++++---- tag.py | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ task.py | 73 +++++++++++++++++++++++++++------- ztm.py | 110 +++----------------------------------------------- 4 files changed, 226 insertions(+), 126 deletions(-) create mode 100644 tag.py diff --git a/model.py b/model.py index aa95965..55ab220 100644 --- a/model.py +++ b/model.py @@ -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() diff --git a/tag.py b/tag.py new file mode 100644 index 0000000..4288f9a --- /dev/null +++ b/tag.py @@ -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? (?/+-