From cab6c9fcbd9e19ba3d89b80ab901087dfbf3e64f Mon Sep 17 00:00:00 2001 From: Awais Jibran Date: Tue, 31 Jul 2018 22:21:25 +0500 Subject: [PATCH] Fix discussion blackout date pasrsing. EDUCATOR-3260 --- cms/djangoapps/contentstore/tests/tests.py | 24 +++++++++++++++++++-- common/lib/xmodule/xmodule/course_module.py | 9 ++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/tests.py b/cms/djangoapps/contentstore/tests/tests.py index 14fd564e1e..a778f0536c 100644 --- a/cms/djangoapps/contentstore/tests/tests.py +++ b/cms/djangoapps/contentstore/tests/tests.py @@ -354,19 +354,39 @@ class ForumTestCase(CourseTestCase): super(ForumTestCase, self).setUp() self.course = CourseFactory.create(org='testX', number='727', display_name='Forum Course') + def set_blackout_dates(self, blackout_dates): + """Helper method to set blackout dates in course.""" + self.course.discussion_blackouts = [ + [start_date.isoformat(), end_date.isoformat()] for start_date, end_date in blackout_dates + ] + def test_blackouts(self): now = datetime.datetime.now(UTC) times1 = [ (now - datetime.timedelta(days=14), now - datetime.timedelta(days=11)), (now + datetime.timedelta(days=24), now + datetime.timedelta(days=30)) ] - self.course.discussion_blackouts = [(t.isoformat(), t2.isoformat()) for t, t2 in times1] + self.set_blackout_dates(times1) self.assertTrue(self.course.forum_posts_allowed) times2 = [ (now - datetime.timedelta(days=14), now + datetime.timedelta(days=2)), (now + datetime.timedelta(days=24), now + datetime.timedelta(days=30)) ] - self.course.discussion_blackouts = [(t.isoformat(), t2.isoformat()) for t, t2 in times2] + self.set_blackout_dates(times2) + self.assertFalse(self.course.forum_posts_allowed) + + # Single date set for allowed forum posts. + self.course.discussion_blackouts = [ + now + datetime.timedelta(days=24), + now + datetime.timedelta(days=30) + ] + self.assertTrue(self.course.forum_posts_allowed) + + # Single date set for restricted forum posts. + self.course.discussion_blackouts = [ + now - datetime.timedelta(days=24), + now + datetime.timedelta(days=30) + ] self.assertFalse(self.course.forum_posts_allowed) # test if user gives empty blackout date it should return true for forum_posts_allowed diff --git a/common/lib/xmodule/xmodule/course_module.py b/common/lib/xmodule/xmodule/course_module.py index c79007241b..c71cfba7a3 100644 --- a/common/lib/xmodule/xmodule/course_module.py +++ b/common/lib/xmodule/xmodule/course_module.py @@ -1273,12 +1273,17 @@ class CourseDescriptor(CourseFields, SequenceDescriptor, LicenseMixin): Get a list of dicts with start and end fields with datetime values from the discussion_blackouts setting """ + + blackout_dates = self.discussion_blackouts date_proxy = Date() + if blackout_dates and type(blackout_dates[0]) not in (list, tuple): + blackout_dates = [blackout_dates] + try: ret = [ {"start": date_proxy.from_json(start), "end": date_proxy.from_json(end)} for start, end - in filter(None, self.discussion_blackouts) + in filter(None, blackout_dates) ] for blackout in ret: if not blackout["start"] or not blackout["end"]: @@ -1287,7 +1292,7 @@ class CourseDescriptor(CourseFields, SequenceDescriptor, LicenseMixin): except (TypeError, ValueError): log.info( "Error parsing discussion_blackouts %s for course %s", - self.discussion_blackouts, + blackout_dates, self.id ) return []