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.
This commit is contained in:
Rohith PR 2021-05-15 11:41:14 +05:30 committed by Mariusz Felisiak
parent 56003b21ea
commit 7cca22964c
6 changed files with 41 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import warnings
from urllib.parse import urlencode from urllib.parse import urlencode
from urllib.request import urlopen from urllib.request import urlopen
@ -7,6 +8,7 @@ from django.core import paginator
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.urls import NoReverseMatch, reverse from django.urls import NoReverseMatch, reverse
from django.utils import translation from django.utils import translation
from django.utils.deprecation import RemovedInDjango50Warning
PING_URL = "https://www.google.com/webmasters/tools/ping" PING_URL = "https://www.google.com/webmasters/tools/ping"
@ -122,6 +124,16 @@ class Sitemap:
def get_protocol(self, protocol=None): def get_protocol(self, protocol=None):
# Determine protocol # 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' return self.protocol or protocol or 'http'
def get_domain(self, site=None): def get_domain(self, site=None):

View File

@ -24,6 +24,9 @@ details on these changes.
* The default value of the ``USE_TZ`` setting will change from ``False`` to * The default value of the ``USE_TZ`` setting will change from ``False`` to
``True``. ``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: .. _deprecation-removed-in-4.1:
4.1 4.1

View File

@ -235,6 +235,11 @@ Note:
sitemap was requested is used. If the sitemap is built outside the sitemap was requested is used. If the sitemap is built outside the
context of a request, the default is ``'http'``. 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 .. attribute:: Sitemap.limit
**Optional.** **Optional.**

View File

@ -459,6 +459,9 @@ Miscellaneous
* The undocumented ``django.utils.datetime_safe`` module is deprecated. * 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 Features removed in 4.0
======================= =======================

View File

@ -1,7 +1,8 @@
from datetime import datetime from datetime import datetime
from django.contrib.sitemaps import GenericSitemap 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 .base import SitemapTestsBase
from .models import TestModel from .models import TestModel
@ -70,6 +71,17 @@ class GenericViewsSitemapTests(SitemapTestsBase):
with self.subTest(protocol=protocol): with self.subTest(protocol=protocol):
self.assertEqual(sitemap.get_protocol(protocol), protocol) self.assertEqual(sitemap.get_protocol(protocol), protocol)
@ignore_warnings(category=RemovedInDjango50Warning)
def test_get_protocol_default(self): def test_get_protocol_default(self):
sitemap = GenericSitemap({'queryset': None}) sitemap = GenericSitemap({'queryset': None})
self.assertEqual(sitemap.get_protocol(), 'http') 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()

View File

@ -4,8 +4,9 @@ from datetime import date
from django.contrib.sitemaps import Sitemap from django.contrib.sitemaps import Sitemap
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.exceptions import ImproperlyConfigured 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 import translation
from django.utils.deprecation import RemovedInDjango50Warning
from django.utils.formats import localize from django.utils.formats import localize
from .base import SitemapTestsBase from .base import SitemapTestsBase
@ -197,6 +198,7 @@ class HTTPSitemapTests(SitemapTestsBase):
""" % date.today() """ % date.today()
self.assertXMLEqual(response.content.decode(), expected_content) self.assertXMLEqual(response.content.decode(), expected_content)
@ignore_warnings(category=RemovedInDjango50Warning)
def test_sitemap_get_urls_no_site_1(self): def test_sitemap_get_urls_no_site_1(self):
""" """
Check we get ImproperlyConfigured if we don't pass a site object to Check we get ImproperlyConfigured if we don't pass a site object to
@ -207,6 +209,7 @@ class HTTPSitemapTests(SitemapTestsBase):
Sitemap().get_urls() Sitemap().get_urls()
@modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'}) @modify_settings(INSTALLED_APPS={'remove': 'django.contrib.sites'})
@ignore_warnings(category=RemovedInDjango50Warning)
def test_sitemap_get_urls_no_site_2(self): def test_sitemap_get_urls_no_site_2(self):
""" """
Check we get ImproperlyConfigured when we don't pass a site object to 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): with self.assertRaisesMessage(ImproperlyConfigured, self.use_sitemap_err_msg):
Sitemap().get_urls() Sitemap().get_urls()
@ignore_warnings(category=RemovedInDjango50Warning)
def test_sitemap_item(self): def test_sitemap_item(self):
""" """
Check to make sure that the raw item is included with each Check to make sure that the raw item is included with each