Commit 50eea969 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add --date flag for setting custom completion date.

parent ce119824
...@@ -195,6 +195,38 @@ class DoCommandTest(CommandTest.CommandTest): ...@@ -195,6 +195,38 @@ class DoCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "Completed: x %s Baz p:1\n" % self.today) self.assertEquals(self.output, "Completed: x %s Baz p:1\n" % self.today)
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_do_custom_date1(self):
command = DoCommand.DoCommand(["-d", "2014-11-18", "3"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Completed: x 2014-11-18 Baz p:1\n")
self.assertEquals(self.errors, "")
def test_do_custom_date2(self):
command = DoCommand.DoCommand(["-d", "2014-11-18", "1"], self.todolist, self.out, self.error, _yes_prompt)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, " 2 Bar p:1\n 3 Baz p:1\nCompleted: x 2014-11-18 Bar p:1\nCompleted: x 2014-11-18 Baz p:1\nCompleted: x 2014-11-18 Foo id:1\n")
self.assertEquals(self.errors, "")
def test_do_custom_date3(self):
command = DoCommand.DoCommand(["--date=2014-11-18", "3"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Completed: x 2014-11-18 Baz p:1\n")
self.assertEquals(self.errors, "")
def test_do_custom_date4(self):
command = DoCommand.DoCommand(["-d", "foo", "3"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Completed: x 2014-11-19 Baz p:1\n")
self.assertEquals(self.errors, "")
def test_empty(self): def test_empty(self):
command = DoCommand.DoCommand([], self.todolist, self.out, self.error) command = DoCommand.DoCommand([], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
""" Tests for the TodoBase class. """ """ Tests for the TodoBase class. """
from datetime import date from datetime import date, timedelta
import re import re
import unittest import unittest
...@@ -273,6 +273,13 @@ class TodoBaseTester(unittest.TestCase): ...@@ -273,6 +273,13 @@ class TodoBaseTester(unittest.TestCase):
self.assertEquals(todo.src, "x 2014-06-13 Foo") self.assertEquals(todo.src, "x 2014-06-13 Foo")
def test_set_complete6(self):
todo = TodoBase.TodoBase("Foo")
yesterday = date.today() - timedelta(1)
todo.set_completed(yesterday)
self.assertEquals(todo.src, "x %s Foo" % yesterday.isoformat())
def test_set_source_text(self): def test_set_source_text(self):
todo = TodoBase.TodoBase("(B) Foo") todo = TodoBase.TodoBase("(B) Foo")
......
...@@ -14,11 +14,13 @@ ...@@ -14,11 +14,13 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import date
import re import re
from DCommand import DCommand from DCommand import DCommand
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
from Recurrence import advance_recurring_todo, strict_advance_recurring_todo from Recurrence import advance_recurring_todo, strict_advance_recurring_todo
from Utils import date_string_to_date
class DoCommand(DCommand): class DoCommand(DCommand):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
...@@ -27,16 +29,19 @@ class DoCommand(DCommand): ...@@ -27,16 +29,19 @@ class DoCommand(DCommand):
p_prompt=lambda a: None): p_prompt=lambda a: None):
self.strict_recurrence = False self.strict_recurrence = False
self.completion_date = date.today()
super(DoCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt) super(DoCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
def get_flags(self): def get_flags(self):
""" Additional flags. """ """ Additional flags. """
return ("s", ["strict"]) return ("d:s", ["date=", "strict"])
def process_flag(self, p_opt, p_value): def process_flag(self, p_opt, p_value):
if p_opt == "-s" or p_opt == "--strict": if p_opt == "-s" or p_opt == "--strict":
self.strict_recurrence = True self.strict_recurrence = True
elif p_opt == "-d" or p_opt == "--date":
self.completion_date = date_string_to_date(p_value) or date.today()
def _handle_recurrence(self): def _handle_recurrence(self):
if self.todo.has_tag('rec'): if self.todo.has_tag('rec'):
...@@ -72,10 +77,10 @@ class DoCommand(DCommand): ...@@ -72,10 +77,10 @@ class DoCommand(DCommand):
The core operation on the todo itself. Also used to operate on The core operation on the todo itself. Also used to operate on
child/parent tasks. child/parent tasks.
""" """
self.todolist.set_todo_completed(p_todo) self.todolist.set_todo_completed(p_todo, self.completion_date)
def usage(self): def usage(self):
return """Synopsis: do [--force] [--strict] <NUMBER>""" return """Synopsis: do [--date] [--force] [--strict] <NUMBER>"""
def help(self): def help(self):
return """Marks the todo with given number as complete. return """Marks the todo with given number as complete.
...@@ -91,4 +96,6 @@ is used to calculate the new recurrence date. Using --strict prevents this, ...@@ -91,4 +96,6 @@ is used to calculate the new recurrence date. Using --strict prevents this,
then the actual due date of the todo item is used to calculate the new then the actual due date of the todo item is used to calculate the new
recurrence date. Note that a future due date is always used as such to recurrence date. Note that a future due date is always used as such to
calculate the new due date. calculate the new due date.
Use --date to set a custom completion date.
""" """
...@@ -185,7 +185,7 @@ class TodoBase(object): ...@@ -185,7 +185,7 @@ class TodoBase(object):
""" """
return self.fields['completionDate'] return self.fields['completionDate']
def set_completed(self): def set_completed(self, p_completion_date=date.today()):
""" """
Marks the todo as complete. Marks the todo as complete.
Sets the completed flag and sets the completion date to today. Sets the completed flag and sets the completion date to today.
...@@ -194,11 +194,10 @@ class TodoBase(object): ...@@ -194,11 +194,10 @@ class TodoBase(object):
self.set_priority(None) self.set_priority(None)
self.fields['completed'] = True self.fields['completed'] = True
today = date.today() self.fields['completionDate'] = p_completion_date
self.fields['completionDate'] = today
self.src = re.sub(r'^(\([A-Z]\) )?', \ self.src = re.sub(r'^(\([A-Z]\) )?', \
'x ' + today.isoformat() + ' ', self.src) 'x ' + p_completion_date.isoformat() + ' ', self.src)
def set_creation_date(self, p_date=date.today()): def set_creation_date(self, p_date=date.today()):
""" """
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
A list of todo items. A list of todo items.
""" """
from datetime import date
import re import re
import Filter import Filter
...@@ -164,8 +165,8 @@ class TodoListBase(object): ...@@ -164,8 +165,8 @@ class TodoListBase(object):
def todos(self): def todos(self):
return self._todos return self._todos
def set_todo_completed(self, p_todo): def set_todo_completed(self, p_todo, p_completion_date=date.today()):
p_todo.set_completed() p_todo.set_completed(p_completion_date)
self.dirty = True self.dirty = True
def set_priority(self, p_todo, p_priority): def set_priority(self, p_todo, p_priority):
......
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