From f587bb82974e52dbf131a6513f5561b9553e88d3 Mon Sep 17 00:00:00 2001 From: Amit <43564590+amitvadhel@users.noreply.github.com> Date: Fri, 10 May 2019 21:10:35 +0300 Subject: [PATCH] INCR-234 (#20511) * INCR-234: Run python-modernize and isort on openedx/core/djangoapps/oauth_dispatch * INCR-234: [ADD] Missing module docstring --- openedx/core/djangoapps/oauth_dispatch/adapters/dop.py | 3 ++- openedx/core/djangoapps/oauth_dispatch/adapters/dot.py | 1 + openedx/core/djangoapps/oauth_dispatch/admin.py | 4 +++- openedx/core/djangoapps/oauth_dispatch/api.py | 4 +++- .../djangoapps/oauth_dispatch/dot_overrides/backends.py | 1 + .../djangoapps/oauth_dispatch/dot_overrides/validators.py | 3 ++- .../core/djangoapps/oauth_dispatch/dot_overrides/views.py | 3 ++- openedx/core/djangoapps/oauth_dispatch/jwt.py | 7 ++++--- .../management/commands/edx_clear_expired_tokens.py | 3 +++ .../management/commands/tests/test_clear_expired_tokens.py | 3 +++ openedx/core/djangoapps/oauth_dispatch/models.py | 5 ++++- openedx/core/djangoapps/oauth_dispatch/scopes.py | 4 +++- openedx/core/djangoapps/oauth_dispatch/toggles.py | 4 +++- openedx/core/djangoapps/oauth_dispatch/urls.py | 2 ++ openedx/core/djangoapps/oauth_dispatch/views.py | 2 +- 15 files changed, 37 insertions(+), 12 deletions(-) diff --git a/openedx/core/djangoapps/oauth_dispatch/adapters/dop.py b/openedx/core/djangoapps/oauth_dispatch/adapters/dop.py index 8948e9a92a..95a7b0e706 100644 --- a/openedx/core/djangoapps/oauth_dispatch/adapters/dop.py +++ b/openedx/core/djangoapps/oauth_dispatch/adapters/dop.py @@ -3,8 +3,9 @@ Adapter to isolate django-oauth2-provider dependencies """ from __future__ import absolute_import -from provider.oauth2 import models + from provider import constants, scope +from provider.oauth2 import models class DOPAdapter(object): diff --git a/openedx/core/djangoapps/oauth_dispatch/adapters/dot.py b/openedx/core/djangoapps/oauth_dispatch/adapters/dot.py index 5994860427..aa727dae2c 100644 --- a/openedx/core/djangoapps/oauth_dispatch/adapters/dot.py +++ b/openedx/core/djangoapps/oauth_dispatch/adapters/dot.py @@ -3,6 +3,7 @@ Adapter to isolate django-oauth-toolkit dependencies """ from __future__ import absolute_import + from oauth2_provider import models from openedx.core.djangoapps.oauth_dispatch.models import RestrictedApplication diff --git a/openedx/core/djangoapps/oauth_dispatch/admin.py b/openedx/core/djangoapps/oauth_dispatch/admin.py index 54689f2d8c..3adf1345b1 100644 --- a/openedx/core/djangoapps/oauth_dispatch/admin.py +++ b/openedx/core/djangoapps/oauth_dispatch/admin.py @@ -2,10 +2,12 @@ Override admin configuration for django-oauth-toolkit """ +from __future__ import absolute_import + from django.contrib.admin import ModelAdmin, site from oauth2_provider import models -from .models import RestrictedApplication, ApplicationAccess, ApplicationOrganization +from .models import ApplicationAccess, ApplicationOrganization, RestrictedApplication def reregister(model_class): diff --git a/openedx/core/djangoapps/oauth_dispatch/api.py b/openedx/core/djangoapps/oauth_dispatch/api.py index d2b61a1c52..34260de6b2 100644 --- a/openedx/core/djangoapps/oauth_dispatch/api.py +++ b/openedx/core/djangoapps/oauth_dispatch/api.py @@ -1,8 +1,10 @@ """ OAuth related Python apis. """ -from oauthlib.oauth2.rfc6749.tokens import BearerToken +from __future__ import absolute_import + from oauth2_provider.models import AccessToken as dot_access_token from oauth2_provider.models import RefreshToken as dot_refresh_token from oauth2_provider.settings import oauth2_settings as dot_settings +from oauthlib.oauth2.rfc6749.tokens import BearerToken from provider.oauth2.models import AccessToken as dop_access_token from provider.oauth2.models import RefreshToken as dop_refresh_token diff --git a/openedx/core/djangoapps/oauth_dispatch/dot_overrides/backends.py b/openedx/core/djangoapps/oauth_dispatch/dot_overrides/backends.py index 27682a0291..b5033de9c8 100644 --- a/openedx/core/djangoapps/oauth_dispatch/dot_overrides/backends.py +++ b/openedx/core/djangoapps/oauth_dispatch/dot_overrides/backends.py @@ -2,6 +2,7 @@ Custom authentication backends. """ from __future__ import absolute_import + from django.contrib.auth.backends import AllowAllUsersModelBackend as UserModelBackend from ratelimitbackend.backends import RateLimitMixin diff --git a/openedx/core/djangoapps/oauth_dispatch/dot_overrides/validators.py b/openedx/core/djangoapps/oauth_dispatch/dot_overrides/validators.py index 77e9e45857..3aabbf71da 100644 --- a/openedx/core/djangoapps/oauth_dispatch/dot_overrides/validators.py +++ b/openedx/core/djangoapps/oauth_dispatch/dot_overrides/validators.py @@ -1,7 +1,8 @@ """ Classes that override default django-oauth-toolkit behavior """ -from __future__ import unicode_literals, absolute_import +from __future__ import absolute_import, unicode_literals + from datetime import datetime, timedelta from django.contrib.auth import authenticate, get_user_model diff --git a/openedx/core/djangoapps/oauth_dispatch/dot_overrides/views.py b/openedx/core/djangoapps/oauth_dispatch/dot_overrides/views.py index 6e59fbfc92..559059711b 100644 --- a/openedx/core/djangoapps/oauth_dispatch/dot_overrides/views.py +++ b/openedx/core/djangoapps/oauth_dispatch/dot_overrides/views.py @@ -1,7 +1,8 @@ """ Classes that override default django-oauth-toolkit behavior """ -from __future__ import unicode_literals, absolute_import +from __future__ import absolute_import, unicode_literals + from oauth2_provider.exceptions import OAuthToolkitError from oauth2_provider.http import HttpResponseUriRedirect from oauth2_provider.models import get_access_token_model, get_application_model diff --git a/openedx/core/djangoapps/oauth_dispatch/jwt.py b/openedx/core/djangoapps/oauth_dispatch/jwt.py index 0d171c8c8b..b8a82a8e98 100644 --- a/openedx/core/djangoapps/oauth_dispatch/jwt.py +++ b/openedx/core/djangoapps/oauth_dispatch/jwt.py @@ -1,14 +1,15 @@ """Utilities for working with ID tokens.""" +from __future__ import absolute_import + import json from time import time from django.conf import settings +from edx_django_utils.monitoring import set_custom_metric +from edx_rbac.utils import create_role_auth_claim_for_user from jwkest import jwk from jwkest.jws import JWS -from edx_rbac.utils import create_role_auth_claim_for_user - -from edx_django_utils.monitoring import set_custom_metric from openedx.core.djangoapps.oauth_dispatch.toggles import ENFORCE_JWT_SCOPES from student.models import UserProfile, anonymous_id_for_user diff --git a/openedx/core/djangoapps/oauth_dispatch/management/commands/edx_clear_expired_tokens.py b/openedx/core/djangoapps/oauth_dispatch/management/commands/edx_clear_expired_tokens.py index 2ed07490a7..10a9786950 100644 --- a/openedx/core/djangoapps/oauth_dispatch/management/commands/edx_clear_expired_tokens.py +++ b/openedx/core/djangoapps/oauth_dispatch/management/commands/edx_clear_expired_tokens.py @@ -1,3 +1,6 @@ +""" +Management command for clear expired access tokens! +""" from __future__ import absolute_import, unicode_literals import logging diff --git a/openedx/core/djangoapps/oauth_dispatch/management/commands/tests/test_clear_expired_tokens.py b/openedx/core/djangoapps/oauth_dispatch/management/commands/tests/test_clear_expired_tokens.py index 9390f2953e..65cabf44a4 100644 --- a/openedx/core/djangoapps/oauth_dispatch/management/commands/tests/test_clear_expired_tokens.py +++ b/openedx/core/djangoapps/oauth_dispatch/management/commands/tests/test_clear_expired_tokens.py @@ -1,3 +1,6 @@ +""" +Tests the ``edx_clear_expired_tokens`` management command. +""" from __future__ import absolute_import import unittest diff --git a/openedx/core/djangoapps/oauth_dispatch/models.py b/openedx/core/djangoapps/oauth_dispatch/models.py index 9f42502aea..42a58e2918 100644 --- a/openedx/core/djangoapps/oauth_dispatch/models.py +++ b/openedx/core/djangoapps/oauth_dispatch/models.py @@ -2,8 +2,11 @@ Specialized models for oauth_dispatch djangoapp """ +from __future__ import absolute_import + from datetime import datetime +import six from django.db import models from django.utils.translation import ugettext_lazy as _ from django_mysql.models import ListCharField @@ -140,4 +143,4 @@ class ApplicationOrganization(models.Model): """ Serialize for use in JWT filter claim. """ - return unicode(':'.join([self.relation_type, self.organization.short_name])) + return six.text_type(':'.join([self.relation_type, self.organization.short_name])) diff --git a/openedx/core/djangoapps/oauth_dispatch/scopes.py b/openedx/core/djangoapps/oauth_dispatch/scopes.py index ddce0bd3f3..8ce5a2ad90 100644 --- a/openedx/core/djangoapps/oauth_dispatch/scopes.py +++ b/openedx/core/djangoapps/oauth_dispatch/scopes.py @@ -2,6 +2,8 @@ Custom Django OAuth Toolkit scopes backends. """ +from __future__ import absolute_import + from oauth2_provider.scopes import SettingsScopes from openedx.core.djangoapps.oauth_dispatch.models import ApplicationAccess @@ -19,5 +21,5 @@ class ApplicationModelScopes(SettingsScopes): application_scopes = [] default_scopes = self.get_default_scopes() - all_scopes = self.get_all_scopes().keys() + all_scopes = list(self.get_all_scopes().keys()) return set(application_scopes + default_scopes).intersection(all_scopes) diff --git a/openedx/core/djangoapps/oauth_dispatch/toggles.py b/openedx/core/djangoapps/oauth_dispatch/toggles.py index fe1fd23909..a97d5708fe 100644 --- a/openedx/core/djangoapps/oauth_dispatch/toggles.py +++ b/openedx/core/djangoapps/oauth_dispatch/toggles.py @@ -2,9 +2,11 @@ Feature toggle code for oauth_dispatch. """ +from __future__ import absolute_import + from edx_rest_framework_extensions.config import SWITCH_ENFORCE_JWT_SCOPES -from openedx.core.djangoapps.waffle_utils import WaffleSwitchNamespace, WaffleSwitch +from openedx.core.djangoapps.waffle_utils import WaffleSwitch, WaffleSwitchNamespace WAFFLE_NAMESPACE = 'oauth2' OAUTH2_SWITCHES = WaffleSwitchNamespace(name=WAFFLE_NAMESPACE) diff --git a/openedx/core/djangoapps/oauth_dispatch/urls.py b/openedx/core/djangoapps/oauth_dispatch/urls.py index 6bf4125f8b..a9712cbe95 100644 --- a/openedx/core/djangoapps/oauth_dispatch/urls.py +++ b/openedx/core/djangoapps/oauth_dispatch/urls.py @@ -2,6 +2,8 @@ OAuth2 wrapper urls """ +from __future__ import absolute_import + from django.conf import settings from django.conf.urls import url from django.views.decorators.csrf import csrf_exempt diff --git a/openedx/core/djangoapps/oauth_dispatch/views.py b/openedx/core/djangoapps/oauth_dispatch/views.py index cda1deb66a..61c694181e 100644 --- a/openedx/core/djangoapps/oauth_dispatch/views.py +++ b/openedx/core/djangoapps/oauth_dispatch/views.py @@ -3,7 +3,7 @@ Views that dispatch processing of OAuth requests to django-oauth2-provider or django-oauth-toolkit as appropriate. """ -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals import json