From 83a742966d8fb6d662b57f7b548521faf3f36dc7 Mon Sep 17 00:00:00 2001 From: arbisoft Date: Tue, 9 Jul 2019 18:37:55 +0500 Subject: [PATCH] Fixing python-modernize issues. --- .../contentstore/git_export_utils.py | 7 ++++-- cms/djangoapps/contentstore/video_utils.py | 16 ++++++++------ .../models/settings/course_grading.py | 19 ++++++++++------ .../models/settings/course_metadata.py | 22 ++++++++++--------- cms/djangoapps/models/settings/encoder.py | 2 ++ .../xblock_config/migrations/0001_initial.py | 2 +- .../0002_courseeditltifieldsenabledflag.py | 2 +- 7 files changed, 42 insertions(+), 28 deletions(-) diff --git a/cms/djangoapps/contentstore/git_export_utils.py b/cms/djangoapps/contentstore/git_export_utils.py index 086f9d9f34..ed3922aca8 100644 --- a/cms/djangoapps/contentstore/git_export_utils.py +++ b/cms/djangoapps/contentstore/git_export_utils.py @@ -3,15 +3,18 @@ Utilities for export a course's XML into a git repository, committing and pushing the changes. """ +from __future__ import absolute_import + import logging import os import subprocess -from urlparse import urlparse +import six from django.conf import settings from django.contrib.auth.models import User from django.utils import timezone from django.utils.translation import ugettext_lazy as _ +from six.moves.urllib.parse import urlparse # pylint: disable=import-error from xmodule.contentstore.django import contentstore from xmodule.modulestore.django import modulestore @@ -32,7 +35,7 @@ class GitExportError(Exception): def __init__(self, message): # Force the lazy i18n values to turn into actual unicode objects - super(GitExportError, self).__init__(unicode(message)) + super(GitExportError, self).__init__(six.text_type(message)) NO_EXPORT_DIR = _(u"GIT_REPO_EXPORT_DIR not set or path {0} doesn't exist, " "please create it, or configure a different path with " diff --git a/cms/djangoapps/contentstore/video_utils.py b/cms/djangoapps/contentstore/video_utils.py index df64c5c281..39260b1385 100644 --- a/cms/djangoapps/contentstore/video_utils.py +++ b/cms/djangoapps/contentstore/video_utils.py @@ -2,16 +2,18 @@ """ Utils related to the videos. """ -import logging -from urlparse import urljoin -import requests +from __future__ import absolute_import +import logging + +import requests +import six from django.conf import settings from django.core.files.images import get_image_dimensions from django.core.files.uploadedfile import SimpleUploadedFile from django.utils.translation import ugettext as _ from edxval.api import get_course_video_image_url, update_video_image - +from six.moves.urllib.parse import urljoin # pylint: disable=import-error # Youtube thumbnail sizes. # https://img.youtube.com/vi/{youtube_id}/{thumbnail_quality}.jpg @@ -40,9 +42,9 @@ def validate_video_image(image_file, skip_aspect_ratio=False): if not all(hasattr(image_file, attr) for attr in ['name', 'content_type', 'size']): error = _('The image must have name, content type, and size information.') - elif image_file.content_type not in settings.VIDEO_IMAGE_SUPPORTED_FILE_FORMATS.values(): + elif image_file.content_type not in list(settings.VIDEO_IMAGE_SUPPORTED_FILE_FORMATS.values()): error = _(u'This image file type is not supported. Supported file types are {supported_file_formats}.').format( - supported_file_formats=settings.VIDEO_IMAGE_SUPPORTED_FILE_FORMATS.keys() + supported_file_formats=list(settings.VIDEO_IMAGE_SUPPORTED_FILE_FORMATS.keys()) ) elif image_file.size > settings.VIDEO_IMAGE_SETTINGS['VIDEO_IMAGE_MAX_BYTES']: error = _(u'This image file must be smaller than {image_max_size}.').format( @@ -126,7 +128,7 @@ def scrape_youtube_thumbnail(course_id, edx_video_id, youtube_id): # Scrape when course video image does not exist for edx_video_id. if not get_course_video_image_url(course_id, edx_video_id): thumbnail_content, thumbnail_content_type = download_youtube_video_thumbnail(youtube_id) - supported_content_types = {v: k for k, v in settings.VIDEO_IMAGE_SUPPORTED_FILE_FORMATS.iteritems()} + supported_content_types = {v: k for k, v in six.iteritems(settings.VIDEO_IMAGE_SUPPORTED_FILE_FORMATS)} image_filename = '{youtube_id}{image_extention}'.format( youtube_id=youtube_id, image_extention=supported_content_types.get( diff --git a/cms/djangoapps/models/settings/course_grading.py b/cms/djangoapps/models/settings/course_grading.py index f6f4b2c361..d66460b7d3 100644 --- a/cms/djangoapps/models/settings/course_grading.py +++ b/cms/djangoapps/models/settings/course_grading.py @@ -1,10 +1,15 @@ +"""Grading policy""" +from __future__ import absolute_import + +import json from base64 import b64encode from datetime import timedelta from hashlib import sha1 -import json + +import six +from eventtracking import tracker from contentstore.signals.signals import GRADING_POLICY_CHANGED -from eventtracking import tracker from track.event_transaction_utils import create_new_event_transaction_id from xmodule.modulestore.django import modulestore @@ -183,7 +188,7 @@ class CourseGradingModel(object): descriptor = modulestore().get_item(location) return { "graderType": descriptor.format if descriptor.format is not None else 'notgraded', - "location": unicode(location), + "location": six.text_type(location), } @staticmethod @@ -256,12 +261,12 @@ class CourseGradingModel(object): def _grading_event_and_signal(course_key, user_id): name = GRADING_POLICY_CHANGED_EVENT_TYPE course = modulestore().get_course(course_key) - grading_policy_hash = unicode(hash_grading_policy(course.grading_policy)) + grading_policy_hash = six.text_type(hash_grading_policy(course.grading_policy)) data = { - "course_id": unicode(course_key), - "user_id": unicode(user_id), + "course_id": six.text_type(course_key), + "user_id": six.text_type(user_id), "grading_policy_hash": grading_policy_hash, - "event_transaction_id": unicode(create_new_event_transaction_id()), + "event_transaction_id": six.text_type(create_new_event_transaction_id()), "event_transaction_type": GRADING_POLICY_CHANGED_EVENT_TYPE, } tracker.emit(name, data) diff --git a/cms/djangoapps/models/settings/course_metadata.py b/cms/djangoapps/models/settings/course_metadata.py index 5c77a702f6..239817ff47 100644 --- a/cms/djangoapps/models/settings/course_metadata.py +++ b/cms/djangoapps/models/settings/course_metadata.py @@ -1,18 +1,20 @@ """ Django module for Course Metadata class -- manages advanced settings and related parameters """ +from __future__ import absolute_import + +import six +from crum import get_current_user from django.conf import settings from django.utils.translation import ugettext as _ from six import text_type from xblock.fields import Scope -from crum import get_current_user +from cms.djangoapps.contentstore.config.waffle import ENABLE_PROCTORING_PROVIDER_OVERRIDES +from openedx.features.course_experience import COURSE_ENABLE_UNENROLLED_ACCESS_FLAG +from student.roles import GlobalStaff from xblock_django.models import XBlockStudioConfigurationFlag from xmodule.modulestore.django import modulestore -from student.roles import GlobalStaff - -from openedx.features.course_experience import COURSE_ENABLE_UNENROLLED_ACCESS_FLAG -from cms.djangoapps.contentstore.config.waffle import ENABLE_PROCTORING_PROVIDER_OVERRIDES class CourseMetadata(object): @@ -151,7 +153,7 @@ class CourseMetadata(object): metadata = cls.fetch_all(descriptor) black_list_of_fields = cls.get_blacklist_of_fields(descriptor.id) - for key, value in metadata.iteritems(): + for key, value in six.iteritems(metadata): if key in black_list_of_fields: continue result[key] = value @@ -195,7 +197,7 @@ class CourseMetadata(object): # Validate the values before actually setting them. key_values = {} - for key, model in jsondict.iteritems(): + for key, model in six.iteritems(jsondict): # should it be an error if one of the filtered list items is in the payload? if key in blacklist_of_fields: continue @@ -228,13 +230,13 @@ class CourseMetadata(object): if not filter_tabs: blacklist_of_fields.remove("tabs") - filtered_dict = dict((k, v) for k, v in jsondict.iteritems() if k not in blacklist_of_fields) + filtered_dict = dict((k, v) for k, v in six.iteritems(jsondict) if k not in blacklist_of_fields) did_validate = True errors = [] key_values = {} updated_data = None - for key, model in filtered_dict.iteritems(): + for key, model in six.iteritems(filtered_dict): try: val = model['value'] if hasattr(descriptor, key) and getattr(descriptor, key) != val: @@ -254,7 +256,7 @@ class CourseMetadata(object): """ Update metadata descriptor from key_values. Saves to modulestore if save is true. """ - for key, value in key_values.iteritems(): + for key, value in six.iteritems(key_values): setattr(descriptor, key, value) if save and key_values: diff --git a/cms/djangoapps/models/settings/encoder.py b/cms/djangoapps/models/settings/encoder.py index c276998322..fcd58cbc95 100644 --- a/cms/djangoapps/models/settings/encoder.py +++ b/cms/djangoapps/models/settings/encoder.py @@ -1,6 +1,8 @@ """ CourseSettingsEncoder """ +from __future__ import absolute_import + import datetime import json from json.encoder import JSONEncoder diff --git a/cms/djangoapps/xblock_config/migrations/0001_initial.py b/cms/djangoapps/xblock_config/migrations/0001_initial.py index ee8de3c6b3..18c07ea98a 100644 --- a/cms/djangoapps/xblock_config/migrations/0001_initial.py +++ b/cms/djangoapps/xblock_config/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals import django.db.models.deletion from django.conf import settings diff --git a/cms/djangoapps/xblock_config/migrations/0002_courseeditltifieldsenabledflag.py b/cms/djangoapps/xblock_config/migrations/0002_courseeditltifieldsenabledflag.py index daf16e6c9a..a21f9dba10 100644 --- a/cms/djangoapps/xblock_config/migrations/0002_courseeditltifieldsenabledflag.py +++ b/cms/djangoapps/xblock_config/migrations/0002_courseeditltifieldsenabledflag.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals import django.db.models.deletion from django.conf import settings