From d9dc31809f6caeb1f0eb20e47e76242114735e04 Mon Sep 17 00:00:00 2001 From: Juan Camilo Montoya Date: Thu, 23 Jun 2016 14:44:56 -0500 Subject: [PATCH 1/4] a first contribution to fix TNL-4204 byte sized bug --- common/lib/xmodule/xmodule/html_module.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/lib/xmodule/xmodule/html_module.py b/common/lib/xmodule/xmodule/html_module.py index 6f5889af66..8eeff4889b 100644 --- a/common/lib/xmodule/xmodule/html_module.py +++ b/common/lib/xmodule/xmodule/html_module.py @@ -440,8 +440,7 @@ class CourseInfoModule(CourseInfoFields, HtmlModuleMixin): return self.data else: course_updates = [item for item in self.items if item.get('status') == self.STATUS_VISIBLE] - course_updates.sort(key=lambda item: CourseInfoModule.safe_parse_date(item['date']), reverse=True) - + course_updates.sort(key=lambda item: (datetime.strptime(item['date'], '%B %d, %Y'), item['id']), reverse=True) context = { 'visible_updates': course_updates[:3], 'hidden_updates': course_updates[3:], From 7a09317160811aa3d827361cc3d19ba1d133f57a Mon Sep 17 00:00:00 2001 From: Juan Camilo Montoya Date: Thu, 23 Jun 2016 18:03:42 -0500 Subject: [PATCH 2/4] minor fix to use safe_parse_date function --- common/lib/xmodule/xmodule/html_module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/lib/xmodule/xmodule/html_module.py b/common/lib/xmodule/xmodule/html_module.py index 8eeff4889b..828e74f73d 100644 --- a/common/lib/xmodule/xmodule/html_module.py +++ b/common/lib/xmodule/xmodule/html_module.py @@ -440,7 +440,7 @@ class CourseInfoModule(CourseInfoFields, HtmlModuleMixin): return self.data else: course_updates = [item for item in self.items if item.get('status') == self.STATUS_VISIBLE] - course_updates.sort(key=lambda item: (datetime.strptime(item['date'], '%B %d, %Y'), item['id']), reverse=True) + course_updates.sort(key=lambda item: (CourseInfoModule.safe_parse_date(item['date']), item['id']), reverse=True) context = { 'visible_updates': course_updates[:3], 'hidden_updates': course_updates[3:], From c5809b95e356dcdbdc542ca7eccd8bb8b090ffe6 Mon Sep 17 00:00:00 2001 From: Juan Camilo Montoya Date: Mon, 27 Jun 2016 14:20:19 -0500 Subject: [PATCH 3/4] indentation fix to avoid a too long line. --- common/lib/xmodule/xmodule/html_module.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/common/lib/xmodule/xmodule/html_module.py b/common/lib/xmodule/xmodule/html_module.py index 828e74f73d..c93351dbaa 100644 --- a/common/lib/xmodule/xmodule/html_module.py +++ b/common/lib/xmodule/xmodule/html_module.py @@ -440,7 +440,10 @@ class CourseInfoModule(CourseInfoFields, HtmlModuleMixin): return self.data else: course_updates = [item for item in self.items if item.get('status') == self.STATUS_VISIBLE] - course_updates.sort(key=lambda item: (CourseInfoModule.safe_parse_date(item['date']), item['id']), reverse=True) + course_updates.sort( + key=lambda item: (CourseInfoModule.safe_parse_date(item['date']), item['id']), + reverse=True + ) context = { 'visible_updates': course_updates[:3], 'hidden_updates': course_updates[3:], From b1f748b5ba147c8097b384f6275a62915a89e8ba Mon Sep 17 00:00:00 2001 From: Juan Camilo Montoya Date: Tue, 2 Aug 2016 13:03:31 -0500 Subject: [PATCH 4/4] added unit test --- .../xmodule/xmodule/tests/test_html_module.py | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/common/lib/xmodule/xmodule/tests/test_html_module.py b/common/lib/xmodule/xmodule/tests/test_html_module.py index 20288c2f5f..0f3992af3f 100644 --- a/common/lib/xmodule/xmodule/tests/test_html_module.py +++ b/common/lib/xmodule/xmodule/tests/test_html_module.py @@ -181,3 +181,66 @@ class CourseInfoModuleTestCase(unittest.TestCase): info_module.get_html() except ValueError: self.fail("CourseInfoModule could not parse an invalid date!") + + def test_updates_order(self): + """ + Tests that a course info module will render its updates in the correct order. + """ + sample_update_data = [ + { + "id": 3, + "date": "March 18, 1982", + "content": "This is a very important update that was inserted last with an older date!", + "status": CourseInfoModule.STATUS_VISIBLE, + }, + { + "id": 1, + "date": "January 1, 2012", + "content": "This is a very important update that was inserted first!", + "status": CourseInfoModule.STATUS_VISIBLE, + }, + { + "id": 2, + "date": "January 1, 2012", + "content": "This is a very important update that was inserted second!", + "status": CourseInfoModule.STATUS_VISIBLE, + } + ] + info_module = CourseInfoModule( + Mock(), + Mock(), + DictFieldData({'items': sample_update_data, 'data': ""}), + Mock() + ) + + # This is the expected context that should be used by the render function + expected_context = { + 'visible_updates': [ + { + "id": 2, + "date": "January 1, 2012", + "content": "This is a very important update that was inserted second!", + "status": CourseInfoModule.STATUS_VISIBLE, + }, + { + "id": 1, + "date": "January 1, 2012", + "content": "This is a very important update that was inserted first!", + "status": CourseInfoModule.STATUS_VISIBLE, + }, + { + "id": 3, + "date": "March 18, 1982", + "content": "This is a very important update that was inserted last with an older date!", + "status": CourseInfoModule.STATUS_VISIBLE, + } + ], + 'hidden_updates': [], + } + template_name = "{0}/course_updates.html".format(info_module.TEMPLATE_DIR) + info_module.get_html() + # Assertion to validate that render function is called with the expected context + info_module.system.render_template.assert_called_once_with( + template_name, + expected_context + )