From 7dfb6cc681608b5ddf509ece0bfec4a9f91871e7 Mon Sep 17 00:00:00 2001 From: John Eskew Date: Tue, 19 Sep 2017 18:17:03 -0400 Subject: [PATCH] Change all UTC timezones to import from pytz instead of Django. --- .../tests/test_course_settings.py | 17 +++++++------ .../tests/test_courseware_index.py | 9 ++++--- common/djangoapps/util/tests/test_file.py | 4 ++-- common/lib/xmodule/xmodule/lti_module.py | 4 ++-- common/lib/xmodule/xmodule/seq_module.py | 4 ++-- .../lib/xmodule/xmodule/tests/test_fields.py | 22 ++++++++--------- .../lib/xmodule/xmodule/tests/test_import.py | 4 ++-- .../xmodule/xmodule/tests/test_lti20_unit.py | 4 ++-- .../xmodule/xmodule/tests/test_lti_unit.py | 4 ++-- .../xmodule/tests/test_randomize_module.py | 4 ++-- lms/djangoapps/ccx/tests/test_views.py | 20 +++++++--------- lms/djangoapps/courseware/access.py | 9 ++++--- lms/djangoapps/courseware/access_utils.py | 4 ++-- .../courseware/tests/test_masquerade.py | 4 ++-- lms/djangoapps/courseware/views/views.py | 4 ++-- .../dashboard/tests/test_sysadmin.py | 2 +- .../django_comment_client/tests/test_utils.py | 2 -- lms/djangoapps/django_comment_client/utils.py | 9 ++++--- lms/djangoapps/instructor/tests/test_api.py | 13 +++++----- lms/djangoapps/instructor/tests/test_tools.py | 24 +++++++++---------- lms/djangoapps/instructor/tests/utils.py | 4 ++-- lms/djangoapps/instructor/views/tools.py | 4 ++-- .../models/tests/test_course_details.py | 8 +++---- .../core/djangoapps/profile_images/views.py | 4 ++-- openedx/core/lib/xblock_utils/__init__.py | 4 ++-- .../views/course_home_messages.py | 5 ++-- 26 files changed, 94 insertions(+), 102 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_course_settings.py b/cms/djangoapps/contentstore/tests/test_course_settings.py index bfa1f985a5..b826afc652 100644 --- a/cms/djangoapps/contentstore/tests/test_course_settings.py +++ b/cms/djangoapps/contentstore/tests/test_course_settings.py @@ -10,7 +10,7 @@ import ddt import mock from django.conf import settings from django.test.utils import override_settings -from django.utils.timezone import UTC +from pytz import UTC from milestones.tests.utils import MilestonesTestCaseMixin from mock import Mock, patch @@ -61,7 +61,7 @@ class CourseSettingsEncoderTest(CourseTestCase): doesn't work for these dates. """ details = CourseDetails.fetch(self.course.id) - pre_1900 = datetime.datetime(1564, 4, 23, 1, 1, 1, tzinfo=UTC()) + pre_1900 = datetime.datetime(1564, 4, 23, 1, 1, 1, tzinfo=UTC) details.enrollment_start = pre_1900 dumped_jsondetails = json.dumps(details, cls=CourseSettingsEncoder) loaded_jsondetails = json.loads(dumped_jsondetails) @@ -74,7 +74,7 @@ class CourseSettingsEncoderTest(CourseTestCase): details = { 'number': 1, 'string': 'string', - 'datetime': datetime.datetime.now(UTC()) + 'datetime': datetime.datetime.now(UTC) } jsondetails = json.dumps(details, cls=CourseSettingsEncoder) jsondetails = json.loads(jsondetails) @@ -121,13 +121,12 @@ class CourseDetailsViewTest(CourseTestCase, MilestonesTestCaseMixin): resp = self.client.get_json(url) self.compare_details_with_encoding(json.loads(resp.content), details.__dict__, "virgin get") - utc = UTC() - self.alter_field(url, details, 'start_date', datetime.datetime(2012, 11, 12, 1, 30, tzinfo=utc)) - self.alter_field(url, details, 'start_date', datetime.datetime(2012, 11, 1, 13, 30, tzinfo=utc)) - self.alter_field(url, details, 'end_date', datetime.datetime(2013, 2, 12, 1, 30, tzinfo=utc)) - self.alter_field(url, details, 'enrollment_start', datetime.datetime(2012, 10, 12, 1, 30, tzinfo=utc)) + self.alter_field(url, details, 'start_date', datetime.datetime(2012, 11, 12, 1, 30, tzinfo=UTC)) + self.alter_field(url, details, 'start_date', datetime.datetime(2012, 11, 1, 13, 30, tzinfo=UTC)) + self.alter_field(url, details, 'end_date', datetime.datetime(2013, 2, 12, 1, 30, tzinfo=UTC)) + self.alter_field(url, details, 'enrollment_start', datetime.datetime(2012, 10, 12, 1, 30, tzinfo=UTC)) - self.alter_field(url, details, 'enrollment_end', datetime.datetime(2012, 11, 15, 1, 30, tzinfo=utc)) + self.alter_field(url, details, 'enrollment_end', datetime.datetime(2012, 11, 15, 1, 30, tzinfo=UTC)) self.alter_field(url, details, 'short_description', "Short Description") self.alter_field(url, details, 'overview', "Overview") self.alter_field(url, details, 'intro_video', "intro_video") diff --git a/cms/djangoapps/contentstore/tests/test_courseware_index.py b/cms/djangoapps/contentstore/tests/test_courseware_index.py index 488c3a27f4..a00c9f6061 100644 --- a/cms/djangoapps/contentstore/tests/test_courseware_index.py +++ b/cms/djangoapps/contentstore/tests/test_courseware_index.py @@ -9,7 +9,6 @@ from uuid import uuid4 import ddt import pytest -from dateutil.tz import tzutc from django.conf import settings from lazy.lazy import lazy from mock import patch @@ -1187,7 +1186,7 @@ class GroupConfigurationSearchMongo(CourseTestCase, MixedWithOptionsTestCase): 'content_type': 'Text', 'org': self.course.org, 'content_groups': content_groups, - 'start_date': datetime(2015, 4, 1, 0, 0, tzinfo=tzutc()) + 'start_date': datetime(2015, 4, 1, 0, 0, tzinfo=UTC) } def _html_experiment_group_result(self, html_unit, content_groups): @@ -1207,7 +1206,7 @@ class GroupConfigurationSearchMongo(CourseTestCase, MixedWithOptionsTestCase): 'content_type': 'Text', 'org': self.course.org, 'content_groups': content_groups, - 'start_date': datetime(2015, 4, 1, 0, 0, tzinfo=tzutc()) + 'start_date': datetime(2015, 4, 1, 0, 0, tzinfo=UTC) } def _vertical_experiment_group_result(self, vertical, content_groups): @@ -1215,7 +1214,7 @@ class GroupConfigurationSearchMongo(CourseTestCase, MixedWithOptionsTestCase): Return object with arguments and content group for split_test vertical. """ return { - 'start_date': datetime(2015, 4, 1, 0, 0, tzinfo=tzutc()), + 'start_date': datetime(2015, 4, 1, 0, 0, tzinfo=UTC), 'content': {'display_name': vertical.display_name}, 'course': unicode(self.course.id), 'location': [ @@ -1247,7 +1246,7 @@ class GroupConfigurationSearchMongo(CourseTestCase, MixedWithOptionsTestCase): 'content_type': 'Text', 'org': self.course.org, 'content_groups': None, - 'start_date': datetime(2015, 4, 1, 0, 0, tzinfo=tzutc()) + 'start_date': datetime(2015, 4, 1, 0, 0, tzinfo=UTC) } def _get_index_values_from_call_args(self, mock_index): diff --git a/common/djangoapps/util/tests/test_file.py b/common/djangoapps/util/tests/test_file.py index 2df186bafd..1ae7cabc29 100644 --- a/common/djangoapps/util/tests/test_file.py +++ b/common/djangoapps/util/tests/test_file.py @@ -11,7 +11,7 @@ from django.core import exceptions from django.core.files.uploadedfile import SimpleUploadedFile from django.http import HttpRequest from django.test import TestCase -from django.utils.timezone import UTC +from pytz import UTC from mock import Mock, patch from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locations import CourseLocator @@ -45,7 +45,7 @@ class FilenameGeneratorTestCase(TestCase): """ Tests for course_and_time_based_filename_generator """ - NOW = datetime.strptime('1974-06-22T01:02:03', '%Y-%m-%dT%H:%M:%S').replace(tzinfo=UTC()) + NOW = datetime.strptime('1974-06-22T01:02:03', '%Y-%m-%dT%H:%M:%S').replace(tzinfo=UTC) def setUp(self): super(FilenameGeneratorTestCase, self).setUp() diff --git a/common/lib/xmodule/xmodule/lti_module.py b/common/lib/xmodule/xmodule/lti_module.py index 733fa0bab7..caaf830790 100644 --- a/common/lib/xmodule/xmodule/lti_module.py +++ b/common/lib/xmodule/xmodule/lti_module.py @@ -62,7 +62,7 @@ from xml.sax.saxutils import escape import bleach import mock import oauthlib.oauth1 -from django.utils.timezone import UTC +from pytz import UTC from lxml import etree from oauthlib.oauth1.rfc5849 import signature from pkg_resources import resource_string @@ -889,7 +889,7 @@ oauth_consumer_key="", oauth_signature="frVp4JuvT1mVXlxktiAUjQ7%2F1cw%3D"'} close_date = due_date + self.graceperiod # pylint: disable=no-member else: close_date = due_date - return close_date is not None and datetime.datetime.now(UTC()) > close_date + return close_date is not None and datetime.datetime.now(UTC) > close_date class LTIDescriptor(LTIFields, MetadataOnlyEditingDescriptor, EmptyDataRawDescriptor): diff --git a/common/lib/xmodule/xmodule/seq_module.py b/common/lib/xmodule/xmodule/seq_module.py index c6d0a72b91..ae63fb8b80 100644 --- a/common/lib/xmodule/xmodule/seq_module.py +++ b/common/lib/xmodule/xmodule/seq_module.py @@ -5,10 +5,10 @@ xModule implementation of a learning sequence # pylint: disable=abstract-method import collections from datetime import datetime -from django.utils.timezone import UTC import json import logging from pkg_resources import resource_string +from pytz import UTC from lxml import etree from xblock.core import XBlock @@ -215,7 +215,7 @@ class SequenceModule(SequenceFields, ProctoringFields, XModule): return ( not date or not hide_after_date or - datetime.now(UTC()) < date + datetime.now(UTC) < date ) def student_view(self, context): diff --git a/common/lib/xmodule/xmodule/tests/test_fields.py b/common/lib/xmodule/xmodule/tests/test_fields.py index 4464470eab..670e294549 100644 --- a/common/lib/xmodule/xmodule/tests/test_fields.py +++ b/common/lib/xmodule/xmodule/tests/test_fields.py @@ -3,7 +3,7 @@ import datetime import unittest -from django.utils.timezone import UTC +from pytz import UTC from xmodule.fields import Date, Timedelta, RelativeTime from xmodule.timeinfo import TimeInfo @@ -57,15 +57,15 @@ class DateTest(unittest.TestCase): self.assertEqual(DateTest.date.enforce_type(""), None) self.assertEqual( DateTest.date.enforce_type("2012-12-31T23:00:01"), - datetime.datetime(2012, 12, 31, 23, 0, 1, tzinfo=UTC()) + datetime.datetime(2012, 12, 31, 23, 0, 1, tzinfo=UTC) ) self.assertEqual( DateTest.date.enforce_type(1234567890000), - datetime.datetime(2009, 2, 13, 23, 31, 30, tzinfo=UTC()) + datetime.datetime(2009, 2, 13, 23, 31, 30, tzinfo=UTC) ) self.assertEqual( - DateTest.date.enforce_type(datetime.datetime(2014, 5, 9, 21, 1, 27, tzinfo=UTC())), - datetime.datetime(2014, 5, 9, 21, 1, 27, tzinfo=UTC()) + DateTest.date.enforce_type(datetime.datetime(2014, 5, 9, 21, 1, 27, tzinfo=UTC)), + datetime.datetime(2014, 5, 9, 21, 1, 27, tzinfo=UTC) ) with self.assertRaises(TypeError): DateTest.date.enforce_type([1]) @@ -79,11 +79,11 @@ class DateTest(unittest.TestCase): def test_old_due_date_format(self): current = datetime.datetime.today() self.assertEqual( - datetime.datetime(current.year, 3, 12, 12, tzinfo=UTC()), + datetime.datetime(current.year, 3, 12, 12, tzinfo=UTC), DateTest.date.from_json("March 12 12:00") ) self.assertEqual( - datetime.datetime(current.year, 12, 4, 16, 30, tzinfo=UTC()), + datetime.datetime(current.year, 12, 4, 16, 30, tzinfo=UTC), DateTest.date.from_json("December 4 16:30") ) self.assertIsNone(DateTest.date.from_json("12 12:00")) @@ -92,13 +92,13 @@ class DateTest(unittest.TestCase): """ Test the non-standard args being passed to from_json """ - now = datetime.datetime.now(UTC()) - delta = now - datetime.datetime.fromtimestamp(0, UTC()) + now = datetime.datetime.now(UTC) + delta = now - datetime.datetime.fromtimestamp(0, UTC) self.assertEqual( DateTest.date.from_json(delta.total_seconds() * 1000), now ) - yesterday = datetime.datetime.now(UTC()) - datetime.timedelta(days=-1) + yesterday = datetime.datetime.now(UTC) - datetime.timedelta(days=-1) self.assertEqual(DateTest.date.from_json(yesterday), yesterday) def test_to_json(self): @@ -157,7 +157,7 @@ class TimedeltaTest(unittest.TestCase): class TimeInfoTest(unittest.TestCase): def test_time_info(self): - due_date = datetime.datetime(2000, 4, 14, 10, tzinfo=UTC()) + due_date = datetime.datetime(2000, 4, 14, 10, tzinfo=UTC) grace_pd_string = '1 day 12 hours 59 minutes 59 seconds' timeinfo = TimeInfo(due_date, grace_pd_string) self.assertEqual( diff --git a/common/lib/xmodule/xmodule/tests/test_import.py b/common/lib/xmodule/xmodule/tests/test_import.py index efad2d853a..52d4a9e630 100644 --- a/common/lib/xmodule/xmodule/tests/test_import.py +++ b/common/lib/xmodule/xmodule/tests/test_import.py @@ -8,7 +8,7 @@ from fs.memoryfs import MemoryFS from lxml import etree from mock import Mock, patch -from django.utils.timezone import UTC +from pytz import UTC from xmodule.xml_module import is_pointer_tag from opaque_keys.edx.locations import Location @@ -346,7 +346,7 @@ class ImportTestCase(BaseCourseTestCase): # Check that the child hasn't started yet self.assertLessEqual( - datetime.datetime.now(UTC()), + datetime.datetime.now(UTC), child.start ) diff --git a/common/lib/xmodule/xmodule/tests/test_lti20_unit.py b/common/lib/xmodule/xmodule/tests/test_lti20_unit.py index bd31475288..43011d7a7b 100644 --- a/common/lib/xmodule/xmodule/tests/test_lti20_unit.py +++ b/common/lib/xmodule/xmodule/tests/test_lti20_unit.py @@ -3,7 +3,7 @@ import datetime import textwrap -from django.utils.timezone import UTC +from pytz import UTC from mock import Mock from xmodule.lti_module import LTIDescriptor from xmodule.lti_2_util import LTIError @@ -388,7 +388,7 @@ class LTI20RESTResultServiceTest(LogicTest): Test that we get a 404 when accept_grades_past_due is False and it is past due """ self.setup_system_xmodule_mocks_for_lti20_request_test() - self.xmodule.due = datetime.datetime.now(UTC()) + self.xmodule.due = datetime.datetime.now(UTC) self.xmodule.accept_grades_past_due = False mock_request = self.get_signed_lti20_mock_request(self.GOOD_JSON_PUT) response = self.xmodule.lti_2_0_result_rest_handler(mock_request, "user/abcd") diff --git a/common/lib/xmodule/xmodule/tests/test_lti_unit.py b/common/lib/xmodule/xmodule/tests/test_lti_unit.py index ce83fed603..0017519c2b 100644 --- a/common/lib/xmodule/xmodule/tests/test_lti_unit.py +++ b/common/lib/xmodule/xmodule/tests/test_lti_unit.py @@ -2,7 +2,7 @@ """Test for LTI Xmodule functional logic.""" import datetime -from django.utils.timezone import UTC +from pytz import UTC from mock import Mock, patch, PropertyMock import textwrap from lxml import etree @@ -180,7 +180,7 @@ class LTIModuleTest(LogicTest): Should fail if we do not accept past due grades, and it is past due. """ self.xmodule.accept_grades_past_due = False - self.xmodule.due = datetime.datetime.now(UTC()) + self.xmodule.due = datetime.datetime.now(UTC) self.xmodule.graceperiod = Timedelta().from_json("0 seconds") request = Request(self.environ) request.body = self.get_request_body() diff --git a/common/lib/xmodule/xmodule/tests/test_randomize_module.py b/common/lib/xmodule/xmodule/tests/test_randomize_module.py index 798d3028b1..48c5220b21 100644 --- a/common/lib/xmodule/xmodule/tests/test_randomize_module.py +++ b/common/lib/xmodule/xmodule/tests/test_randomize_module.py @@ -4,7 +4,7 @@ Test cases covering workflows and behaviors for the Randomize XModule import unittest from datetime import datetime, timedelta -from django.utils.timezone import UTC +from pytz import UTC from opaque_keys.edx.locator import BlockUsageLocator from xblock.fields import ScopeIds from xmodule.randomize_module import RandomizeModule @@ -16,7 +16,7 @@ ORG = 'test_org' COURSE = 'test_course' START = '2013-01-01T01:00:00' -_TODAY = datetime.now(UTC()) +_TODAY = datetime.now(UTC) _LAST_WEEK = _TODAY - timedelta(days=7) _NEXT_WEEK = _TODAY + timedelta(days=7) diff --git a/lms/djangoapps/ccx/tests/test_views.py b/lms/djangoapps/ccx/tests/test_views.py index b388e040cb..7d271e7668 100644 --- a/lms/djangoapps/ccx/tests/test_views.py +++ b/lms/djangoapps/ccx/tests/test_views.py @@ -7,14 +7,12 @@ import re import urlparse import ddt -import pytz from ccx_keys.locator import CCXLocator -from dateutil.tz import tzutc from django.conf import settings from django.core.urlresolvers import resolve, reverse from django.test import RequestFactory from django.test.utils import override_settings -from django.utils.timezone import UTC +from pytz import UTC from django.utils.translation import ugettext as _ from mock import MagicMock, patch from nose.plugins.attrib import attr @@ -183,8 +181,8 @@ class TestCCXProgressChanges(CcxTestCase, LoginEnrollmentTestCase): Set up tests """ super(TestCCXProgressChanges, cls).setUpClass() - start = datetime.datetime(2016, 7, 1, 0, 0, tzinfo=tzutc()) - due = datetime.datetime(2016, 7, 8, 0, 0, tzinfo=tzutc()) + start = datetime.datetime(2016, 7, 1, 0, 0, tzinfo=UTC) + due = datetime.datetime(2016, 7, 8, 0, 0, tzinfo=UTC) cls.course = course = CourseFactory.create(enable_ccx=True, start=start) chapter = ItemFactory.create(start=start, parent=course, category=u'chapter') @@ -467,7 +465,7 @@ class TestCoachDashboard(CcxTestCase, LoginEnrollmentTestCase): """ Get CCX schedule, modify it, save it. """ - today.return_value = datetime.datetime(2014, 11, 25, tzinfo=pytz.UTC) + today.return_value = datetime.datetime(2014, 11, 25, tzinfo=UTC) self.make_coach() ccx = self.make_ccx() url = reverse( @@ -919,10 +917,10 @@ class TestCoachDashboardSchedule(CcxTestCase, LoginEnrollmentTestCase, ModuleSto # Create a course outline self.mooc_start = start = datetime.datetime( - 2010, 5, 12, 2, 42, tzinfo=pytz.UTC + 2010, 5, 12, 2, 42, tzinfo=UTC ) self.mooc_due = due = datetime.datetime( - 2010, 7, 7, 0, 0, tzinfo=pytz.UTC + 2010, 7, 7, 0, 0, tzinfo=UTC ) self.chapters = [ @@ -1004,7 +1002,7 @@ class TestCoachDashboardSchedule(CcxTestCase, LoginEnrollmentTestCase, ModuleSto Hides nodes at a different depth and checks that these nodes are not in the schedule. """ - today.return_value = datetime.datetime(2014, 11, 25, tzinfo=pytz.UTC) + today.return_value = datetime.datetime(2014, 11, 25, tzinfo=UTC) self.make_coach() ccx = self.make_ccx() url = reverse( @@ -1066,7 +1064,7 @@ class TestCCXGrades(FieldOverrideTestMixin, SharedModuleStoreTestCase, LoginEnro # Create a course outline cls.mooc_start = start = datetime.datetime( - 2010, 5, 12, 2, 42, tzinfo=pytz.UTC + 2010, 5, 12, 2, 42, tzinfo=UTC ) chapter = ItemFactory.create( start=start, parent=course, category='sequential' @@ -1332,7 +1330,7 @@ class TestStudentViewsWithCCX(ModuleStoreTestCase): # Create a CCX course and enroll the user in it. self.ccx = CcxFactory(course_id=self.split_course.id, coach=self.coach) - last_week = datetime.datetime.now(UTC()) - datetime.timedelta(days=7) + last_week = datetime.datetime.now(UTC) - datetime.timedelta(days=7) override_field_for_ccx(self.ccx, self.split_course, 'start', last_week) # Required by self.ccx.has_started(). self.ccx_course_key = CCXLocator.from_course_locator(self.split_course.id, self.ccx.id) CourseEnrollment.enroll(self.student, self.ccx_course_key) diff --git a/lms/djangoapps/courseware/access.py b/lms/djangoapps/courseware/access.py index ec7871c4d6..5fb708a956 100644 --- a/lms/djangoapps/courseware/access.py +++ b/lms/djangoapps/courseware/access.py @@ -13,11 +13,10 @@ Note: The access control logic in this file does NOT check for enrollment in import logging from datetime import datetime -import pytz from ccx_keys.locator import CCXLocator from django.conf import settings from django.contrib.auth.models import AnonymousUser -from django.utils.timezone import UTC +from pytz import UTC from opaque_keys.edx.keys import CourseKey, UsageKey from xblock.core import XBlock @@ -274,9 +273,9 @@ def _can_enroll_courselike(user, courselike): debug("Deny: invitation only") return ACCESS_DENIED - now = datetime.now(UTC()) - enrollment_start = courselike.enrollment_start or datetime.min.replace(tzinfo=pytz.UTC) - enrollment_end = courselike.enrollment_end or datetime.max.replace(tzinfo=pytz.UTC) + now = datetime.now(UTC) + enrollment_start = courselike.enrollment_start or datetime.min.replace(tzinfo=UTC) + enrollment_end = courselike.enrollment_end or datetime.max.replace(tzinfo=UTC) if reg_method_ok and enrollment_start < now < enrollment_end: debug("Allow: in enrollment period") return ACCESS_GRANTED diff --git a/lms/djangoapps/courseware/access_utils.py b/lms/djangoapps/courseware/access_utils.py index 7dc3a01b4a..5a9084e8c5 100644 --- a/lms/djangoapps/courseware/access_utils.py +++ b/lms/djangoapps/courseware/access_utils.py @@ -7,7 +7,7 @@ from datetime import datetime, timedelta from logging import getLogger from django.conf import settings -from django.utils.timezone import UTC +from pytz import UTC from courseware.access_response import AccessResponse, StartDateError from courseware.masquerade import is_masquerading_as_student @@ -64,7 +64,7 @@ def check_start_date(user, days_early_for_beta, start, course_key): if start_dates_disabled and not is_masquerading_as_student(user, course_key): return ACCESS_GRANTED else: - now = datetime.now(UTC()) + now = datetime.now(UTC) if start is None or in_preview_mode(): return ACCESS_GRANTED diff --git a/lms/djangoapps/courseware/tests/test_masquerade.py b/lms/djangoapps/courseware/tests/test_masquerade.py index fbe1f9733c..5049438b84 100644 --- a/lms/djangoapps/courseware/tests/test_masquerade.py +++ b/lms/djangoapps/courseware/tests/test_masquerade.py @@ -8,7 +8,7 @@ from datetime import datetime from django.conf import settings from django.core.urlresolvers import reverse from django.test import TestCase -from django.utils.timezone import UTC +from pytz import UTC from mock import patch from nose.plugins.attrib import attr from xblock.runtime import DictKeyValueStore @@ -42,7 +42,7 @@ class MasqueradeTestCase(SharedModuleStoreTestCase, LoginEnrollmentTestCase): @classmethod def setUpClass(cls): super(MasqueradeTestCase, cls).setUpClass() - cls.course = CourseFactory.create(number='masquerade-test', metadata={'start': datetime.now(UTC())}) + cls.course = CourseFactory.create(number='masquerade-test', metadata={'start': datetime.now(UTC)}) cls.info_page = ItemFactory.create( category="course_info", parent_location=cls.course.location, data="OOGIE BLOOGIE", display_name="updates" diff --git a/lms/djangoapps/courseware/views/views.py b/lms/djangoapps/courseware/views/views.py index a07ebac5ce..90872ee083 100644 --- a/lms/djangoapps/courseware/views/views.py +++ b/lms/djangoapps/courseware/views/views.py @@ -49,7 +49,7 @@ from django.shortcuts import redirect from django.utils.decorators import method_decorator from django.utils.http import urlquote_plus from django.utils.text import slugify -from django.utils.timezone import UTC +from pytz import UTC from django.utils.translation import ugettext as _ from django.views.decorators.cache import cache_control from django.views.decorators.csrf import ensure_csrf_cookie @@ -1650,7 +1650,7 @@ def get_financial_aid_courses(user): enrollment.course_overview and \ enrollment.course_overview.eligible_for_financial_aid and \ CourseMode.objects.filter( - Q(_expiration_datetime__isnull=True) | Q(_expiration_datetime__gt=datetime.now(UTC())), + Q(_expiration_datetime__isnull=True) | Q(_expiration_datetime__gt=datetime.now(UTC)), course_id=enrollment.course_id, mode_slug=CourseMode.VERIFIED).exists(): diff --git a/lms/djangoapps/dashboard/tests/test_sysadmin.py b/lms/djangoapps/dashboard/tests/test_sysadmin.py index 5ae1eeba19..655d55ca8d 100644 --- a/lms/djangoapps/dashboard/tests/test_sysadmin.py +++ b/lms/djangoapps/dashboard/tests/test_sysadmin.py @@ -14,7 +14,7 @@ from django.conf import settings from django.core.urlresolvers import reverse from django.test.client import Client from django.test.utils import override_settings -from django.utils.timezone import utc as UTC +from pytz import UTC from nose.plugins.attrib import attr from opaque_keys.edx.keys import CourseKey diff --git a/lms/djangoapps/django_comment_client/tests/test_utils.py b/lms/djangoapps/django_comment_client/tests/test_utils.py index 1d28963684..5f29b23261 100644 --- a/lms/djangoapps/django_comment_client/tests/test_utils.py +++ b/lms/djangoapps/django_comment_client/tests/test_utils.py @@ -7,7 +7,6 @@ import mock from django.core.management import call_command from django.core.urlresolvers import reverse from django.test import RequestFactory, TestCase -from django.utils.timezone import UTC as django_utc from mock import Mock, patch from nose.plugins.attrib import attr from pytz import UTC @@ -539,7 +538,6 @@ class CategoryMapTestCase(CategoryMapTestMixin, ModuleStoreTestCase): ) def test_get_unstarted_discussion_xblocks(self): - later = datetime.datetime(datetime.MAXYEAR, 1, 1, tzinfo=django_utc()) self.create_discussion("Chapter 1", "Discussion 1", start=later) diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index 80d67c27a2..f07af6a0f8 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -3,13 +3,12 @@ import logging from collections import defaultdict from datetime import datetime -import pytz from django.conf import settings from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.db import connection from django.http import HttpResponse -from django.utils.timezone import UTC +from pytz import UTC from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locations import i4xEncoder @@ -231,7 +230,7 @@ def _filter_unstarted_categories(category_map, course): Returns a subset of categories from the provided map which have not yet met the start date Includes information about category children, subcategories (different), and entries """ - now = datetime.now(UTC()) + now = datetime.now(UTC) result_map = {} @@ -344,7 +343,7 @@ def get_discussion_category_map(course, user, divided_only_if_explicit=False, ex sort_key = xblock.sort_key category = " / ".join([x.strip() for x in xblock.discussion_category.split("/")]) # Handle case where xblock.start is None - entry_start_date = xblock.start if xblock.start else datetime.max.replace(tzinfo=pytz.UTC) + entry_start_date = xblock.start if xblock.start else datetime.max.replace(tzinfo=UTC) unexpanded_category_map[category].append({"title": title, "id": discussion_id, "sort_key": sort_key, @@ -411,7 +410,7 @@ def get_discussion_category_map(course, user, divided_only_if_explicit=False, ex category_map['entries'][topic] = { "id": entry["id"], "sort_key": entry.get("sort_key", topic), - "start_date": datetime.now(UTC()), + "start_date": datetime.now(UTC), "is_divided": ( discussion_division_enabled and entry["id"] in divided_discussion_ids ) diff --git a/lms/djangoapps/instructor/tests/test_api.py b/lms/djangoapps/instructor/tests/test_api.py index b3d1a528c4..dc0cd0e922 100644 --- a/lms/djangoapps/instructor/tests/test_api.py +++ b/lms/djangoapps/instructor/tests/test_api.py @@ -11,7 +11,6 @@ import shutil import tempfile import ddt -import pytz from boto.exception import BotoServerError from django.conf import settings from django.contrib.auth.models import User @@ -21,7 +20,7 @@ from django.core.urlresolvers import reverse as django_reverse from django.http import HttpRequest, HttpResponse from django.test import RequestFactory, TestCase from django.test.utils import override_settings -from django.utils.timezone import utc +from pytz import UTC from django.utils.translation import ugettext as _ from mock import Mock, patch from nose.plugins.attrib import attr @@ -4135,7 +4134,7 @@ class TestDueDateExtensions(SharedModuleStoreTestCase, LoginEnrollmentTestCase): def setUpClass(cls): super(TestDueDateExtensions, cls).setUpClass() cls.course = CourseFactory.create() - cls.due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=utc) + cls.due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=UTC) with cls.store.bulk_operations(cls.course.id, emit_signals=False): cls.week1 = ItemFactory.create(due=cls.due) @@ -4217,7 +4216,7 @@ class TestDueDateExtensions(SharedModuleStoreTestCase, LoginEnrollmentTestCase): 'due_datetime': '12/30/2013 00:00' }) self.assertEqual(response.status_code, 200, response.content) - self.assertEqual(datetime.datetime(2013, 12, 30, 0, 0, tzinfo=utc), + self.assertEqual(datetime.datetime(2013, 12, 30, 0, 0, tzinfo=UTC), get_extended_due(self.course, self.week1, self.user1)) def test_change_to_invalid_due_date(self): @@ -4304,7 +4303,7 @@ class TestDueDateExtensionsDeletedDate(ModuleStoreTestCase, LoginEnrollmentTestC super(TestDueDateExtensionsDeletedDate, self).setUp() self.course = CourseFactory.create() - self.due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=utc) + self.due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=UTC) with self.store.bulk_operations(self.course.id, emit_signals=False): self.week1 = ItemFactory.create(due=self.due) @@ -4385,7 +4384,7 @@ class TestDueDateExtensionsDeletedDate(ModuleStoreTestCase, LoginEnrollmentTestC 'due_datetime': '12/30/2013 00:00' }) self.assertEqual(response.status_code, 200, response.content) - self.assertEqual(datetime.datetime(2013, 12, 30, 0, 0, tzinfo=utc), + self.assertEqual(datetime.datetime(2013, 12, 30, 0, 0, tzinfo=UTC), get_extended_due(self.course, self.week1, self.user1)) self.week1.due = None @@ -4949,7 +4948,7 @@ class TestCourseRegistrationCodes(SharedModuleStoreTestCase): coupon = Coupon( code='coupon{0}'.format(i), description='test_description', course_id=self.course.id, percentage_discount='{0}'.format(i), created_by=self.instructor, is_active=True, - expiration_date=datetime.datetime.now(pytz.UTC) + datetime.timedelta(days=2) + expiration_date=datetime.datetime.now(UTC) + datetime.timedelta(days=2) ) coupon.save() diff --git a/lms/djangoapps/instructor/tests/test_tools.py b/lms/djangoapps/instructor/tests/test_tools.py index 07a147788f..0754dd2036 100644 --- a/lms/djangoapps/instructor/tests/test_tools.py +++ b/lms/djangoapps/instructor/tests/test_tools.py @@ -9,7 +9,7 @@ import unittest import mock from django.test import TestCase from django.test.utils import override_settings -from django.utils.timezone import utc +from pytz import UTC from nose.plugins.attrib import attr from opaque_keys.edx.keys import CourseKey @@ -96,7 +96,7 @@ class TestParseDatetime(unittest.TestCase): def test_parse_no_error(self): self.assertEqual( tools.parse_datetime('5/12/2010 2:42'), - datetime.datetime(2010, 5, 12, 2, 42, tzinfo=utc)) + datetime.datetime(2010, 5, 12, 2, 42, tzinfo=UTC)) def test_parse_error(self): with self.assertRaises(tools.DashboardError): @@ -144,7 +144,7 @@ class TestGetUnitsWithDueDate(ModuleStoreTestCase): """ super(TestGetUnitsWithDueDate, self).setUp() - due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=utc) + due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=UTC) course = CourseFactory.create() week1 = ItemFactory.create(due=due, parent=course) week2 = ItemFactory.create(due=due, parent=course) @@ -199,7 +199,7 @@ class TestSetDueDateExtension(ModuleStoreTestCase): """ super(TestSetDueDateExtension, self).setUp() - self.due = due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=utc) + self.due = due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=UTC) course = CourseFactory.create() week1 = ItemFactory.create(due=due, parent=course) week2 = ItemFactory.create(due=due, parent=course) @@ -234,7 +234,7 @@ class TestSetDueDateExtension(ModuleStoreTestCase): block.fields['due']._del_cached_value(block) # pylint: disable=protected-access def test_set_due_date_extension(self): - extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=utc) + extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=UTC) tools.set_due_date_extension(self.course, self.week1, self.user, extended) self._clear_field_data_cache() self.assertEqual(self.week1.due, extended) @@ -242,23 +242,23 @@ class TestSetDueDateExtension(ModuleStoreTestCase): self.assertEqual(self.assignment.due, extended) def test_set_due_date_extension_num_queries(self): - extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=utc) + extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=UTC) with self.assertNumQueries(5): tools.set_due_date_extension(self.course, self.week1, self.user, extended) self._clear_field_data_cache() def test_set_due_date_extension_invalid_date(self): - extended = datetime.datetime(2009, 1, 1, 0, 0, tzinfo=utc) + extended = datetime.datetime(2009, 1, 1, 0, 0, tzinfo=UTC) with self.assertRaises(tools.DashboardError): tools.set_due_date_extension(self.course, self.week1, self.user, extended) def test_set_due_date_extension_no_date(self): - extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=utc) + extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=UTC) with self.assertRaises(tools.DashboardError): tools.set_due_date_extension(self.course, self.week3, self.user, extended) def test_reset_due_date_extension(self): - extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=utc) + extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=UTC) tools.set_due_date_extension(self.course, self.week1, self.user, extended) tools.set_due_date_extension(self.course, self.week1, self.user, None) self.assertEqual(self.week1.due, self.due) @@ -276,7 +276,7 @@ class TestDataDumps(ModuleStoreTestCase): """ super(TestDataDumps, self).setUp() - due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=utc) + due = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=UTC) course = CourseFactory.create() week1 = ItemFactory.create(due=due, parent=course) week2 = ItemFactory.create(due=due, parent=course) @@ -296,7 +296,7 @@ class TestDataDumps(ModuleStoreTestCase): self.user2 = user2 def test_dump_module_extensions(self): - extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=utc) + extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=UTC) tools.set_due_date_extension(self.course, self.week1, self.user1, extended) tools.set_due_date_extension(self.course, self.week1, self.user2, @@ -316,7 +316,7 @@ class TestDataDumps(ModuleStoreTestCase): "Extended Due Date": "2013-12-25 00:00"}]) def test_dump_student_extensions(self): - extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=utc) + extended = datetime.datetime(2013, 12, 25, 0, 0, tzinfo=UTC) tools.set_due_date_extension(self.course, self.week1, self.user1, extended) tools.set_due_date_extension(self.course, self.week2, self.user1, diff --git a/lms/djangoapps/instructor/tests/utils.py b/lms/djangoapps/instructor/tests/utils.py index 8c1308ebb3..84c34e1cf2 100644 --- a/lms/djangoapps/instructor/tests/utils.py +++ b/lms/djangoapps/instructor/tests/utils.py @@ -5,7 +5,7 @@ import datetime import json import random -from django.utils.timezone import utc +from pytz import UTC from util.date_utils import get_default_time_display @@ -62,7 +62,7 @@ class FakeEmail(FakeInfo): day = random.randint(1, 28) hour = random.randint(0, 23) minute = random.randint(0, 59) - self.created = datetime.datetime(year, month, day, hour, minute, tzinfo=utc) + self.created = datetime.datetime(year, month, day, hour, minute, tzinfo=UTC) self.targets = FakeTargetGroup() diff --git a/lms/djangoapps/instructor/views/tools.py b/lms/djangoapps/instructor/views/tools.py index 3e70b2168d..25f843e007 100644 --- a/lms/djangoapps/instructor/views/tools.py +++ b/lms/djangoapps/instructor/views/tools.py @@ -6,7 +6,7 @@ import json import dateutil from django.contrib.auth.models import User from django.http import HttpResponseBadRequest -from django.utils.timezone import utc +from pytz import UTC from django.utils.translation import ugettext as _ from opaque_keys.edx.keys import UsageKey @@ -91,7 +91,7 @@ def parse_datetime(datestr): UTC. """ try: - return dateutil.parser.parse(datestr).replace(tzinfo=utc) + return dateutil.parser.parse(datestr).replace(tzinfo=UTC) except ValueError: raise DashboardError(_("Unable to parse date: ") + datestr) diff --git a/openedx/core/djangoapps/models/tests/test_course_details.py b/openedx/core/djangoapps/models/tests/test_course_details.py index 74ad8eb8b2..4653bb1b38 100644 --- a/openedx/core/djangoapps/models/tests/test_course_details.py +++ b/openedx/core/djangoapps/models/tests/test_course_details.py @@ -4,7 +4,7 @@ Tests for CourseDetails import datetime import ddt -from django.utils.timezone import UTC +from pytz import UTC from nose.plugins.attrib import attr from xmodule.modulestore import ModuleStoreEnum @@ -84,17 +84,17 @@ class CourseDetailsTestCase(ModuleStoreTestCase): CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).self_paced, jsondetails.self_paced ) - jsondetails.start_date = datetime.datetime(2010, 10, 1, 0, tzinfo=UTC()) + jsondetails.start_date = datetime.datetime(2010, 10, 1, 0, tzinfo=UTC) self.assertEqual( CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).start_date, jsondetails.start_date ) - jsondetails.end_date = datetime.datetime(2011, 10, 1, 0, tzinfo=UTC()) + jsondetails.end_date = datetime.datetime(2011, 10, 1, 0, tzinfo=UTC) self.assertEqual( CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).end_date, jsondetails.end_date ) - jsondetails.certificate_available_date = datetime.datetime(2010, 10, 1, 0, tzinfo=UTC()) + jsondetails.certificate_available_date = datetime.datetime(2010, 10, 1, 0, tzinfo=UTC) self.assertEqual( CourseDetails.update_from_json( self.course.id, jsondetails.__dict__, self.user diff --git a/openedx/core/djangoapps/profile_images/views.py b/openedx/core/djangoapps/profile_images/views.py index d4aa052b66..43bb324022 100644 --- a/openedx/core/djangoapps/profile_images/views.py +++ b/openedx/core/djangoapps/profile_images/views.py @@ -6,7 +6,7 @@ import itertools import logging from contextlib import closing -from django.utils.timezone import utc +from pytz import UTC from django.utils.translation import ugettext as _ from rest_framework import permissions, status from rest_framework.parsers import FormParser, MultiPartParser @@ -37,7 +37,7 @@ def _make_upload_dt(): Generate a server-side timestamp for the upload. This is in a separate function so its behavior can be overridden in tests. """ - return datetime.datetime.utcnow().replace(tzinfo=utc) + return datetime.datetime.utcnow().replace(tzinfo=UTC) class ProfileImageView(DeveloperErrorViewMixin, APIView): diff --git a/openedx/core/lib/xblock_utils/__init__.py b/openedx/core/lib/xblock_utils/__init__.py index fa8603d7c7..a396fb4224 100644 --- a/openedx/core/lib/xblock_utils/__init__.py +++ b/openedx/core/lib/xblock_utils/__init__.py @@ -15,7 +15,7 @@ from contracts import contract from django.conf import settings from django.contrib.staticfiles.storage import staticfiles_storage from django.core.urlresolvers import reverse -from django.utils.timezone import UTC +from pytz import UTC from django.utils.html import escape from django.contrib.auth.models import User from edxmako.shortcuts import render_to_string @@ -350,7 +350,7 @@ def add_staff_markup(user, has_instructor_access, disable_staff_debug_info, bloc # Useful to indicate to staff if problem has been released or not. # TODO (ichuang): use _has_access_descriptor.can_load in lms.courseware.access, # instead of now>mstart comparison here. - now = datetime.datetime.now(UTC()) + now = datetime.datetime.now(UTC) is_released = "unknown" mstart = block.start diff --git a/openedx/features/course_experience/views/course_home_messages.py b/openedx/features/course_experience/views/course_home_messages.py index 3f35c8e68f..aea1864aaf 100644 --- a/openedx/features/course_experience/views/course_home_messages.py +++ b/openedx/features/course_experience/views/course_home_messages.py @@ -9,7 +9,8 @@ from django.conf import settings from django.contrib import auth from django.template.loader import render_to_string from django.utils.http import urlquote_plus -from django.utils.timezone import UTC +from pytz import UTC +from django.utils.translation import get_language, to_locale from django.utils.translation import ugettext as _ from django.utils.translation import get_language, to_locale from opaque_keys.edx.keys import CourseKey @@ -54,7 +55,7 @@ class CourseHomeMessageFragmentView(EdxFragmentView): course = get_course_with_access(request.user, 'load', course_key) # Get time until the start date, if already started, or no start date, value will be zero or negative - now = datetime.now(UTC()) + now = datetime.now(UTC) already_started = course.start and now > course.start days_until_start_string = "started" if already_started else format_timedelta(course.start - now, locale=to_locale(get_language())) course_start_data = {