From e5c6a45c96520ae64878eb0920f4eed1295f3563 Mon Sep 17 00:00:00 2001 From: Alexander Artemenko Date: Wed, 11 Feb 2009 09:16:26 +0300 Subject: [PATCH] Added ability to show opened tasks with or without closed. Two new commands 'show-all' and 'show-closed' were introdiced in the command line client. --- gtd.py | 15 ++++++++++++--- gtdzen/__init__.py | 7 ++++--- tests/task_tests.py | 22 +++++++++++++++++++++- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/gtd.py b/gtd.py index 3d18f27..605c153 100755 --- a/gtd.py +++ b/gtd.py @@ -28,7 +28,7 @@ def __init__(self, database): self.gtd = gtdzen.GTD(database) def run(self, cmd_name, args): - method = getattr(self, 'cmd_%s' % cmd_name, self.cmd_help) + method = getattr(self, 'cmd_%s' % cmd_name.replace('-', '_'), self.cmd_help) return method(*args) def cmd_add(self, title, priority = 1, tags = u''): @@ -39,15 +39,24 @@ def cmd_add(self, title, priority = 1, tags = u''): ) print u'Task %s was added' % task - def cmd_show(self, tags = u''): + def cmd_show(self, tags = u'', mode = 'open'): with_tags, without_tags = _add_remove_tags(_parse_tags(tags)) - tasks = self.gtd.getTasks(tags = with_tags, without_tags = without_tags) + tasks = self.gtd.getTasks( + tags = with_tags, + without_tags = without_tags, + show = mode) if len(tasks) > 0: for task in tasks: print u'%d %s' % (task.id, task) else: print u'No tasks' + def cmd_show_closed(self, *args, **kwargs): + return self.cmd_show(mode = 'closed', *args, **kwargs) + + def cmd_show_all(self, *args, **kwargs): + return self.cmd_show(mode = 'all', *args, **kwargs) + def cmd_update(self, task_ids, title = None, priority = None, tags = None): if tags is not None: add_tags, remove_tags = _add_remove_tags(_parse_tags(tags)) diff --git a/gtdzen/__init__.py b/gtdzen/__init__.py index 1a05b8a..a2ebde2 100644 --- a/gtdzen/__init__.py +++ b/gtdzen/__init__.py @@ -28,7 +28,8 @@ def addTask(self, title, note = None, tags = [], priority = 1): def getTaskById(self, task_id): return Task.query.get(task_id) - def getTasks(self, tags = [], without_tags = [], show_closed = False): + def getTasks(self, tags = [], without_tags = [], show = 'open'): + assert(show in ('all', 'open', 'closed')) query = Task.query tags = make_list(tags) @@ -39,8 +40,8 @@ def getTasks(self, tags = [], without_tags = [], show_closed = False): for tag in without_tags: query = query.filter(not_(Task.tags.any(title = tag))) - if not show_closed: - query = query.filter_by(done = False) + if show != 'all': + query = query.filter_by(done = (show == 'closed')) return query.all() diff --git a/tests/task_tests.py b/tests/task_tests.py index dca6339..8560a25 100644 --- a/tests/task_tests.py +++ b/tests/task_tests.py @@ -126,7 +126,7 @@ def testCloseTask(self): self.assertEqual(1, len(tasks)) self.assertEqual(u'Second', tasks[0].title) - tasks = self.gtd.getTasks(show_closed = True) + tasks = self.gtd.getTasks(show = 'all') self.assertEqual(2, len(tasks)) self.assertEqual(u'First', tasks[0].title) self.assertEqual(u'Second', tasks[1].title) @@ -140,3 +140,23 @@ def testGetTasksNegation(self): self.assertEqual(task2, tasks[0]) self.assertEqual(task1, tasks[1]) + def testShowModes(self): + self.gtd.closeTask(self.gtd.addTask(title = u'Closed', priority = 10).id) + self.gtd.addTask(title = u'Open', priority = 1) + + # by default, only 'open' tasks + tasks = self.gtd.getTasks() + self.assertEqual(1, len(tasks)) + self.assertEqual(u'Open', tasks[0].title) + + tasks = self.gtd.getTasks(show = 'all') + self.assertEqual(2, len(tasks)) + self.assertEqual(u'Closed', tasks[0].title) + self.assertEqual(u'Open', tasks[1].title) + + tasks = self.gtd.getTasks(show = 'closed') + self.assertEqual(1, len(tasks)) + self.assertEqual(u'Closed', tasks[0].title) + + self.assertRaises(AssertionError, self.gtd.getTasks, show = 'bad-mode') +