From e40d259a0973372f684c423ca4ba76eba559a2cf Mon Sep 17 00:00:00 2001 From: Awais Qureshi Date: Thu, 11 Mar 2021 14:49:15 +0500 Subject: [PATCH] BOM-2442 pyupgrade in course-search and discount. --- .../course_search/views/course_search.py | 6 +++--- openedx/features/discounts/admin.py | 1 - openedx/features/discounts/applicability.py | 5 ++--- openedx/features/discounts/apps.py | 1 - .../discounts/migrations/0001_initial.py | 1 - .../migrations/0002_auto_20191022_1720.py | 1 - .../discounts/tests/test_applicability.py | 7 +++---- .../tests/test_discount_restriction_models.py | 2 +- openedx/features/discounts/tests/test_utils.py | 16 ++++++++-------- openedx/features/discounts/tests/test_views.py | 7 +++---- openedx/features/discounts/urls.py | 4 ++-- openedx/features/discounts/utils.py | 8 ++++---- 12 files changed, 26 insertions(+), 33 deletions(-) diff --git a/openedx/features/course_search/views/course_search.py b/openedx/features/course_search/views/course_search.py index 22762c1fca..d9c2f11ed8 100644 --- a/openedx/features/course_search/views/course_search.py +++ b/openedx/features/course_search/views/course_search.py @@ -33,10 +33,10 @@ class CourseSearchView(CourseTabView): """ Displays the home page for the specified course. """ - return super(CourseSearchView, self).get(request, course_id, 'courseware', **kwargs) # lint-amnesty, pylint: disable=super-with-arguments + return super().get(request, course_id, 'courseware', **kwargs) def render_to_fragment(self, request, course=None, tab=None, **kwargs): # lint-amnesty, pylint: disable=arguments-differ, unused-argument - course_id = six.text_type(course.id) + course_id = str(course.id) home_fragment_view = CourseSearchFragmentView() return home_fragment_view.render_to_fragment(request, course_id=course_id, **kwargs) @@ -53,7 +53,7 @@ class CourseSearchFragmentView(EdxFragmentView): course_key = CourseKey.from_string(course_id) course = get_course_overview_with_access(request.user, 'load', course_key, check_if_enrolled=True) course_url_name = default_course_url_name(course.id) - course_url = reverse(course_url_name, kwargs={'course_id': six.text_type(course.id)}) + course_url = reverse(course_url_name, kwargs={'course_id': str(course.id)}) # Render the course home fragment context = { diff --git a/openedx/features/discounts/admin.py b/openedx/features/discounts/admin.py index 35b0003826..57a8e1b5da 100644 --- a/openedx/features/discounts/admin.py +++ b/openedx/features/discounts/admin.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ Django Admin pages for DiscountRestrictionConfig. """ diff --git a/openedx/features/discounts/applicability.py b/openedx/features/discounts/applicability.py index c681d811d0..e11f288c01 100644 --- a/openedx/features/discounts/applicability.py +++ b/openedx/features/discounts/applicability.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ Contains code related to computing discount percentage and discount applicability. @@ -36,8 +35,8 @@ from common.djangoapps.track import segment # .. toggle_tickets: REVEM-282 # .. toggle_warnings: This temporary feature toggle does not have a target removal date. DISCOUNT_APPLICABILITY_FLAG = LegacyWaffleFlag( - waffle_namespace=LegacyWaffleFlagNamespace(name=u'discounts'), - flag_name=u'enable_discounting', + waffle_namespace=LegacyWaffleFlagNamespace(name='discounts'), + flag_name='enable_discounting', module_name=__name__, ) diff --git a/openedx/features/discounts/apps.py b/openedx/features/discounts/apps.py index 74374d59eb..21c6f69930 100644 --- a/openedx/features/discounts/apps.py +++ b/openedx/features/discounts/apps.py @@ -1,4 +1,3 @@ - """ Discounts application configuration """ diff --git a/openedx/features/discounts/migrations/0001_initial.py b/openedx/features/discounts/migrations/0001_initial.py index 113d939304..6cdbb71ff6 100644 --- a/openedx/features/discounts/migrations/0001_initial.py +++ b/openedx/features/discounts/migrations/0001_initial.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.20 on 2019-05-31 20:08 diff --git a/openedx/features/discounts/migrations/0002_auto_20191022_1720.py b/openedx/features/discounts/migrations/0002_auto_20191022_1720.py index 0364d5ab8a..32d2e1c3cc 100644 --- a/openedx/features/discounts/migrations/0002_auto_20191022_1720.py +++ b/openedx/features/discounts/migrations/0002_auto_20191022_1720.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Generated by Django 1.11.25 on 2019-10-22 17:20 diff --git a/openedx/features/discounts/tests/test_applicability.py b/openedx/features/discounts/tests/test_applicability.py index 1a6a584ec1..e575701c76 100644 --- a/openedx/features/discounts/tests/test_applicability.py +++ b/openedx/features/discounts/tests/test_applicability.py @@ -1,5 +1,4 @@ """Tests of openedx.features.discounts.applicability""" -# -*- coding: utf-8 -*- from datetime import datetime, timedelta @@ -11,7 +10,7 @@ from django.contrib.sites.models import Site from django.utils.timezone import now from edx_toggles.toggles.testutils import override_waffle_flag from enterprise.models import EnterpriseCustomer, EnterpriseCustomerUser -from mock import Mock, patch +from unittest.mock import Mock, patch from common.djangoapps.course_modes.models import CourseMode from common.djangoapps.course_modes.tests.factories import CourseModeFactory @@ -35,12 +34,12 @@ class TestApplicability(ModuleStoreTestCase): """ def setUp(self): - super(TestApplicability, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.site, _ = Site.objects.get_or_create(domain='example.com') self.user = UserFactory.create() self.course = CourseFactory.create(run='test', display_name='test') CourseModeFactory.create(course_id=self.course.id, mode_slug='verified') - now_time = datetime.now(tz=pytz.UTC).strftime(u"%Y-%m-%d %H:%M:%S%z") + now_time = datetime.now(tz=pytz.UTC).strftime("%Y-%m-%d %H:%M:%S%z") ExperimentData.objects.create( user=self.user, experiment_id=REV1008_EXPERIMENT_ID, key=str(self.course.id), value=now_time ) diff --git a/openedx/features/discounts/tests/test_discount_restriction_models.py b/openedx/features/discounts/tests/test_discount_restriction_models.py index 12714c4453..38815a660f 100644 --- a/openedx/features/discounts/tests/test_discount_restriction_models.py +++ b/openedx/features/discounts/tests/test_discount_restriction_models.py @@ -24,7 +24,7 @@ class TestDiscountRestrictionConfig(CacheIsolationTestCase): def setUp(self): self.course_overview = CourseOverviewFactory.create() self.user = UserFactory.create() - super(TestDiscountRestrictionConfig, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() @ddt.data(True, False) def test_disabled_for_course_stacked_config( diff --git a/openedx/features/discounts/tests/test_utils.py b/openedx/features/discounts/tests/test_utils.py index 695b001df8..15a3b26005 100644 --- a/openedx/features/discounts/tests/test_utils.py +++ b/openedx/features/discounts/tests/test_utils.py @@ -1,7 +1,7 @@ """ Tests of the openedx.features.discounts.utils module. """ -from mock import patch, Mock +from unittest.mock import patch, Mock import ddt import six @@ -33,7 +33,7 @@ class TestStrikeoutPrice(TestCase): ): content, has_discount = utils.format_strikeout_price(Mock(name='user'), Mock(name='course')) - assert six.text_type(content) == u"$100" + assert str(content) == "$100" assert not has_discount @ddt.data((15, 100, "$100", "$85",), (50, 50, "$50", "$25"), (10, 99, "$99", "$89.10")) @@ -47,12 +47,12 @@ class TestStrikeoutPrice(TestCase): ): content, has_discount = utils.format_strikeout_price(Mock(name='user'), Mock(name='course')) - assert six.text_type(content) == ( - u"" - u"Original price: {original_price}, discount price: " - u"" - u"{discount_price} " - u"" + assert str(content) == ( + "" + "Original price: {original_price}, discount price: " + "" + "{discount_price} " + "" ).format(original_price=formatted_base_price, discount_price=final_price) assert has_discount diff --git a/openedx/features/discounts/tests/test_views.py b/openedx/features/discounts/tests/test_views.py index bf8f564dbc..5a735375c3 100644 --- a/openedx/features/discounts/tests/test_views.py +++ b/openedx/features/discounts/tests/test_views.py @@ -1,5 +1,4 @@ """Tests of openedx.features.discounts.views""" -# -*- coding: utf-8 -*- import jwt @@ -19,20 +18,20 @@ class TestCourseUserDiscount(ModuleStoreTestCase): """ def setUp(self): - super(TestCourseUserDiscount, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments + super().setUp() self.user = UserFactory.create() self.course = CourseFactory.create(run='test', display_name='test') self.client = Client() self.url = reverse( 'api_discounts:course_user_discount', - kwargs={'course_key_string': six.text_type(self.course.id)} + kwargs={'course_key_string': str(self.course.id)} ) def test_url(self): """ Test that the url hasn't changed """ - assert self.url == ('/api/discounts/course/' + six.text_type(self.course.id)) + assert self.url == ('/api/discounts/course/' + str(self.course.id)) def test_course_user_discount(self): """ diff --git a/openedx/features/discounts/urls.py b/openedx/features/discounts/urls.py index 52e12f7024..f3a9e29186 100644 --- a/openedx/features/discounts/urls.py +++ b/openedx/features/discounts/urls.py @@ -9,8 +9,8 @@ from django.conf.urls import url from .views import CourseUserDiscount, CourseUserDiscountWithUserParam urlpatterns = [ - url(r'^course/{}'.format(settings.COURSE_KEY_PATTERN), CourseUserDiscount.as_view(), name='course_user_discount'), - url(r'^user/(?P[^/]*)/course/{}'.format(settings.COURSE_KEY_PATTERN), + url(fr'^course/{settings.COURSE_KEY_PATTERN}', CourseUserDiscount.as_view(), name='course_user_discount'), + url(fr'^user/(?P[^/]*)/course/{settings.COURSE_KEY_PATTERN}', CourseUserDiscountWithUserParam.as_view(), name='course_user_discount_with_param'), ] diff --git a/openedx/features/discounts/utils.py b/openedx/features/discounts/utils.py index afd305b8a1..c5a3e71036 100644 --- a/openedx/features/discounts/utils.py +++ b/openedx/features/discounts/utils.py @@ -43,7 +43,7 @@ def offer_banner_wrapper(user, block, view, frag, context): # pylint: disable=W # Course content must be escaped to render correctly due to the way the # way the XBlock rendering works. Transforming the safe markup to unicode # escapes correctly. - offer_banner_fragment.content = six.text_type(offer_banner_fragment.content) + offer_banner_fragment.content = str(offer_banner_fragment.content) offer_banner_fragment.add_content(frag.content) offer_banner_fragment.add_fragment_resources(frag) @@ -69,9 +69,9 @@ def _get_discount_prices(user, course, assume_discount=False): discounted_price = base_price * ((100.0 - percentage) / 100) if discounted_price: # leave 0 prices alone, as format_course_price below will adjust to 'Free' if discounted_price == int(discounted_price): - discounted_price = '{:0.0f}'.format(discounted_price) + discounted_price = f'{discounted_price:0.0f}' else: - discounted_price = '{:0.2f}'.format(discounted_price) + discounted_price = f'{discounted_price:0.2f}' return format_course_price(base_price), format_course_price(discounted_price), percentage else: @@ -215,7 +215,7 @@ def get_first_purchase_offer_banner_fragment_from_key(user, course_key): shouldn't show a first purchase offer message for this user. """ request_cache = RequestCache('get_first_purchase_offer_banner_fragment_from_key') - cache_key = 'html:{},{}'.format(user.id, course_key) + cache_key = f'html:{user.id},{course_key}' cache_response = request_cache.get_cached_response(cache_key) if cache_response.is_found: cached_html = cache_response.value