Fix the DeprecationWarning for unescape (#23936)

This PR solves the DeprecationWarning mentioned in:
https://build.testeng.edx.org/job/edx-platform-python-pipeline-master/warning_5freport_5fall_2ehtml/

HTMLParser was renamed in html.parser in Python3:
https://docs.python.org/2/library/htmlparser.html#module-HTMLParser

* html_parser.HTMLParser().unescape from six.moves has been deprecated

* instead use html.unescape from Python3

Documentation for unescape in Python3:
https://docs.python.org/3/library/html.html#html.unescape

- html_parser from six.moves has been deprecated

- instead use html.parser from Python3

- Order imports using isort

- Delete unused import crum
This commit is contained in:
Leonardo Martinez
2020-06-19 08:27:45 -05:00
committed by GitHub
parent 137fd0a96e
commit b4fee68283
4 changed files with 27 additions and 30 deletions

View File

@@ -4,18 +4,24 @@ Tests courseware views.py
"""
import html
import itertools
import json
import unittest
from datetime import datetime, timedelta
from pytz import utc
from uuid import uuid4
import crum
import ddt
import six
from markupsafe import escape
from mock import MagicMock, PropertyMock, call, create_autospec, patch
from pytz import UTC, utc
from six import text_type
from six.moves import range
from six.moves.urllib.parse import quote, urlencode
from completion.test_utils import CompletionWaffleTestMixin
from crum import set_current_request
import ddt
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.http import Http404, HttpResponseBadRequest
@@ -24,32 +30,17 @@ from django.test.client import Client
from django.test.utils import override_settings
from django.urls import reverse, reverse_lazy
from freezegun import freeze_time
from markupsafe import escape
from milestones.tests.utils import MilestonesTestCaseMixin
from mock import MagicMock, PropertyMock, call, create_autospec, patch
from opaque_keys.edx.keys import CourseKey, UsageKey
from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator
from pytz import UTC
from six import text_type
from six.moves import range
from six.moves.html_parser import HTMLParser
from six.moves.urllib.parse import quote, urlencode
from web_fragments.fragment import Fragment
from xblock.core import XBlock
from xblock.fields import Scope, String
import lms.djangoapps.courseware.views.views as views
from capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory
from course_modes.models import CourseMode
from course_modes.tests.factories import CourseModeFactory
from lms.djangoapps.courseware.access_utils import check_course_open_for_learner
from lms.djangoapps.courseware.model_data import FieldDataCache, set_score
from lms.djangoapps.courseware.module_render import get_module, handle_xblock_callback
from lms.djangoapps.courseware.tests.factories import GlobalStaffFactory, RequestFactoryNoCsrf, StudentModuleFactory
from lms.djangoapps.courseware.tests.helpers import get_expiration_banner_text
from lms.djangoapps.courseware.testutils import RenderXBlockTestMixin
from lms.djangoapps.courseware.url_helpers import get_redirect_url
from lms.djangoapps.courseware.user_state_client import DjangoXBlockUserStateClient
from lms.djangoapps.certificates import api as certs_api
from lms.djangoapps.certificates.models import (
CertificateGenerationConfiguration,
@@ -59,17 +50,23 @@ from lms.djangoapps.certificates.models import (
from lms.djangoapps.certificates.tests.factories import CertificateInvalidationFactory, GeneratedCertificateFactory
from lms.djangoapps.commerce.models import CommerceConfiguration
from lms.djangoapps.commerce.utils import EcommerceService
from lms.djangoapps.courseware.views.index import show_courseware_mfe_link
from lms.djangoapps.courseware.access_utils import check_course_open_for_learner
from lms.djangoapps.courseware.model_data import FieldDataCache, set_score
from lms.djangoapps.courseware.module_render import get_module, handle_xblock_callback
from lms.djangoapps.courseware.tests.factories import GlobalStaffFactory, RequestFactoryNoCsrf, StudentModuleFactory
from lms.djangoapps.courseware.tests.helpers import get_expiration_banner_text
from lms.djangoapps.courseware.testutils import RenderXBlockTestMixin
from lms.djangoapps.courseware.toggles import (
COURSEWARE_MICROFRONTEND_COURSE_TEAM_PREVIEW,
REDIRECT_TO_COURSEWARE_MICROFRONTEND,
REDIRECT_TO_COURSEWARE_MICROFRONTEND
)
from lms.djangoapps.courseware.url_helpers import get_microfrontend_url
from lms.djangoapps.courseware.url_helpers import get_microfrontend_url, get_redirect_url
from lms.djangoapps.courseware.user_state_client import DjangoXBlockUserStateClient
from lms.djangoapps.courseware.views.index import show_courseware_mfe_link
from lms.djangoapps.grades.config.waffle import ASSUME_ZERO_GRADE_IF_ABSENT
from lms.djangoapps.grades.config.waffle import waffle as grades_waffle
from lms.djangoapps.verify_student.models import VerificationDeadline
from lms.djangoapps.verify_student.services import IDVerificationService
from opaque_keys.edx.keys import CourseKey, UsageKey
from openedx.core.djangoapps.catalog.tests.factories import CourseFactory as CatalogCourseFactory
from openedx.core.djangoapps.catalog.tests.factories import CourseRunFactory, ProgramFactory
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
@@ -100,7 +97,6 @@ from xmodule.course_module import COURSE_VISIBILITY_PRIVATE, COURSE_VISIBILITY_P
from xmodule.graders import ShowCorrectness
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.tests.django_utils import (
TEST_DATA_MIXED_MODULESTORE,
TEST_DATA_SPLIT_MODULESTORE,
@@ -694,7 +690,7 @@ class ViewsTestCase(BaseViewsTestCase):
'location': six.text_type(usage_key),
})
response = self.client.get(url)
response_content = HTMLParser().unescape(response.content.decode('utf-8'))
response_content = html.unescape(response.content.decode('utf-8'))
# We have update the state 4 times: twice to change content, and twice
# to set the scores. We'll check that the identifying content from each is

View File

@@ -6,6 +6,7 @@ This file contains utility functions which will responsible for sending emails.
import logging
import os
import uuid
import html
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
@@ -106,8 +107,7 @@ def send_credit_notifications(username, course_key):
cur_text = cur_file.read()
# use html parser to unescape html characters which are changed
# by the 'pynliner' while adding inline css to html content
html_parser = six.moves.html_parser.HTMLParser()
email_body_content = html_parser.unescape(with_inline_css(cur_text))
email_body_content = html.unescape(with_inline_css(cur_text))
# cache the email body content before rendering it since the
# email context will change for each user e.g., 'full_name'
cache.set('credit.email.css-email-body', email_body_content, settings.CREDIT_NOTIFICATION_CACHE_TIMEOUT)

View File

@@ -6,6 +6,7 @@ Tests for js_utils.py
import json
import re
import html
from unittest import TestCase
import six
@@ -184,9 +185,8 @@ class TestJSUtils(TestCase):
should be parseable into a near equivalent to test_dict.
"""
html_parser = six.moves.html_parser.HTMLParser()
expected_json = html_parser.unescape(expected_json_for_html_string)
expected_json = html.unescape(expected_json_for_html_string)
parsed_expected_dict = json.loads(expected_json)
# tuples become arrays in json, so it is parsed to a list that is
# switched back to a tuple before comparing

View File

@@ -41,6 +41,7 @@ import json
import sys
import unittest
from datetime import datetime, timedelta
import html
import mock
import pytz
@@ -466,7 +467,7 @@ class XBlockTestCase(XBlockStudentTestCaseMixin,
print("Dice 2", repr(xblock_html.split('<')[1].split('>')[1]), file=sys.stderr)
raise
# Finally, we unescape the contents
decoded_html = six.moves.html_parser.HTMLParser().unescape(escaped_html).strip()
decoded_html = html.unescape(escaped_html).strip()
return decoded_html