From b06fcd27d2874e5cf7fd175cfcecf20710badd8c Mon Sep 17 00:00:00 2001 From: Ayub khan Date: Tue, 9 Jul 2019 14:30:27 +0500 Subject: [PATCH] INCR-334 python3 compatibility --- .../commands/tests/test_cleanup_assets.py | 14 +++---- .../commands/tests/test_create_course.py | 8 +++- .../commands/tests/test_delete_course.py | 7 +++- .../commands/tests/test_export_all_courses.py | 7 +++- .../commands/tests/test_fix_not_found.py | 8 +++- .../management/commands/tests/test_import.py | 5 ++- .../tests/test_migrate_transcripts.py | 37 ++++++++++--------- .../commands/tests/test_reindex_courses.py | 9 +++-- .../contentstore/views/transcript_settings.py | 21 ++++++----- 9 files changed, 70 insertions(+), 46 deletions(-) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_cleanup_assets.py b/cms/djangoapps/contentstore/management/commands/tests/test_cleanup_assets.py index 0930c6571d..b48d3e507e 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_cleanup_assets.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_cleanup_assets.py @@ -2,20 +2,20 @@ Test for assets cleanup of courses for Mac OS metadata files (with filename ".DS_Store" or with filename which starts with "._") """ -from django.core.management import call_command +from __future__ import absolute_import +from django.conf import settings +from django.core.management import call_command from opaque_keys.edx.keys import CourseKey + from xmodule.contentstore.content import XASSET_LOCATION_TAG from xmodule.contentstore.django import contentstore -from xmodule.modulestore.django import modulestore from xmodule.modulestore import ModuleStoreEnum +from xmodule.modulestore.django import modulestore from xmodule.modulestore.mongo.base import location_to_query from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase -from xmodule.modulestore.tests.utils import ( - add_temp_files_from_dict, remove_temp_files_from_list, DOT_FILES_DICT -) +from xmodule.modulestore.tests.utils import DOT_FILES_DICT, add_temp_files_from_dict, remove_temp_files_from_list from xmodule.modulestore.xml_importer import import_course_from_xml -from django.conf import settings TEST_DATA_DIR = settings.COMMON_TEST_DATA_ROOT @@ -32,7 +32,7 @@ class ExportAllCourses(ModuleStoreTestCase): self.content_store = contentstore() # pylint: disable=protected-access self.module_store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.mongo) - self.addCleanup(remove_temp_files_from_list, DOT_FILES_DICT.keys(), self.course_dir / "static") + self.addCleanup(remove_temp_files_from_list, list(DOT_FILES_DICT.keys()), self.course_dir / "static") add_temp_files_from_dict(DOT_FILES_DICT, self.course_dir / "static") def test_export_all_courses(self): diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_create_course.py b/cms/djangoapps/contentstore/management/commands/tests/test_create_course.py index d235880691..92a61c8ebf 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_create_course.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_create_course.py @@ -1,14 +1,18 @@ """ Unittests for creating a course in an chosen modulestore """ +from __future__ import absolute_import + from StringIO import StringIO + import ddt +import six from django.core.management import CommandError, call_command from django.test import TestCase from xmodule.modulestore import ModuleStoreEnum -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.django import modulestore +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase class TestArgParsing(TestCase): @@ -108,7 +112,7 @@ class TestCreateCourse(ModuleStoreTestCase): ) course = self.store.get_course(lowercase_course_id) self.assertIsNotNone(course, 'Course not found using lowercase course key.') - self.assertEqual(unicode(course.id), unicode(lowercase_course_id)) + self.assertEqual(six.text_type(course.id), six.text_type(lowercase_course_id)) # Verify store does not return course with different case. uppercase_course_id = self.store.make_course_key(org.upper(), number.upper(), run.upper()) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_delete_course.py b/cms/djangoapps/contentstore/management/commands/tests/test_delete_course.py index dc72d78e36..8d0388aa14 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_delete_course.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_delete_course.py @@ -1,3 +1,8 @@ +""" +Delete course tests. +""" +from __future__ import absolute_import + import mock from django.core.management import CommandError, call_command @@ -6,7 +11,7 @@ from student.tests.factories import UserFactory from xmodule.contentstore.content import StaticContent from xmodule.contentstore.django import contentstore from xmodule.modulestore.django import modulestore -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase, TEST_DATA_SPLIT_MODULESTORE +from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_export_all_courses.py b/cms/djangoapps/contentstore/management/commands/tests/test_export_all_courses.py index 5013bce8cd..6d843eb6c8 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_export_all_courses.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_export_all_courses.py @@ -1,11 +1,14 @@ """ Test for export all courses. """ +from __future__ import absolute_import + import shutil from tempfile import mkdtemp -from contentstore.management.commands.export_all_courses import export_courses_to_output_path +import six +from contentstore.management.commands.export_all_courses import export_courses_to_output_path from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase @@ -47,4 +50,4 @@ class ExportAllCourses(ModuleStoreTestCase): courses, failed_export_courses = export_courses_to_output_path(self.temp_dir) self.assertEqual(len(courses), 2) self.assertEqual(len(failed_export_courses), 1) - self.assertEqual(failed_export_courses[0], unicode(second_course_id)) + self.assertEqual(failed_export_courses[0], six.text_type(second_course_id)) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_fix_not_found.py b/cms/djangoapps/contentstore/management/commands/tests/test_fix_not_found.py index dac5ad0393..5a0894b678 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_fix_not_found.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_fix_not_found.py @@ -2,7 +2,11 @@ Tests for the fix_not_found management command """ +from __future__ import absolute_import + +import six from django.core.management import CommandError, call_command + from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory @@ -25,7 +29,7 @@ class TestFixNotFound(ModuleStoreTestCase): """ course = CourseFactory.create(default_store=ModuleStoreEnum.Type.mongo) with self.assertRaisesRegexp(CommandError, "The owning modulestore does not support this command."): - call_command("fix_not_found", unicode(course.id)) + call_command("fix_not_found", six.text_type(course.id)) def test_fix_not_found(self): course = CourseFactory.create(default_store=ModuleStoreEnum.Type.split) @@ -45,7 +49,7 @@ class TestFixNotFound(ModuleStoreTestCase): self.assertEqual(len(course.children), 2) self.assertIn(dangling_pointer, course.children) - call_command("fix_not_found", unicode(course.id)) + call_command("fix_not_found", six.text_type(course.id)) # make sure the dangling pointer was removed from # the course block's children diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_import.py b/cms/djangoapps/contentstore/management/commands/tests/test_import.py index c5771171f8..9c19a5474c 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_import.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_import.py @@ -2,10 +2,13 @@ Unittests for importing a course via management command """ +from __future__ import absolute_import + import os import shutil import tempfile +import six from django.core.management import call_command from path import Path as path @@ -91,4 +94,4 @@ class TestImport(ModuleStoreTestCase): course = modulestore().get_course(self.base_course_key) # With the bug, this fails because the chapter's course_key is the split mongo form, # while the course's course_key is the old mongo form. - self.assertEqual(unicode(course.location.course_key), unicode(course.children[0].course_key)) + self.assertEqual(six.text_type(course.location.course_key), six.text_type(course.children[0].course_key)) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_migrate_transcripts.py b/cms/djangoapps/contentstore/management/commands/tests/test_migrate_transcripts.py index 113fc1fa81..fb701fd815 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_migrate_transcripts.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_migrate_transcripts.py @@ -2,26 +2,27 @@ """ Tests for course transcript migration management command. """ +from __future__ import absolute_import + import itertools import logging from datetime import datetime -import pytz import ddt +import pytz +import six +from django.core.management import CommandError, call_command from django.test import TestCase -from django.core.management import call_command, CommandError +from edxval import api as api from mock import patch +from testfixtures import LogCapture -from openedx.core.djangoapps.video_config.models import ( - TranscriptMigrationSetting, MigrationEnqueuedCourse -) +from openedx.core.djangoapps.video_config.models import MigrationEnqueuedCourse, TranscriptMigrationSetting from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory from xmodule.video_module import VideoBlock from xmodule.video_module.transcripts_utils import save_to_store -from edxval import api as api -from testfixtures import LogCapture LOGGER_NAME = "cms.djangoapps.contentstore.tasks" @@ -94,7 +95,7 @@ class TestMigrateTranscripts(ModuleStoreTestCase): 'client_video_id': 'test1.mp4', 'duration': 42.0, 'status': 'upload', - 'courses': [unicode(self.course.id)], + 'courses': [six.text_type(self.course.id)], 'encoded_videos': [], 'created': datetime.now(pytz.utc) } @@ -155,7 +156,7 @@ class TestMigrateTranscripts(ModuleStoreTestCase): self.assertFalse(api.is_transcript_available(self.video_descriptor.edx_video_id, 'ge')) # now call migrate_transcripts command and check the transcript availability - call_command('migrate_transcripts', '--course-id', unicode(self.course.id), '--commit') + call_command('migrate_transcripts', '--course-id', six.text_type(self.course.id), '--commit') languages = api.get_available_transcript_languages(self.video_descriptor.edx_video_id) self.assertEqual(len(languages), 2) @@ -173,7 +174,7 @@ class TestMigrateTranscripts(ModuleStoreTestCase): self.assertFalse(api.is_transcript_available(self.video_descriptor.edx_video_id, 'ge')) # now call migrate_transcripts command and check the transcript availability - call_command('migrate_transcripts', '--course-id', unicode(self.course.id)) + call_command('migrate_transcripts', '--course-id', six.text_type(self.course.id)) # check that transcripts still do not exist languages = api.get_available_transcript_languages(self.video_descriptor.edx_video_id) @@ -191,7 +192,7 @@ class TestMigrateTranscripts(ModuleStoreTestCase): self.assertFalse(api.is_transcript_available(self.video_descriptor.edx_video_id, 'ge')) # now call migrate_transcripts command and check the transcript availability - call_command('migrate_transcripts', '--course-id', unicode(self.course.id), '--commit') + call_command('migrate_transcripts', '--course-id', six.text_type(self.course.id), '--commit') self.assertTrue(api.is_transcript_available(self.video_descriptor.edx_video_id, 'hr')) self.assertTrue(api.is_transcript_available(self.video_descriptor.edx_video_id, 'ge')) @@ -206,19 +207,19 @@ class TestMigrateTranscripts(ModuleStoreTestCase): self.assertFalse(api.is_transcript_available(self.video_descriptor.edx_video_id, 'ge')) # now call migrate_transcripts command and check the transcript availability - call_command('migrate_transcripts', '--course-id', unicode(self.course.id), '--commit') + call_command('migrate_transcripts', '--course-id', six.text_type(self.course.id), '--commit') self.assertTrue(api.is_transcript_available(self.video_descriptor.edx_video_id, 'hr')) self.assertTrue(api.is_transcript_available(self.video_descriptor.edx_video_id, 'ge')) # now call migrate_transcripts command again and check the transcript availability - call_command('migrate_transcripts', '--course-id', unicode(self.course.id), '--commit') + call_command('migrate_transcripts', '--course-id', six.text_type(self.course.id), '--commit') self.assertTrue(api.is_transcript_available(self.video_descriptor.edx_video_id, 'hr')) self.assertTrue(api.is_transcript_available(self.video_descriptor.edx_video_id, 'ge')) # now call migrate_transcripts command with --force-update and check the transcript availability - call_command('migrate_transcripts', '--course-id', unicode(self.course.id), '--force-update', '--commit') + call_command('migrate_transcripts', '--course-id', six.text_type(self.course.id), '--force-update', '--commit') self.assertTrue(api.is_transcript_available(self.video_descriptor.edx_video_id, 'hr')) self.assertTrue(api.is_transcript_available(self.video_descriptor.edx_video_id, 'ge')) @@ -227,7 +228,7 @@ class TestMigrateTranscripts(ModuleStoreTestCase): """ Test migrate transcripts logging and output """ - course_id = unicode(self.course.id) + course_id = six.text_type(self.course.id) expected_log = ( ( 'cms.djangoapps.contentstore.tasks', 'INFO', @@ -255,7 +256,7 @@ class TestMigrateTranscripts(ModuleStoreTestCase): ) with LogCapture(LOGGER_NAME, level=logging.INFO) as logger: - call_command('migrate_transcripts', '--course-id', unicode(self.course.id)) + call_command('migrate_transcripts', '--course-id', six.text_type(self.course.id)) logger.check( *expected_log ) @@ -264,7 +265,7 @@ class TestMigrateTranscripts(ModuleStoreTestCase): """ Test migrate transcripts exception logging """ - course_id = unicode(self.course_2.id) + course_id = six.text_type(self.course_2.id) expected_log = ( ( 'cms.djangoapps.contentstore.tasks', 'INFO', @@ -292,7 +293,7 @@ class TestMigrateTranscripts(ModuleStoreTestCase): ) with LogCapture(LOGGER_NAME, level=logging.INFO) as logger: - call_command('migrate_transcripts', '--course-id', unicode(self.course_2.id), '--commit') + call_command('migrate_transcripts', '--course-id', six.text_type(self.course_2.id), '--commit') logger.check( *expected_log ) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_reindex_courses.py b/cms/djangoapps/contentstore/management/commands/tests/test_reindex_courses.py index ebcc04fe78..35d8725116 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_reindex_courses.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_reindex_courses.py @@ -1,17 +1,18 @@ """ Tests for course reindex command """ +from __future__ import absolute_import + import ddt -from django.core.management import call_command, CommandError import mock +from django.core.management import CommandError, call_command from six import text_type +from contentstore.courseware_index import SearchIndexingError +from contentstore.management.commands.reindex_course import Command as ReindexCommand from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, LibraryFactory -from contentstore.management.commands.reindex_course import Command as ReindexCommand -from contentstore.courseware_index import SearchIndexingError - @ddt.ddt class TestReindexCourse(ModuleStoreTestCase): diff --git a/cms/djangoapps/contentstore/views/transcript_settings.py b/cms/djangoapps/contentstore/views/transcript_settings.py index 4324663956..14db7a2d95 100644 --- a/cms/djangoapps/contentstore/views/transcript_settings.py +++ b/cms/djangoapps/contentstore/views/transcript_settings.py @@ -1,30 +1,31 @@ """ Views related to the transcript preferences feature """ -import os +from __future__ import absolute_import + import logging +import os from django.contrib.auth.decorators import login_required from django.core.files.base import ContentFile -from django.http import HttpResponseNotFound, HttpResponse +from django.http import HttpResponse, HttpResponseNotFound from django.utils.translation import ugettext as _ -from django.views.decorators.http import require_http_methods, require_POST, require_GET +from django.views.decorators.http import require_GET, require_http_methods, require_POST from edxval.api import ( create_or_update_video_transcript, delete_video_transcript, - get_available_transcript_languages, get_3rd_party_transcription_plans, + get_available_transcript_languages, get_video_transcript_data, - update_transcript_credentials_state_for_org, + update_transcript_credentials_state_for_org ) from opaque_keys.edx.keys import CourseKey +from contentstore.views.videos import TranscriptProvider from openedx.core.djangoapps.video_config.models import VideoTranscriptEnabledFlag from openedx.core.djangoapps.video_pipeline.api import update_3rd_party_transcription_service_credentials from student.auth import has_studio_write_access from util.json_request import JsonResponse, expect_json - -from contentstore.views.videos import TranscriptProvider from xmodule.video_module.transcripts_utils import Transcript, TranscriptsGenerationException __all__ = [ @@ -58,7 +59,7 @@ def validate_transcript_credentials(provider, **credentials): only returns the validated ones. """ error_message, validated_credentials = '', {} - valid_providers = get_3rd_party_transcription_plans().keys() + valid_providers = list(get_3rd_party_transcription_plans().keys()) if provider in valid_providers: must_have_props = [] if provider == TranscriptProvider.THREE_PLAY_MEDIA: @@ -66,7 +67,9 @@ def validate_transcript_credentials(provider, **credentials): elif provider == TranscriptProvider.CIELO24: must_have_props = ['api_key', 'username'] - missing = [must_have_prop for must_have_prop in must_have_props if must_have_prop not in credentials.keys()] + missing = [ + must_have_prop for must_have_prop in must_have_props if must_have_prop not in list(credentials.keys()) + ] if missing: error_message = u'{missing} must be specified.'.format(missing=' and '.join(missing)) return error_message, validated_credentials