From 01882078199585e755eb3f25e80169a2cfcb41a5 Mon Sep 17 00:00:00 2001 From: Alexander Kryklia Date: Tue, 22 Oct 2013 16:23:25 +0300 Subject: [PATCH] Fix to_json of RelativeTime to support Isotime format. --- common/lib/xmodule/xmodule/fields.py | 18 +++++++++--------- .../lib/xmodule/xmodule/tests/test_fields.py | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/common/lib/xmodule/xmodule/fields.py b/common/lib/xmodule/xmodule/fields.py index 2fc6a2a055..424ed208a0 100644 --- a/common/lib/xmodule/xmodule/fields.py +++ b/common/lib/xmodule/xmodule/fields.py @@ -196,7 +196,7 @@ class RelativeTime(Field): if isinstance(value, float): # backward compatibility if value > 86400: value = 86400 - return str(datetime.timedelta(seconds=value)) + return self._str(datetime.timedelta(seconds=value)) if isinstance(value, datetime.timedelta): if value.total_seconds() > 86400: # sanity check @@ -204,18 +204,18 @@ class RelativeTime(Field): "RelativeTime max value is 23:59:59=86400.0 seconds, " "but {} seconds is passed".format(value.total_seconds()) ) - return str(value) + return self._str(value) raise TypeError("RelativeTime: cannot convert {!r} to json".format(value)) - def __str__(self): + def _str(self, value): """ - Makes first H in str representation non-optional. + Makes first 'H' in str representation non-optional. str(timedelta) has [H]H:MM:SS format, which is not suitable - for front-end (and IsoTime standart), so we forse HH:MM:SS + for front-end (and IsoTime standart), so we forse HH:MM:SS format. """ - _str = super(RelativeTime, slef).__str__(self) - if len(_str) == 7: - _str = '0' + _str - return _str + string = str(value) + if len(string) == 7: + string = '0' + string + return string diff --git a/common/lib/xmodule/xmodule/tests/test_fields.py b/common/lib/xmodule/xmodule/tests/test_fields.py index cc0a34b0e0..a159540650 100644 --- a/common/lib/xmodule/xmodule/tests/test_fields.py +++ b/common/lib/xmodule/xmodule/tests/test_fields.py @@ -145,7 +145,7 @@ class RelativeTimeTest(unittest.TestCase): def test_to_json(self): self.assertEqual( - "1:02:03", + "01:02:03", RelativeTimeTest.delta.to_json(datetime.timedelta(seconds=3723)) ) self.assertEqual( @@ -153,7 +153,7 @@ class RelativeTimeTest(unittest.TestCase): RelativeTimeTest.delta.to_json(None) ) self.assertEqual( - "0:01:40", + "00:01:40", RelativeTimeTest.delta.to_json(100.0) ) @@ -170,7 +170,7 @@ class RelativeTimeTest(unittest.TestCase): def test_str(self): self.assertEqual( - "1:02:03", + "01:02:03", RelativeTimeTest.delta.to_json(datetime.timedelta(seconds=3723)) ) self.assertEqual(