From f33a89fc5238b9b19513332fa1b71e47bf278b87 Mon Sep 17 00:00:00 2001 From: Ayub khan Date: Tue, 9 Jul 2019 12:42:43 +0500 Subject: [PATCH] python 3 compatibility --- .../core/djangoapps/user_api/accounts/api.py | 7 +----- openedx/core/djangoapps/user_api/admin.py | 5 ++++- openedx/core/djangoapps/user_api/api.py | 22 +++++++++++++------ openedx/core/djangoapps/user_api/forms.py | 2 ++ openedx/core/djangoapps/user_api/helpers.py | 13 ++++++----- .../core/djangoapps/user_api/legacy_urls.py | 2 ++ .../core/djangoapps/user_api/message_types.py | 2 ++ .../core/djangoapps/user_api/middleware.py | 4 +++- openedx/core/djangoapps/user_api/models.py | 3 +++ .../djangoapps/user_api/partition_schemes.py | 6 +++-- .../core/djangoapps/user_api/permissions.py | 2 ++ openedx/core/djangoapps/user_api/rules.py | 2 +- .../core/djangoapps/user_api/serializers.py | 4 +++- openedx/core/djangoapps/user_api/urls.py | 4 +++- openedx/core/djangoapps/user_api/views.py | 19 ++++++++++------ 15 files changed, 65 insertions(+), 32 deletions(-) diff --git a/openedx/core/djangoapps/user_api/accounts/api.py b/openedx/core/djangoapps/user_api/accounts/api.py index 7b60511233..6e2b9ea3da 100644 --- a/openedx/core/djangoapps/user_api/accounts/api.py +++ b/openedx/core/djangoapps/user_api/accounts/api.py @@ -43,12 +43,7 @@ from student.models import ( from util.model_utils import emit_setting_changed_event from util.password_policy_validators import normalize_password, validate_password -from .serializers import ( - AccountLegacyProfileSerializer, - AccountUserSerializer, - UserReadOnlySerializer, - _visible_fields -) +from .serializers import AccountLegacyProfileSerializer, AccountUserSerializer, UserReadOnlySerializer, _visible_fields # Public access point for this function. visible_fields = _visible_fields diff --git a/openedx/core/djangoapps/user_api/admin.py b/openedx/core/djangoapps/user_api/admin.py index 6a5f50c2a0..b10612f18b 100644 --- a/openedx/core/djangoapps/user_api/admin.py +++ b/openedx/core/djangoapps/user_api/admin.py @@ -1,6 +1,8 @@ """ Django admin configuration pages for the user_api app """ +from __future__ import absolute_import + from django.conf.urls import url from django.contrib import admin, messages from django.core.exceptions import ValidationError @@ -11,7 +13,8 @@ from django.utils.html import format_html from django.utils.translation import ugettext as _ from openedx.core.djangoapps.user_api.accounts.forms import RetirementQueueDeletionForm -from .models import UserRetirementPartnerReportingStatus, RetirementState, UserRetirementStatus, UserRetirementRequest + +from .models import RetirementState, UserRetirementPartnerReportingStatus, UserRetirementRequest, UserRetirementStatus @admin.register(RetirementState) diff --git a/openedx/core/djangoapps/user_api/api.py b/openedx/core/djangoapps/user_api/api.py index 75c87755cc..2668a42522 100644 --- a/openedx/core/djangoapps/user_api/api.py +++ b/openedx/core/djangoapps/user_api/api.py @@ -1,23 +1,31 @@ -import copy -import crum +""" +User Api. +""" +from __future__ import absolute_import +import copy + +import crum +import six from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.urls import reverse from django.utils.translation import ugettext as _ from django_countries import countries -import accounts import third_party_auth from edxmako.shortcuts import marketing_link -from openedx.core.djangolib.markup import HTML, Text from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers +from openedx.core.djangoapps.user_api import accounts from openedx.core.djangoapps.user_api.helpers import FormDescription +from openedx.core.djangolib.markup import HTML, Text from openedx.features.enterprise_support.api import enterprise_customer_for_request from student.forms import get_registration_extension_form from student.models import UserProfile from util.password_policy_validators import ( - password_validators_instruction_texts, password_validators_restrictions, DEFAULT_MAX_PASSWORD_LENGTH, + DEFAULT_MAX_PASSWORD_LENGTH, + password_validators_instruction_texts, + password_validators_restrictions ) @@ -476,7 +484,7 @@ class RegistrationFormFactory(object): # form used to select the user's year of birth. yob_label = _(u"Year of birth") - options = [(unicode(year), unicode(year)) for year in UserProfile.VALID_YEARS] + options = [(six.text_type(year), six.text_type(year)) for year in UserProfile.VALID_YEARS] form_desc.add_field( "year_of_birth", label=yob_label, @@ -512,7 +520,7 @@ class RegistrationFormFactory(object): field_type = "select" include_default_option = True field_options = extra_field_options.get(field_name) - options = [(unicode(option.lower()), option) for option in field_options] + options = [(six.text_type(option.lower()), option) for option in field_options] error_msg = '' error_msg = getattr(accounts, u'REQUIRED_FIELD_{}_SELECT_MSG'.format(field_name.upper())) diff --git a/openedx/core/djangoapps/user_api/forms.py b/openedx/core/djangoapps/user_api/forms.py index df296db2a2..5375cc5a57 100644 --- a/openedx/core/djangoapps/user_api/forms.py +++ b/openedx/core/djangoapps/user_api/forms.py @@ -3,4 +3,6 @@ """ Django Administration forms module """ +from __future__ import absolute_import + from student.forms import PasswordResetFormNoActive diff --git a/openedx/core/djangoapps/user_api/helpers.py b/openedx/core/djangoapps/user_api/helpers.py index c462ffb633..07d010e213 100644 --- a/openedx/core/djangoapps/user_api/helpers.py +++ b/openedx/core/djangoapps/user_api/helpers.py @@ -2,15 +2,18 @@ Helper functions for the account/profile Python APIs. This is NOT part of the public API. """ +from __future__ import absolute_import + import json import logging import traceback from collections import defaultdict from functools import wraps +import six from django import forms from django.core.serializers.json import DjangoJSONEncoder -from django.http import HttpResponseBadRequest, HttpRequest +from django.http import HttpRequest, HttpResponseBadRequest from django.utils.encoding import force_text from django.utils.functional import Promise @@ -58,7 +61,7 @@ def intercept_errors(api_error, ignore_errors=None): u"with arguments '{args}' and keyword arguments '{kwargs}': " u"{exception}" ).format( - func_name=func.func_name, + func_name=func.__name__, args=args, kwargs=kwargs, exception=ex.developer_message if hasattr(ex, 'developer_message') else repr(ex) @@ -74,7 +77,7 @@ def intercept_errors(api_error, ignore_errors=None): u"with arguments '{args}' and keyword arguments '{kwargs}' from {caller}: " u"{exception}" ).format( - func_name=func.func_name, + func_name=func.__name__, args=args, kwargs=kwargs, exception=ex.developer_message if hasattr(ex, 'developer_message') else repr(ex), @@ -264,7 +267,7 @@ class FormDescription(object): if restrictions is not None: allowed_restrictions = self.ALLOWED_RESTRICTIONS.get(field_type, []) - for key, val in restrictions.iteritems(): + for key, val in six.iteritems(restrictions): if key in allowed_restrictions: field_dict["restrictions"][key] = val else: @@ -361,7 +364,7 @@ class FormDescription(object): self._field_overrides[field_name].update({ property_name: property_value - for property_name, property_value in kwargs.iteritems() + for property_name, property_value in six.iteritems(kwargs) if property_name in self.OVERRIDE_FIELD_PROPERTIES }) diff --git a/openedx/core/djangoapps/user_api/legacy_urls.py b/openedx/core/djangoapps/user_api/legacy_urls.py index 868366edc9..77ddd36ed8 100644 --- a/openedx/core/djangoapps/user_api/legacy_urls.py +++ b/openedx/core/djangoapps/user_api/legacy_urls.py @@ -2,6 +2,8 @@ Defines the URL routes for this app. """ +from __future__ import absolute_import + from django.conf import settings from django.conf.urls import include, url from rest_framework import routers diff --git a/openedx/core/djangoapps/user_api/message_types.py b/openedx/core/djangoapps/user_api/message_types.py index be0f0e42a3..a92e857e4a 100644 --- a/openedx/core/djangoapps/user_api/message_types.py +++ b/openedx/core/djangoapps/user_api/message_types.py @@ -2,6 +2,8 @@ Message Types for user_api emails """ +from __future__ import absolute_import + from django.conf import settings from openedx.core.djangoapps.ace_common.message import BaseMessageType diff --git a/openedx/core/djangoapps/user_api/middleware.py b/openedx/core/djangoapps/user_api/middleware.py index ffb3303d9c..bd7c63b32d 100644 --- a/openedx/core/djangoapps/user_api/middleware.py +++ b/openedx/core/djangoapps/user_api/middleware.py @@ -3,10 +3,12 @@ Middleware for user api. Adds user's tags to tracking event context. """ +from __future__ import absolute_import + +from eventtracking import tracker from opaque_keys import InvalidKeyError from opaque_keys.edx.keys import CourseKey -from eventtracking import tracker from track.contexts import COURSE_REGEX from .models import UserCourseTag diff --git a/openedx/core/djangoapps/user_api/models.py b/openedx/core/djangoapps/user_api/models.py index 1fb6e933f0..2a2e79707a 100644 --- a/openedx/core/djangoapps/user_api/models.py +++ b/openedx/core/djangoapps/user_api/models.py @@ -1,6 +1,8 @@ """ Django ORM model specifications for the User API application """ +from __future__ import absolute_import + from django.contrib.auth.models import User from django.core.validators import RegexValidator from django.db import models @@ -8,6 +10,7 @@ from django.db.models.signals import post_delete, post_save, pre_save from django.dispatch import receiver from model_utils.models import TimeStampedModel from opaque_keys.edx.django.models import CourseKeyField + from openedx.core.djangolib.model_mixins import DeletableByUserValue # Currently, the "student" app is responsible for diff --git a/openedx/core/djangoapps/user_api/partition_schemes.py b/openedx/core/djangoapps/user_api/partition_schemes.py index 2ce8c955b2..16cc26008b 100644 --- a/openedx/core/djangoapps/user_api/partition_schemes.py +++ b/openedx/core/djangoapps/user_api/partition_schemes.py @@ -1,13 +1,15 @@ """ Provides partition support to the user service. """ +from __future__ import absolute_import + import logging import random -import course_tag.api as course_tag_api from eventtracking import tracker -from xmodule.partitions.partitions import UserPartitionError, NoSuchUserPartitionGroupError +import openedx.core.djangoapps.user_api.course_tag.api as course_tag_api +from xmodule.partitions.partitions import NoSuchUserPartitionGroupError, UserPartitionError log = logging.getLogger(__name__) diff --git a/openedx/core/djangoapps/user_api/permissions.py b/openedx/core/djangoapps/user_api/permissions.py index 677633d694..dbcc6d078e 100644 --- a/openedx/core/djangoapps/user_api/permissions.py +++ b/openedx/core/djangoapps/user_api/permissions.py @@ -1,6 +1,8 @@ """ Permissions classes for User-API aware views. """ +from __future__ import absolute_import + from django.contrib.auth.models import User from django.http import Http404 from django.shortcuts import get_object_or_404 diff --git a/openedx/core/djangoapps/user_api/rules.py b/openedx/core/djangoapps/user_api/rules.py index 7eecbe99c4..0574ec7bb7 100644 --- a/openedx/core/djangoapps/user_api/rules.py +++ b/openedx/core/djangoapps/user_api/rules.py @@ -3,8 +3,8 @@ Django rules for accounts """ from __future__ import absolute_import -from django.conf import settings import rules +from django.conf import settings @rules.predicate diff --git a/openedx/core/djangoapps/user_api/serializers.py b/openedx/core/djangoapps/user_api/serializers.py index 76dcdb7fe5..3156083359 100644 --- a/openedx/core/djangoapps/user_api/serializers.py +++ b/openedx/core/djangoapps/user_api/serializers.py @@ -1,11 +1,13 @@ """ Django REST Framework serializers for the User API application """ +from __future__ import absolute_import + from django.contrib.auth.models import User from django.utils.timezone import now from rest_framework import serializers -from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification, SSOVerification, ManualVerification +from lms.djangoapps.verify_student.models import ManualVerification, SoftwareSecurePhotoVerification, SSOVerification from .models import UserPreference diff --git a/openedx/core/djangoapps/user_api/urls.py b/openedx/core/djangoapps/user_api/urls.py index 334ffc7e8c..dcce105ce2 100644 --- a/openedx/core/djangoapps/user_api/urls.py +++ b/openedx/core/djangoapps/user_api/urls.py @@ -2,6 +2,8 @@ Defines the URL routes for this app. """ +from __future__ import absolute_import + from django.conf import settings from django.conf.urls import url @@ -17,8 +19,8 @@ from .accounts.views import ( UsernameReplacementView ) from .preferences.views import PreferencesDetailView, PreferencesView -from .verification_api.views import IDVerificationStatusView from .validation.views import RegistrationValidationView +from .verification_api.views import IDVerificationStatusView ME = AccountViewSet.as_view({ 'get': 'get', diff --git a/openedx/core/djangoapps/user_api/views.py b/openedx/core/djangoapps/user_api/views.py index bc7078ae4f..b947e0fcf4 100644 --- a/openedx/core/djangoapps/user_api/views.py +++ b/openedx/core/djangoapps/user_api/views.py @@ -1,5 +1,7 @@ """HTTP end-points for the User API. """ +from __future__ import absolute_import + from django.contrib.auth.models import User from django.core.exceptions import NON_FIELD_ERRORS, PermissionDenied, ValidationError from django.db import transaction @@ -9,16 +11,15 @@ from django.utils.translation import ugettext as _ from django.views.decorators.csrf import csrf_exempt, csrf_protect, ensure_csrf_cookie from django.views.decorators.debug import sensitive_post_parameters from django_filters.rest_framework import DjangoFilterBackend -from rest_framework import authentication, generics, status, viewsets -from rest_framework.exceptions import ParseError -from rest_framework.views import APIView -from rest_framework.permissions import IsAuthenticated -from six import text_type - from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser from opaque_keys import InvalidKeyError from opaque_keys.edx import locator from opaque_keys.edx.keys import CourseKey +from rest_framework import authentication, generics, status, viewsets +from rest_framework.exceptions import ParseError +from rest_framework.permissions import IsAuthenticated +from rest_framework.views import APIView +from six import text_type from openedx.core.djangoapps.django_comment_common.models import Role from openedx.core.djangoapps.user_api import accounts @@ -31,7 +32,11 @@ from openedx.core.djangoapps.user_api.api import ( from openedx.core.djangoapps.user_api.helpers import require_post_params, shim_student_view from openedx.core.djangoapps.user_api.models import UserPreference from openedx.core.djangoapps.user_api.preferences.api import get_country_time_zones, update_email_opt_in -from openedx.core.djangoapps.user_api.serializers import CountryTimeZoneSerializer, UserPreferenceSerializer, UserSerializer +from openedx.core.djangoapps.user_api.serializers import ( + CountryTimeZoneSerializer, + UserPreferenceSerializer, + UserSerializer +) from openedx.core.djangoapps.user_authn.cookies import set_logged_in_cookies from openedx.core.djangoapps.user_authn.views.register import create_account_with_params from openedx.core.lib.api.permissions import ApiKeyHeaderPermission