From 4f6a6b57762d27dc4ff00afc21efbf5ed4c8d116 Mon Sep 17 00:00:00 2001 From: Awais Qureshi Date: Mon, 22 Feb 2021 21:41:54 +0500 Subject: [PATCH] BOM-2375 Run Pyupgrade on static replace folder. --- common/djangoapps/static_replace/__init__.py | 14 +- .../commands/clear_collectstatic_cache.py | 2 +- .../static_replace/migrations/0001_initial.py | 5 +- .../0002_assetexcludedextensionsconfig.py | 5 +- common/djangoapps/static_replace/models.py | 21 +- .../test/test_static_replace.py | 503 +++++++++--------- 6 files changed, 269 insertions(+), 281 deletions(-) diff --git a/common/djangoapps/static_replace/__init__.py b/common/djangoapps/static_replace/__init__.py index 04a240debb..6ec87706cb 100644 --- a/common/djangoapps/static_replace/__init__.py +++ b/common/djangoapps/static_replace/__init__.py @@ -3,12 +3,10 @@ import logging import re -import six from django.conf import settings from django.contrib.staticfiles import finders from django.contrib.staticfiles.storage import staticfiles_storage from opaque_keys.edx.locator import AssetLocator -from six import text_type from xmodule.contentstore.content import StaticContent @@ -39,7 +37,7 @@ def try_staticfiles_lookup(path): try: url = staticfiles_storage.url(path) except Exception as err: # lint-amnesty, pylint: disable=broad-except - log.warning("staticfiles_storage couldn't find path {0}: {1}".format( + log.warning("staticfiles_storage couldn't find path {}: {}".format( path, str(err))) # Just return the original path; don't kill everything. url = path @@ -85,7 +83,7 @@ def replace_course_urls(text, course_key): returns: text with the links replaced """ - course_id = text_type(course_key) + course_id = str(course_key) def replace_course_url(match): quote = match.group('quote') @@ -114,7 +112,7 @@ def process_static_urls(text, replacement_function, data_dir=None): # works for actual static assets and for magical course asset URLs.... full_url = prefix + rest - starts_with_static_url = full_url.startswith(six.text_type(settings.STATIC_URL)) + starts_with_static_url = full_url.startswith(str(settings.STATIC_URL)) starts_with_prefix = full_url.startswith(XBLOCK_STATIC_RESOURCE_PREFIX) contains_prefix = XBLOCK_STATIC_RESOURCE_PREFIX in full_url if starts_with_prefix or (starts_with_static_url and contains_prefix): @@ -123,7 +121,7 @@ def process_static_urls(text, replacement_function, data_dir=None): return replacement_function(original, prefix, quote, rest) return re.sub( - _url_replace_regex(u'(?:{static_url}|/static/)(?!{data_dir})'.format( + _url_replace_regex('(?:{static_url}|/static/)(?!{data_dir})'.format( static_url=settings.STATIC_URL, data_dir=data_dir )), @@ -192,7 +190,7 @@ def replace_static_urls(text, data_directory=None, course_id=None, static_asset_ try: exists_in_staticfiles_storage = staticfiles_storage.exists(rest) except Exception as err: # lint-amnesty, pylint: disable=broad-except - log.warning("staticfiles_storage couldn't find path {0}: {1}".format( + log.warning("staticfiles_storage couldn't find path {}: {}".format( rest, str(err))) if exists_in_staticfiles_storage: @@ -220,7 +218,7 @@ def replace_static_urls(text, data_directory=None, course_id=None, static_asset_ url = staticfiles_storage.url(course_path) # And if that fails, assume that it's course content, and add manually data directory except Exception as err: # lint-amnesty, pylint: disable=broad-except - log.warning("staticfiles_storage couldn't find path {0}: {1}".format( + log.warning("staticfiles_storage couldn't find path {}: {}".format( rest, str(err))) url = "".join([prefix, course_path]) diff --git a/common/djangoapps/static_replace/management/commands/clear_collectstatic_cache.py b/common/djangoapps/static_replace/management/commands/clear_collectstatic_cache.py index 7a1d5a2125..48655c671a 100644 --- a/common/djangoapps/static_replace/management/commands/clear_collectstatic_cache.py +++ b/common/djangoapps/static_replace/management/commands/clear_collectstatic_cache.py @@ -3,8 +3,8 @@ Django management command to clear the 'staticfiles' Django cache """ -from django.core.management.base import BaseCommand from django.core.cache import caches +from django.core.management.base import BaseCommand class Command(BaseCommand): diff --git a/common/djangoapps/static_replace/migrations/0001_initial.py b/common/djangoapps/static_replace/migrations/0001_initial.py index 875813fd7a..9cccfeaf36 100644 --- a/common/djangoapps/static_replace/migrations/0001_initial.py +++ b/common/djangoapps/static_replace/migrations/0001_initial.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - import django.db.models.deletion from django.conf import settings from django.db import migrations, models @@ -19,7 +16,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('change_date', models.DateTimeField(auto_now_add=True, verbose_name='Change date')), ('enabled', models.BooleanField(default=False, verbose_name='Enabled')), - ('base_url', models.TextField(help_text=u'The alternative hostname to serve static assets from. Should be in the form of hostname[:port].', blank=True)), + ('base_url', models.TextField(help_text='The alternative hostname to serve static assets from. Should be in the form of hostname[:port].', blank=True)), ('changed_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, editable=False, to=settings.AUTH_USER_MODEL, null=True, verbose_name='Changed by')), ], ), diff --git a/common/djangoapps/static_replace/migrations/0002_assetexcludedextensionsconfig.py b/common/djangoapps/static_replace/migrations/0002_assetexcludedextensionsconfig.py index d086230002..1a6f61fbc8 100644 --- a/common/djangoapps/static_replace/migrations/0002_assetexcludedextensionsconfig.py +++ b/common/djangoapps/static_replace/migrations/0002_assetexcludedextensionsconfig.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - import django.db.models.deletion from django.conf import settings from django.db import migrations, models @@ -20,7 +17,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('change_date', models.DateTimeField(auto_now_add=True, verbose_name='Change date')), ('enabled', models.BooleanField(default=False, verbose_name='Enabled')), - ('excluded_extensions', models.TextField(default=u'html', help_text=u'The file extensions to exclude from canonicalization. No leading period required. Values should be space separated i.e. "html svg css"')), + ('excluded_extensions', models.TextField(default='html', help_text='The file extensions to exclude from canonicalization. No leading period required. Values should be space separated i.e. "html svg css"')), ('changed_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, editable=False, to=settings.AUTH_USER_MODEL, null=True, verbose_name='Changed by')), ], ), diff --git a/common/djangoapps/static_replace/models.py b/common/djangoapps/static_replace/models.py index d905043ff2..d7f982368e 100644 --- a/common/djangoapps/static_replace/models.py +++ b/common/djangoapps/static_replace/models.py @@ -3,9 +3,6 @@ Models for static_replace """ -import six -from six.moves import map - from config_models.models import ConfigurationModel from django.db.models.fields import TextField from django.utils.encoding import python_2_unicode_compatible @@ -19,12 +16,12 @@ class AssetBaseUrlConfig(ConfigurationModel): .. no_pii: """ - class Meta(object): + class Meta: app_label = 'static_replace' base_url = TextField( blank=True, - help_text=u"The alternative hostname to serve static assets from. Should be in the form of hostname[:port]." + help_text="The alternative hostname to serve static assets from. Should be in the form of hostname[:port]." ) @classmethod @@ -33,10 +30,10 @@ class AssetBaseUrlConfig(ConfigurationModel): return cls.current().base_url def __repr__(self): - return ''.format(self.get_base_url()) + return f'' def __str__(self): - return six.text_type(repr(self)) + return str(repr(self)) @python_2_unicode_compatible @@ -47,12 +44,12 @@ class AssetExcludedExtensionsConfig(ConfigurationModel): .. no_pii: """ - class Meta(object): + class Meta: app_label = 'static_replace' excluded_extensions = TextField( - default=u'html', - help_text=u'The file extensions to exclude from canonicalization. No leading period required. ' + + default='html', + help_text='The file extensions to exclude from canonicalization. No leading period required. ' + 'Values should be space separated i.e. "html svg css"' ) @@ -63,7 +60,7 @@ class AssetExcludedExtensionsConfig(ConfigurationModel): return list(map(add_period, cls.current().excluded_extensions.split())) def __repr__(self): - return ''.format(self.get_excluded_extensions()) + return f'' def __str__(self): - return six.text_type(repr(self)) + return str(repr(self)) diff --git a/common/djangoapps/static_replace/test/test_static_replace.py b/common/djangoapps/static_replace/test/test_static_replace.py index fde36d64e6..0afe3ee35f 100644 --- a/common/djangoapps/static_replace/test/test_static_replace.py +++ b/common/djangoapps/static_replace/test/test_static_replace.py @@ -1,18 +1,17 @@ -# -*- coding: utf-8 -*- """Tests for static_replace""" import re -from six import BytesIO -from six.moves.urllib.parse import parse_qsl, urlparse, urlunparse +from unittest.mock import Mock, patch import ddt import pytest from django.test import override_settings from django.utils.http import urlencode, urlquote -from mock import Mock, patch from opaque_keys.edx.keys import CourseKey from PIL import Image +from io import BytesIO +from urllib.parse import parse_qsl, urlparse, urlunparse from common.djangoapps.static_replace import ( _url_replace_regex, @@ -68,7 +67,7 @@ def test_process_url(): def test_process_url_data_dir_exists(): - base = '"/static/{data_dir}/file.png"'.format(data_dir=DATA_DIRECTORY) + base = f'"/static/{DATA_DIRECTORY}/file.png"' def processor(original, prefix, quote, rest): # pylint: disable=unused-argument return quote + 'test' + rest + quote @@ -119,7 +118,7 @@ def test_mongo_filestore(mock_get_excluded_extensions, mock_get_base_url, mock_m mock_modulestore.return_value = Mock(MongoModuleStore) mock_static_content.get_canonicalized_asset_path.return_value = "c4x://mock_url" - mock_get_base_url.return_value = u'' + mock_get_base_url.return_value = '' mock_get_excluded_extensions.return_value = ['foobar'] # No namespace => no change to path @@ -129,7 +128,7 @@ def test_mongo_filestore(mock_get_excluded_extensions, mock_get_base_url, mock_m assert '"' + mock_static_content.get_canonicalized_asset_path.return_value + '"' == \ replace_static_urls(STATIC_SOURCE, DATA_DIRECTORY, course_id=COURSE_KEY) - mock_static_content.get_canonicalized_asset_path.assert_called_once_with(COURSE_KEY, 'file.png', u'', ['foobar']) + mock_static_content.get_canonicalized_asset_path.assert_called_once_with(COURSE_KEY, 'file.png', '', ['foobar']) @patch('common.djangoapps.static_replace.settings', autospec=True) @@ -193,9 +192,9 @@ def test_static_paths_out(mock_modulestore, mock_storage): raw_url = '/static/js/capa/protex/protex.nocache.js?raw' xblock_url = '/static/xblock/resources/babys_first.lil_xblock/public/images/pacifier.png' # xss-lint: disable=python-wrap-html - pre_text = 'EMBED src ="{}" xblock={} text