From 7b1bcc3250d19bfe1e50cc5be2d03cf216dcdcec Mon Sep 17 00:00:00 2001 From: Syed Hassan Raza Date: Wed, 8 Jul 2015 16:58:38 +0500 Subject: [PATCH] start-date-reset-to-DEFAULT_START_DATE --- cms/djangoapps/contentstore/views/item.py | 14 +++++++++-- .../contentstore/views/tests/test_item.py | 25 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/cms/djangoapps/contentstore/views/item.py b/cms/djangoapps/contentstore/views/item.py index 414d61908b..1032a1afe9 100644 --- a/cms/djangoapps/contentstore/views/item.py +++ b/cms/djangoapps/contentstore/views/item.py @@ -801,6 +801,8 @@ def create_xblock_info(xblock, data=None, metadata=None, include_ancestor_info=F else: child_info = None + release_date = _get_release_date(xblock, user) + if xblock.category != 'course': visibility_state = _compute_visibility_state(xblock, child_info, is_xblock_unit and has_changes) else: @@ -835,7 +837,7 @@ def create_xblock_info(xblock, data=None, metadata=None, include_ancestor_info=F "published_on": get_default_time_display(xblock.published_on) if published and xblock.published_on else None, "studio_url": xblock_studio_url(xblock, parent_xblock), "released_to_students": datetime.now(UTC) > xblock.start, - "release_date": _get_release_date(xblock, user), + "release_date": release_date, "visibility_state": visibility_state, "has_explicit_staff_lock": xblock.fields['visible_to_staff_only'].is_set_on(xblock), "start": xblock.fields['start'].to_json(xblock.start), @@ -1036,7 +1038,15 @@ def _get_release_date(xblock, user=None): Returns the release date for the xblock, or None if the release date has never been set. """ # If year of start date is less than 1900 then reset the start date to DEFAULT_START_DATE - if xblock.start.year < 1900 and user: + reset_to_default = False + try: + reset_to_default = xblock.start.year < 1900 + except ValueError: + # For old mongo courses, accessing the start attribute calls `to_json()`, + # which raises a `ValueError` for years < 1900. + reset_to_default = True + + if reset_to_default and user: xblock.start = DEFAULT_START_DATE xblock = _update_with_callback(xblock, user) diff --git a/cms/djangoapps/contentstore/views/tests/test_item.py b/cms/djangoapps/contentstore/views/tests/test_item.py index 4261042711..1050bec27e 100644 --- a/cms/djangoapps/contentstore/views/tests/test_item.py +++ b/cms/djangoapps/contentstore/views/tests/test_item.py @@ -1532,7 +1532,6 @@ class TestXBlockInfo(ItemTest): def test_vertical_xblock_info(self): vertical = modulestore().get_item(self.vertical.location) - vertical.start = datetime(year=1899, month=1, day=1, tzinfo=UTC) xblock_info = create_xblock_info( vertical, @@ -1553,6 +1552,29 @@ class TestXBlockInfo(ItemTest): ) self.validate_component_xblock_info(xblock_info) + @ddt.data(ModuleStoreEnum.Type.split, ModuleStoreEnum.Type.mongo) + def test_validate_start_date(self, store_type): + """ + Validate if start-date year is less than 1900 reset the date to DEFAULT_START_DATE. + """ + with self.store.default_store(store_type): + course = CourseFactory.create() + chapter = ItemFactory.create( + parent_location=course.location, category='chapter', display_name='Week 1' + ) + + chapter.start = datetime(year=1899, month=1, day=1, tzinfo=UTC) + + xblock_info = create_xblock_info( + chapter, + include_child_info=True, + include_children_predicate=ALWAYS, + include_ancestor_info=True, + user=self.user + ) + + self.assertEqual(xblock_info['start'], DEFAULT_START_DATE.strftime('%Y-%m-%dT%H:%M:%SZ')) + def validate_course_xblock_info(self, xblock_info, has_child_info=True, course_outline=False): """ Validate that the xblock info is correct for the test course. @@ -1605,7 +1627,6 @@ class TestXBlockInfo(ItemTest): self.assertEqual(xblock_info['display_name'], 'Unit 1') self.assertTrue(xblock_info['published']) self.assertEqual(xblock_info['edited_by'], 'testuser') - self.assertEqual(xblock_info['start'], DEFAULT_START_DATE.strftime('%Y-%m-%dT%H:%M:%SZ')) # Validate that the correct ancestor info has been included ancestor_info = xblock_info.get('ancestor_info', None)