From af2e7705b1564bf267f89645980d1ebca84564cf Mon Sep 17 00:00:00 2001 From: Ayub khan Date: Fri, 12 Jul 2019 14:05:06 +0500 Subject: [PATCH] INCR-434 python3 compatibility --- lms/djangoapps/courseware/__init__.py | 2 ++ lms/djangoapps/courseware/access.py | 26 ++++++++++++------- lms/djangoapps/courseware/access_response.py | 2 ++ lms/djangoapps/courseware/access_utils.py | 7 +++-- .../courseware/courseware_access_exception.py | 2 ++ lms/djangoapps/courseware/field_overrides.py | 14 +++++----- lms/djangoapps/courseware/masquerade.py | 4 ++- lms/djangoapps/courseware/tabs.py | 10 ++++--- lms/djangoapps/courseware/url_helpers.py | 14 +++++----- 9 files changed, 51 insertions(+), 30 deletions(-) diff --git a/lms/djangoapps/courseware/__init__.py b/lms/djangoapps/courseware/__init__.py index 07fabffcdb..94d44daee7 100644 --- a/lms/djangoapps/courseware/__init__.py +++ b/lms/djangoapps/courseware/__init__.py @@ -1,4 +1,6 @@ #pylint: disable=missing-docstring +from __future__ import absolute_import + import warnings if __name__ == 'courseware': diff --git a/lms/djangoapps/courseware/access.py b/lms/djangoapps/courseware/access.py index ae351b2ebd..a34b86df65 100644 --- a/lms/djangoapps/courseware/access.py +++ b/lms/djangoapps/courseware/access.py @@ -10,33 +10,34 @@ Note: The access control logic in this file does NOT check for enrollment in If enrollment is to be checked, use get_course_with_access in courseware.courses. It is a wrapper around has_access that additionally checks for enrollment. """ +from __future__ import absolute_import + import logging from datetime import datetime +import six from django.conf import settings from django.contrib.auth.models import AnonymousUser -from pytz import UTC from opaque_keys.edx.keys import CourseKey, UsageKey +from pytz import UTC from six import text_type from xblock.core import XBlock from courseware.access_response import ( + IncorrectPartitionGroupError, MilestoneAccessError, MobileAvailabilityError, - VisibilityError, + NoAllowedPartitionGroupsError, + VisibilityError ) from courseware.access_utils import ( ACCESS_DENIED, ACCESS_GRANTED, adjust_start_date, + check_course_open_for_learner, check_start_date, debug, - in_preview_mode, - check_course_open_for_learner, -) -from courseware.access_response import ( - NoAllowedPartitionGroupsError, - IncorrectPartitionGroupError, + in_preview_mode ) from courseware.masquerade import get_masquerade_role, is_masquerading_as_student from lms.djangoapps.ccx.custom_exception import CCXLocatorValidationException @@ -162,7 +163,7 @@ def has_access(user, action, obj, course_key=None): if isinstance(obj, UsageKey): return _has_access_location(user, action, obj, course_key) - if isinstance(obj, basestring): + if isinstance(obj, six.string_types): return _has_access_string(user, action, obj) # Passing an unknown object here is a coding error, so rather than @@ -812,7 +813,12 @@ def _can_access_descriptor_with_milestones(user, descriptor, course_key): descriptor: the object being accessed course_key: key for the course for this descriptor """ - if milestones_helpers.get_course_content_milestones(course_key, unicode(descriptor.location), 'requires', user.id): + if milestones_helpers.get_course_content_milestones( + course_key, + six.text_type(descriptor.location), + 'requires', + user.id + ): debug("Deny: user has not completed all milestones for content") return ACCESS_DENIED else: diff --git a/lms/djangoapps/courseware/access_response.py b/lms/djangoapps/courseware/access_response.py index 2bc02c209b..781468a4e0 100644 --- a/lms/djangoapps/courseware/access_response.py +++ b/lms/djangoapps/courseware/access_response.py @@ -2,6 +2,8 @@ This file contains all the classes used by has_access for error handling """ +from __future__ import absolute_import + from django.utils.translation import ugettext as _ from xmodule.course_metadata_utils import DEFAULT_START_DATE diff --git a/lms/djangoapps/courseware/access_utils.py b/lms/djangoapps/courseware/access_utils.py index 58f4afa8dd..f434d6fcaa 100644 --- a/lms/djangoapps/courseware/access_utils.py +++ b/lms/djangoapps/courseware/access_utils.py @@ -3,6 +3,8 @@ Simple utility functions for computing access. It allows us to share code between access.py and block transformers. """ +from __future__ import absolute_import + from datetime import datetime, timedelta from logging import getLogger @@ -11,10 +13,7 @@ from django.utils.translation import ugettext as _ from pytz import UTC from courseware.access_response import AccessResponse, StartDateError -from courseware.masquerade import ( - get_course_masquerade, - is_masquerading_as_student -) +from courseware.masquerade import get_course_masquerade, is_masquerading_as_student from openedx.core.djangoapps.util.user_messages import PageLevelMessages from openedx.core.djangolib.markup import HTML from openedx.features.course_experience import COURSE_PRE_START_ACCESS_FLAG diff --git a/lms/djangoapps/courseware/courseware_access_exception.py b/lms/djangoapps/courseware/courseware_access_exception.py index 931782f98c..0afba9024a 100644 --- a/lms/djangoapps/courseware/courseware_access_exception.py +++ b/lms/djangoapps/courseware/courseware_access_exception.py @@ -1,6 +1,8 @@ """ This file contains the exception used in courseware access """ +from __future__ import absolute_import + from django.http import Http404 diff --git a/lms/djangoapps/courseware/field_overrides.py b/lms/djangoapps/courseware/field_overrides.py index f2da813216..48ad693474 100644 --- a/lms/djangoapps/courseware/field_overrides.py +++ b/lms/djangoapps/courseware/field_overrides.py @@ -14,10 +14,13 @@ package and is used to wrap the `authored_data` when constructing an `LmsFieldData`. This means overrides will be in effect for all scopes covered by `authored_data`, e.g. course content and settings stored in Mongo. """ +from __future__ import absolute_import + import threading from abc import ABCMeta, abstractmethod from contextlib import contextmanager +import six from django.conf import settings from edx_django_utils.cache import DEFAULT_REQUEST_CACHE from xblock.field_data import FieldData @@ -91,7 +94,7 @@ def overrides_disabled(): return bool(_OVERRIDES_DISABLED.disabled) -class FieldOverrideProvider(object): +class FieldOverrideProvider(six.with_metaclass(ABCMeta, object)): """ Abstract class which defines the interface that a `FieldOverrideProvider` must provide. In general, providers should derive from this class, but @@ -102,7 +105,6 @@ class FieldOverrideProvider(object): field overrides. To set overrides, there will be a domain specific API for the concrete override implementation being used. """ - __metaclass__ = ABCMeta def __init__(self, user, fallback_field_data): self.user = user @@ -187,7 +189,7 @@ class OverrideFieldData(FieldData): if course is None: cache_key = ENABLED_OVERRIDE_PROVIDERS_KEY.format(course_id='None') else: - cache_key = ENABLED_OVERRIDE_PROVIDERS_KEY.format(course_id=unicode(course.id)) + cache_key = ENABLED_OVERRIDE_PROVIDERS_KEY.format(course_id=six.text_type(course.id)) enabled_providers = request_cache.data.get(cache_key, NOTSET) if enabled_providers == NOTSET: enabled_providers = tuple( @@ -234,7 +236,7 @@ class OverrideFieldData(FieldData): # If this is an inheritable field and an override is set above, # then we want to return False here, so the field_data uses the # override and not the original value for this block. - inheritable = InheritanceMixin.fields.keys() + inheritable = list(InheritanceMixin.fields.keys()) # pylint: disable=no-member if name in inheritable: for ancestor in _lineage(block): if self.get_override(ancestor, name) is not NOTSET: @@ -249,7 +251,7 @@ class OverrideFieldData(FieldData): # The `default` method is overloaded by the field storage system to # also handle inheritance. if self.providers and not overrides_disabled(): - inheritable = InheritanceMixin.fields.keys() + inheritable = list(InheritanceMixin.fields.keys()) # pylint: disable=no-member if name in inheritable: for ancestor in _lineage(block): value = self.get_override(ancestor, name) @@ -294,7 +296,7 @@ class OverrideModulestoreFieldData(OverrideFieldData): Arguments: block: An XBlock """ - course_id = unicode(block.location.course_key) + course_id = six.text_type(block.location.course_key) cache_key = ENABLED_MODULESTORE_OVERRIDE_PROVIDERS_KEY.format(course_id=course_id) request_cache = DEFAULT_REQUEST_CACHE diff --git a/lms/djangoapps/courseware/masquerade.py b/lms/djangoapps/courseware/masquerade.py index 98c36b21d0..4e3cf34ef6 100644 --- a/lms/djangoapps/courseware/masquerade.py +++ b/lms/djangoapps/courseware/masquerade.py @@ -4,9 +4,11 @@ Allow course staff to see a student or staff view of courseware. Which kind of view has been selected is stored in the session state. ''' -import logging +from __future__ import absolute_import +import logging from datetime import datetime + from django.conf import settings from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User diff --git a/lms/djangoapps/courseware/tabs.py b/lms/djangoapps/courseware/tabs.py index 33ed092c76..c8a8f86192 100644 --- a/lms/djangoapps/courseware/tabs.py +++ b/lms/djangoapps/courseware/tabs.py @@ -2,11 +2,15 @@ This module is essentially a broker to xmodule/tabs.py -- it was originally introduced to perform some LMS-specific tab display gymnastics for the Entrance Exams feature """ -from courseware.access import has_access -from courseware.entrance_exams import user_can_skip_entrance_exam +from __future__ import absolute_import + +import six from django.conf import settings from django.utils.translation import ugettext as _ from django.utils.translation import ugettext_noop + +from courseware.access import has_access +from courseware.entrance_exams import user_can_skip_entrance_exam from openedx.core.lib.course_tabs import CourseTabPluginManager from openedx.features.course_experience import UNIFIED_COURSE_TAB_FLAG, default_course_url_name from student.models import CourseEnrollment @@ -291,7 +295,7 @@ class SingleTextbookTab(CourseTab): def __init__(self, name, tab_id, view_name, index): def link_func(course, reverse_func, index=index): """ Constructs a link for textbooks from a view name, a course, and an index. """ - return reverse_func(view_name, args=[unicode(course.id), index]) + return reverse_func(view_name, args=[six.text_type(course.id), index]) tab_dict = dict() tab_dict['name'] = name diff --git a/lms/djangoapps/courseware/url_helpers.py b/lms/djangoapps/courseware/url_helpers.py index 2c82f2a653..0cf00a194b 100644 --- a/lms/djangoapps/courseware/url_helpers.py +++ b/lms/djangoapps/courseware/url_helpers.py @@ -1,9 +1,11 @@ """ Module to define url helpers functions """ -from urllib import urlencode +from __future__ import absolute_import +import six from django.urls import reverse +from six.moves.urllib.parse import urlencode # pylint: disable=import-error from xmodule.modulestore.django import modulestore from xmodule.modulestore.search import navigation_index, path_to_location @@ -32,13 +34,13 @@ def get_redirect_url(course_key, usage_key): # args provided by the redirect. # Rely on index to do all error handling and access control. if chapter is None: - redirect_url = reverse('courseware', args=(unicode(course_key), )) + redirect_url = reverse('courseware', args=(six.text_type(course_key), )) elif section is None: - redirect_url = reverse('courseware_chapter', args=(unicode(course_key), chapter)) + redirect_url = reverse('courseware_chapter', args=(six.text_type(course_key), chapter)) elif position is None: redirect_url = reverse( 'courseware_section', - args=(unicode(course_key), chapter, section) + args=(six.text_type(course_key), chapter, section) ) else: # Here we use the navigation_index from the position returned from @@ -46,7 +48,7 @@ def get_redirect_url(course_key, usage_key): # moment redirect_url = reverse( 'courseware_position', - args=(unicode(course_key), chapter, section, navigation_index(position)) + args=(six.text_type(course_key), chapter, section, navigation_index(position)) ) - redirect_url += "?{}".format(urlencode({'activate_block_id': unicode(final_target_id)})) + redirect_url += "?{}".format(urlencode({'activate_block_id': six.text_type(final_target_id)})) return redirect_url