From 2c8c3b8a74970911d423be8aecd0da3f4d7efa44 Mon Sep 17 00:00:00 2001 From: Amit <43564590+amitvadhel@users.noreply.github.com> Date: Mon, 8 Jul 2019 20:57:26 +0300 Subject: [PATCH] INCR-469: Updates on Python 3.x, disable: import-error and fix line over length (#20962) --- lms/djangoapps/certificates/apis/urls.py | 2 + .../certificates/apis/v0/tests/test_views.py | 9 +- lms/djangoapps/certificates/apis/v0/urls.py | 2 + lms/djangoapps/certificates/apis/v0/views.py | 22 +-- .../certificates/tests/factories.py | 2 + .../tests/test_create_fake_cert.py | 2 + .../certificates/tests/test_queue.py | 16 +- .../certificates/tests/test_webview_views.py | 139 ++++++++++-------- 8 files changed, 113 insertions(+), 81 deletions(-) diff --git a/lms/djangoapps/certificates/apis/urls.py b/lms/djangoapps/certificates/apis/urls.py index d37514a358..8855821d54 100644 --- a/lms/djangoapps/certificates/apis/urls.py +++ b/lms/djangoapps/certificates/apis/urls.py @@ -1,6 +1,8 @@ """ Certificates API URLs. """ +from __future__ import absolute_import + from django.conf.urls import include, url app_name = 'certificates' diff --git a/lms/djangoapps/certificates/apis/v0/tests/test_views.py b/lms/djangoapps/certificates/apis/v0/tests/test_views.py index 2b026ba1d8..09ea8b076d 100644 --- a/lms/djangoapps/certificates/apis/v0/tests/test_views.py +++ b/lms/djangoapps/certificates/apis/v0/tests/test_views.py @@ -1,9 +1,12 @@ """ Tests for the Certificate REST APIs. """ +from __future__ import absolute_import + from itertools import product import ddt +import six from django.urls import reverse from django.utils import timezone from freezegun import freeze_time @@ -18,7 +21,7 @@ from lms.djangoapps.certificates.tests.factories import GeneratedCertificateFact from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory from openedx.core.djangoapps.oauth_dispatch.toggles import ENFORCE_JWT_SCOPES from openedx.core.djangoapps.user_api.tests.factories import UserPreferenceFactory -from openedx.core.djangoapps.user_authn.tests.utils import AuthType, AuthAndScopesTestMixin, JWT_AUTH_TYPES +from openedx.core.djangoapps.user_authn.tests.utils import JWT_AUTH_TYPES, AuthAndScopesTestMixin, AuthType from student.tests.factories import UserFactory from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory @@ -80,7 +83,7 @@ class CertificatesDetailRestApiTest(AuthAndScopesTestMixin, SharedModuleStoreTes 'grade': '0.88', 'download_url': 'www.google.com', 'certificate_type': CourseMode.VERIFIED, - 'course_id': unicode(self.course.id), + 'course_id': six.text_type(self.course.id), 'created_date': self.now, } ) @@ -158,7 +161,7 @@ class CertificatesListRestApiTest(AuthAndScopesTestMixin, SharedModuleStoreTestC response.data, [{ 'username': self.student.username, - 'course_id': unicode(self.course.id), + 'course_id': six.text_type(self.course.id), 'course_display_name': self.course.display_name, 'course_organization': self.course.org, 'certificate_type': CourseMode.VERIFIED, diff --git a/lms/djangoapps/certificates/apis/v0/urls.py b/lms/djangoapps/certificates/apis/v0/urls.py index d2eba86785..ceaeb24176 100644 --- a/lms/djangoapps/certificates/apis/v0/urls.py +++ b/lms/djangoapps/certificates/apis/v0/urls.py @@ -1,6 +1,8 @@ """ Certificates API v0 URLs. """ +from __future__ import absolute_import + from django.conf import settings from django.conf.urls import include, url diff --git a/lms/djangoapps/certificates/apis/v0/views.py b/lms/djangoapps/certificates/apis/v0/views.py index 42ad12b284..7d7561ee6b 100644 --- a/lms/djangoapps/certificates/apis/v0/views.py +++ b/lms/djangoapps/certificates/apis/v0/views.py @@ -1,24 +1,26 @@ """ API v0 views. """ +from __future__ import absolute_import + import logging +import six from django.contrib.auth import get_user_model +from edx_rest_framework_extensions import permissions +from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication +from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser +from opaque_keys import InvalidKeyError +from opaque_keys.edx.keys import CourseKey from rest_condition import C from rest_framework.generics import GenericAPIView from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response -from edx_rest_framework_extensions import permissions -from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication -from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser from lms.djangoapps.certificates.api import get_certificate_for_user, get_certificates_for_user -from opaque_keys import InvalidKeyError -from opaque_keys.edx.keys import CourseKey from openedx.core.djangoapps.certificates.api import certificates_viewable_for_course from openedx.core.djangoapps.content.course_overviews.models import CourseOverview from openedx.core.djangoapps.user_api.accounts.api import visible_fields from openedx.core.lib.api.authentication import OAuth2AuthenticationAllowInactiveUser - log = logging.getLogger(__name__) User = get_user_model() @@ -118,7 +120,7 @@ class CertificatesDetailView(GenericAPIView): return Response( { "username": user_cert.get('username'), - "course_id": unicode(user_cert.get('course_key')), + "course_id": six.text_type(user_cert.get('course_key')), "certificate_type": user_cert.get('type'), "created_date": user_cert.get('created'), "status": user_cert.get('status'), @@ -222,7 +224,7 @@ class CertificatesListView(GenericAPIView): for user_cert in self._get_certificates_for_user(username): user_certs.append({ 'username': user_cert.get('username'), - 'course_id': unicode(user_cert.get('course_key')), + 'course_id': six.text_type(user_cert.get('course_key')), 'course_display_name': user_cert.get('course_display_name'), 'course_organization': user_cert.get('course_organization'), 'certificate_type': user_cert.get('type'), @@ -263,7 +265,9 @@ class CertificatesListView(GenericAPIView): passing_certificates[course_key] = course_certificate viewable_certificates = [] - for course_key, course_overview in CourseOverview.get_from_ids_if_exists(passing_certificates.keys()).items(): + for course_key, course_overview in CourseOverview.get_from_ids_if_exists( + list(passing_certificates.keys()) + ).items(): if certificates_viewable_for_course(course_overview): course_certificate = passing_certificates[course_key] course_certificate['course_display_name'] = course_overview.display_name_with_default diff --git a/lms/djangoapps/certificates/tests/factories.py b/lms/djangoapps/certificates/tests/factories.py index 24a3e4eec4..fe9667fa65 100644 --- a/lms/djangoapps/certificates/tests/factories.py +++ b/lms/djangoapps/certificates/tests/factories.py @@ -1,5 +1,7 @@ # Factories are self documenting # pylint: disable=missing-docstring +from __future__ import absolute_import + from uuid import uuid4 from factory.django import DjangoModelFactory diff --git a/lms/djangoapps/certificates/tests/test_create_fake_cert.py b/lms/djangoapps/certificates/tests/test_create_fake_cert.py index d8a3a296f8..94cceca099 100644 --- a/lms/djangoapps/certificates/tests/test_create_fake_cert.py +++ b/lms/djangoapps/certificates/tests/test_create_fake_cert.py @@ -1,5 +1,7 @@ """Tests for the create_fake_certs management command. """ +from __future__ import absolute_import + from django.core.management import call_command from django.core.management.base import CommandError from django.test import TestCase diff --git a/lms/djangoapps/certificates/tests/test_queue.py b/lms/djangoapps/certificates/tests/test_queue.py index 3d820778d9..b0f3a0ef84 100644 --- a/lms/djangoapps/certificates/tests/test_queue.py +++ b/lms/djangoapps/certificates/tests/test_queue.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- """Tests for the XQueue certificates interface. """ +from __future__ import absolute_import + import json from contextlib import contextmanager from datetime import datetime, timedelta @@ -7,6 +9,7 @@ from datetime import datetime, timedelta import ddt import freezegun import pytz +import six from django.test import TestCase from django.test.utils import override_settings from mock import Mock, patch @@ -19,10 +22,15 @@ from testfixtures import LogCapture # and verify that items are being correctly added to the queue # in our `XQueueCertInterface` implementation. from capa.xqueue_interface import XQueueInterface -from lms.djangoapps.certificates.models import CertificateStatuses, ExampleCertificate, ExampleCertificateSet, GeneratedCertificate +from course_modes.models import CourseMode +from lms.djangoapps.certificates.models import ( + CertificateStatuses, + ExampleCertificate, + ExampleCertificateSet, + GeneratedCertificate +) from lms.djangoapps.certificates.queue import LOGGER, XQueueCertInterface from lms.djangoapps.certificates.tests.factories import CertificateWhitelistFactory, GeneratedCertificateFactory -from course_modes.models import CourseMode from lms.djangoapps.grades.tests.utils import mock_passing_grade from lms.djangoapps.verify_student.tests.factories import SoftwareSecurePhotoVerificationFactory from student.tests.factories import CourseEnrollmentFactory, UserFactory @@ -344,7 +352,7 @@ class XQueueCertInterfaceAddCertificateTest(ModuleStoreTestCase): u"and download_url '{download_url}'." ).format( student_id=self.user_2.id, - course_id=unicode(self.course.id), + course_id=six.text_type(self.course.id), status=CertificateStatuses.downloadable, download_url=download_url ) @@ -414,7 +422,7 @@ class XQueueCertInterfaceExampleCertificateTest(TestCase): 'action': 'create', 'username': cert.uuid, 'name': u'John Doƫ', - 'course_id': unicode(self.COURSE_KEY), + 'course_id': six.text_type(self.COURSE_KEY), 'template_pdf': 'test.pdf', 'example_certificate': True } diff --git a/lms/djangoapps/certificates/tests/test_webview_views.py b/lms/djangoapps/certificates/tests/test_webview_views.py index 3087c0e22a..fa094efe54 100644 --- a/lms/djangoapps/certificates/tests/test_webview_views.py +++ b/lms/djangoapps/certificates/tests/test_webview_views.py @@ -1,19 +1,30 @@ # -*- coding: utf-8 -*- """Tests for certificates views. """ +from __future__ import absolute_import + import datetime import json from collections import OrderedDict -from urllib import urlencode from uuid import uuid4 +import ddt +import six from django.conf import settings -from django.urls import reverse from django.test.client import Client, RequestFactory from django.test.utils import override_settings +from django.urls import reverse from mock import patch +from six.moves import range +from six.moves.urllib.parse import urlencode # pylint: disable=import-error -import ddt +from course_modes.models import CourseMode +from lms.djangoapps.badges.events.course_complete import get_completion_badge +from lms.djangoapps.badges.tests.factories import ( + BadgeAssertionFactory, + BadgeClassFactory, + CourseCompleteImageConfigurationFactory +) from lms.djangoapps.certificates.api import get_certificate_url from lms.djangoapps.certificates.models import ( CertificateGenerationCourseSetting, @@ -29,22 +40,16 @@ from lms.djangoapps.certificates.tests.factories import ( GeneratedCertificateFactory, LinkedInAddToProfileConfigurationFactory ) -from course_modes.models import CourseMode -from lms.djangoapps.badges.events.course_complete import get_completion_badge -from lms.djangoapps.badges.tests.factories import ( - BadgeAssertionFactory, - BadgeClassFactory, - CourseCompleteImageConfigurationFactory -) from lms.djangoapps.grades.tests.utils import mock_passing_grade from openedx.core.djangoapps.certificates.config import waffle from openedx.core.djangoapps.dark_lang.models import DarkLangConfig from openedx.core.djangoapps.site_configuration.tests.test_util import ( - with_site_configuration, with_site_configuration_context, + with_site_configuration, + with_site_configuration_context ) -from openedx.core.lib.tests.assertions.events import assert_event_matches from openedx.core.djangolib.js_utils import js_escaped_string from openedx.core.djangolib.testing.utils import CacheIsolationTestCase +from openedx.core.lib.tests.assertions.events import assert_event_matches from student.roles import CourseStaffRole from student.tests.factories import CourseEnrollmentFactory, UserFactory from track.tests import EventTrackingTestCase @@ -127,7 +132,7 @@ class CommonCertificatesTestCase(ModuleStoreTestCase): 'organization': 'Signatory_Organization ' + str(i), 'signature_image_path': u'/static/certificates/images/demo-sig{}.png'.format(i), 'id': i - } for i in xrange(signatory_count) + } for i in range(signatory_count) ] @@ -141,7 +146,7 @@ class CommonCertificatesTestCase(ModuleStoreTestCase): 'signatories': signatories, 'version': 1, 'is_active': is_active - } for i in xrange(count) + } for i in range(count) ] self.course.certificates = {'certificates': certificates} @@ -402,11 +407,11 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) 'logo': '/logo_test1.png/' } test_org = organizations_api.add_organization(organization_data=test_organization_data) - organizations_api.add_organization_course(organization_data=test_org, course_id=unicode(self.course.id)) + organizations_api.add_organization_course(organization_data=test_org, course_id=six.text_type(self.course.id)) self._add_course_certificates(count=1, signatory_count=1, is_active=True) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) content = response.content.decode(response.charset) @@ -473,7 +478,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) 'logo': '/logo_test1.png' } test_org = organizations_api.add_organization(organization_data=test_organization_data) - organizations_api.add_organization_course(organization_data=test_org, course_id=unicode(self.course.id)) + organizations_api.add_organization_course(organization_data=test_org, course_id=six.text_type(self.course.id)) self._add_course_certificates(count=1, signatory_count=1, is_active=True) badge_class = get_completion_badge(course_id=self.course_id, user=self.user) BadgeAssertionFactory.create( @@ -488,7 +493,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url, HTTP_HOST='test.localhost') @@ -556,7 +561,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=2) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) self.assertIn(str(self.cert.verify_uuid), response.content) @@ -582,7 +587,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=2) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) # Validate certificate @@ -617,7 +622,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=2) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) @@ -637,7 +642,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=2) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) # Validate certificate @@ -659,7 +664,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=2) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) self.cert.invalidate() @@ -682,7 +687,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=2) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) user_language = 'fr' @@ -717,7 +722,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) self.assertIn("Invalid Certificate", response.content) @@ -729,7 +734,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=2) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) @@ -758,7 +763,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self.store.update_item(self.course, self.user.id) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) @@ -776,7 +781,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=2) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) self.course.display_coursenumber = "overridden_number" @@ -805,7 +810,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) # make sure response html has only one organization logo container for edX @@ -816,7 +821,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) self._add_course_certificates(count=1, signatory_count=0) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) self.assertNotIn('Signatory_Name 0', response.content) @@ -844,7 +849,7 @@ class CertificatesViewsTests(CommonCertificatesTestCase, CacheIsolationTestCase) test_url = get_certificate_url( user_id=self.user.id, - course_id=unicode(self.course.id) + course_id=six.text_type(self.course.id) ) response = self.client.get(test_url) self.assertNotIn('