From 7cca22964c09e8dafc313a400c428242404d527a Mon Sep 17 00:00:00 2001 From: Rohith PR Date: Sat, 15 May 2021 11:41:14 +0530 Subject: [PATCH] Fixed #32375 -- Started deprecation toward changing the default sitemap protocol to https. The default sitemap protocol, when it is built outside the context of a request, will be changed from 'http' to 'https' in Django 5.0. --- django/contrib/sitemaps/__init__.py | 12 ++++++++++++ docs/internals/deprecation.txt | 3 +++ docs/ref/contrib/sitemaps.txt | 5 +++++ docs/releases/4.0.txt | 3 +++ tests/sitemaps_tests/test_generic.py | 14 +++++++++++++- tests/sitemaps_tests/test_http.py | 6 +++++- 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py index 46a54ec8f2..7ad0f31c20 100644 --- a/django/contrib/sitemaps/__init__.py +++ b/django/contrib/sitemaps/__init__.py @@ -1,3 +1,4 @@ +import warnings from urllib.parse import urlencode from urllib.request import urlopen @@ -7,6 +8,7 @@ from django.core import paginator from django.core.exceptions import ImproperlyConfigured from django.urls import NoReverseMatch, reverse from django.utils import translation +from django.utils.deprecation import RemovedInDjango50Warning PING_URL = "https://www.google.com/webmasters/tools/ping" @@ -122,6 +124,16 @@ class Sitemap: def get_protocol(self, protocol=None): # Determine protocol + if self.protocol is None and protocol is None: + warnings.warn( + "The default sitemap protocol will be changed from 'http' to " + "'https' in Django 5.0. Set Sitemap.protocol to silence this " + "warning.", + category=RemovedInDjango50Warning, + stacklevel=2, + ) + # RemovedInDjango50Warning: when the deprecation ends, replace 'http' + # with 'https'. return self.protocol or protocol or 'http' def get_domain(self, site=None): diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 1b5eb35769..00e20b151a 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -24,6 +24,9 @@ details on these changes. * The default value of the ``USE_TZ`` setting will change from ``False`` to ``True``. +* The default sitemap protocol for sitemaps built outside the context of a + request will change from ``'http'`` to ``'https'``. + .. _deprecation-removed-in-4.1: 4.1 diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt index 43ad1212d9..9aaf08ca03 100644 --- a/docs/ref/contrib/sitemaps.txt +++ b/docs/ref/contrib/sitemaps.txt @@ -235,6 +235,11 @@ Note: sitemap was requested is used. If the sitemap is built outside the context of a request, the default is ``'http'``. + .. deprecated:: 4.0 + + The default protocol for sitemaps built outside the context of a + request will change from ``'http'`` to ``'https'`` in Django 5.0. + .. attribute:: Sitemap.limit **Optional.** diff --git a/docs/releases/4.0.txt b/docs/releases/4.0.txt index e1b55d8160..761b47b982 100644 --- a/docs/releases/4.0.txt +++ b/docs/releases/4.0.txt @@ -459,6 +459,9 @@ Miscellaneous * The undocumented ``django.utils.datetime_safe`` module is deprecated. +* The default sitemap protocol for sitemaps built outside the context of a + request will change from ``'http'`` to ``'https'`` in Django 5.0. + Features removed in 4.0 ======================= diff --git a/tests/sitemaps_tests/test_generic.py b/tests/sitemaps_tests/test_generic.py index ba24773b9e..d7d4908565 100644 --- a/tests/sitemaps_tests/test_generic.py +++ b/tests/sitemaps_tests/test_generic.py @@ -1,7 +1,8 @@ from datetime import datetime from django.contrib.sitemaps import GenericSitemap -from django.test import override_settings +from django.test import ignore_warnings, override_settings +from django.utils.deprecation import RemovedInDjango50Warning from .base import SitemapTestsBase from .models import TestModel @@ -70,6 +71,17 @@ class GenericViewsSitemapTests(SitemapTestsBase): with self.subTest(protocol=protocol): self.assertEqual(sitemap.get_protocol(protocol), protocol) + @ignore_warnings(category=RemovedInDjango50Warning) def test_get_protocol_default(self): sitemap = GenericSitemap({'queryset': None}) self.assertEqual(sitemap.get_protocol(), 'http') + + def test_get_protocol_default_warning(self): + sitemap = GenericSitemap({'queryset': None}) + msg = ( + "The default sitemap protocol will be changed from 'http' to " + "'https' in Django 5.0. Set Sitemap.protocol to silence this " + "warning." + ) + with self.assertWarnsMessage(RemovedInDjango50Warning, msg): + sitemap.get_protocol() diff --git a/tests/sitemaps_tests/test_http.py b/tests/sitemaps_tests/test_http.py index fadf88eb60..5dffe4017d 100644 --- a/tests/sitemaps_tests/test_http.py +++ b/tests/sitemaps_tests/test_http.py @@ -4,8 +4,9 @@ from datetime import date from django.contrib.sitemaps import Sitemap from django.contrib.sites.models import Site from django.core.exceptions import ImproperlyConfigured -from django.test import modify_settings, override_settings +from django.test import ignore_warnings, modify_settings, override_settings from django.utils import translation +from django.utils.deprecation import RemovedInDjango50Warning from django.utils.formats import localize from .base import SitemapTestsBase @@ -197,6 +198,7 @@ class HTTPSitemapTests(SitemapTestsBase): """ % date.today() self.assertXMLEqual(response.content.decode(), expected_content) + @ignore_warnings(category=RemovedInDjango50Warning) def test_sitemap_get_urls_no_site_1(self): """ Check we get ImproperlyConfigured if we don't pass a site object to @@ -207,6 +209,7 @@ class HTTPSitemapTests(SitemapTestsBase): Sitemap().get_urls() @modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}) + @ignore_warnings(category=RemovedInDjango50Warning) def test_sitemap_get_urls_no_site_2(self): """ Check we get ImproperlyConfigured when we don't pass a site object to @@ -216,6 +219,7 @@ class HTTPSitemapTests(SitemapTestsBase): with self.assertRaisesMessage(ImproperlyConfigured, self.use_sitemap_err_msg): Sitemap().get_urls() + @ignore_warnings(category=RemovedInDjango50Warning) def test_sitemap_item(self): """ Check to make sure that the raw item is included with each