Commit 8d1f2e16 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add function to add a tag to a todo.

parent 4b5e72b7
...@@ -55,13 +55,20 @@ class TodoBase(object): ...@@ -55,13 +55,20 @@ class TodoBase(object):
if p_value == "" or t == p_value] if p_value == "" or t == p_value]
return len(result) > 0 return len(result) > 0
def set_tag(self, p_key, p_value=""): def add_tag(self, p_key, p_value):
""" Adds a tag to the todo. """
self.set_tag(p_key, p_value, True)
def set_tag(self, p_key, p_value="", p_force_add=False):
""" """
Sets a occurrence of the tag identified by p_key. Sets an arbitrary Sets a occurrence of the tag identified by p_key. Sets an arbitrary
instance of the tag when the todo contains multiple tags with this key. instance of the tag when the todo contains multiple tags with this key.
When p_key does not exist, the tag is added. When p_key does not exist, the tag is added.
When p_value is not set, the tag will be removed. When p_value is not set, the tag will be removed.
When p_force_add is true, a tag will always be added to the todo, in
case there is already a tag with the given key.
""" """
if p_value == "": if p_value == "":
...@@ -69,7 +76,7 @@ class TodoBase(object): ...@@ -69,7 +76,7 @@ class TodoBase(object):
return return
value = self.tag_value(p_key) value = self.tag_value(p_key)
if value: if not p_force_add and value:
# remove old value from the tags # remove old value from the tags
self.fields['tags'] = [t for t in self.fields['tags'] \ self.fields['tags'] = [t for t in self.fields['tags'] \
if t[0] != p_key and t[1] != value] if t[0] != p_key and t[1] != value]
......
...@@ -17,7 +17,7 @@ class TodoBaseTester(unittest.TestCase): ...@@ -17,7 +17,7 @@ class TodoBaseTester(unittest.TestCase):
self.assertTrue(todo.has_tag('blah')) self.assertTrue(todo.has_tag('blah'))
self.assertTrue(todo.has_tag('blah', 'zah:haz')) self.assertTrue(todo.has_tag('blah', 'zah:haz'))
def test_add_tag(self): def test_add_tag1(self):
todo = TodoBase.TodoBase("(C) Foo") todo = TodoBase.TodoBase("(C) Foo")
todo.set_tag('foo', 'bar') todo.set_tag('foo', 'bar')
...@@ -27,6 +27,12 @@ class TodoBaseTester(unittest.TestCase): ...@@ -27,6 +27,12 @@ class TodoBaseTester(unittest.TestCase):
self.assertFalse(todo.has_tag('bar')) self.assertFalse(todo.has_tag('bar'))
self.assertTrue(re.search(r'\bfoo:bar\b', todo.src)) self.assertTrue(re.search(r'\bfoo:bar\b', todo.src))
def test_add_tag2(self):
todo = TodoBase.TodoBase("(C) Foo id:1")
todo.add_tag('id', '2')
self.assertEquals(todo.source(), '(C) Foo id:1 id:2')
def test_set_tag(self): def test_set_tag(self):
todo = TodoBase.TodoBase("(C) Foo foo:bar") todo = TodoBase.TodoBase("(C) Foo foo:bar")
todo.set_tag('foo', 'baz') todo.set_tag('foo', 'baz')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment