Silenced some deprecation warnings in contrib.sitemaps; refs #22384.

This commit is contained in:
Loic Bistuer 2014-09-19 05:40:51 +07:00
parent b23d47412c
commit 45840927d3
4 changed files with 70 additions and 17 deletions

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
import os import os
from datetime import date from datetime import date
from unittest import skipUnless from unittest import skipUnless
import warnings
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
@ -10,6 +11,7 @@ from django.contrib.sitemaps import Sitemap, GenericSitemap
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 modify_settings, override_settings
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.formats import localize from django.utils.formats import localize
from django.utils._os import upath from django.utils._os import upath
from django.utils.translation import activate, deactivate from django.utils.translation import activate, deactivate
@ -21,7 +23,15 @@ class HTTPSitemapTests(SitemapTestsBase):
def test_simple_sitemap_index(self): def test_simple_sitemap_index(self):
"A simple sitemap index can be rendered" "A simple sitemap index can be rendered"
response = self.client.get('/simple/index.xml') with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
# The URL for views.sitemap in tests/urls/http.py has been updated
# with a name but since reversing by Python path is tried first
# before reversing by name and works since we're giving
# name='django.contrib.sitemaps.views.sitemap', we need to silence
# the erroneous warning until reversing by dotted path is removed.
# The test will work without modification when it's removed.
response = self.client.get('/simple/index.xml')
expected_content = """<?xml version="1.0" encoding="UTF-8"?> expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap> <sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
@ -34,7 +44,15 @@ class HTTPSitemapTests(SitemapTestsBase):
) )
def test_simple_sitemap_custom_index(self): def test_simple_sitemap_custom_index(self):
"A simple sitemap index can be rendered with a custom template" "A simple sitemap index can be rendered with a custom template"
response = self.client.get('/simple/custom-index.xml') with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
# The URL for views.sitemap in tests/urls/http.py has been updated
# with a name but since reversing by Python path is tried first
# before reversing by name and works since we're giving
# name='django.contrib.sitemaps.views.sitemap', we need to silence
# the erroneous warning until reversing by dotted path is removed.
# The test will work without modification when it's removed.
response = self.client.get('/simple/custom-index.xml')
expected_content = """<?xml version="1.0" encoding="UTF-8"?> expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a customised template --> <!-- This is a customised template -->
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@ -177,7 +195,15 @@ class HTTPSitemapTests(SitemapTestsBase):
self.assertXMLEqual(response.content.decode('utf-8'), expected_content) self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
def test_x_robots_sitemap(self): def test_x_robots_sitemap(self):
response = self.client.get('/simple/index.xml') with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
# The URL for views.sitemap in tests/urls/http.py has been updated
# with a name but since reversing by Python path is tried first
# before reversing by name and works since we're giving
# name='django.contrib.sitemaps.views.sitemap', we need to silence
# the erroneous warning until reversing by dotted path is removed.
# The test will work without modification when it's removed.
response = self.client.get('/simple/index.xml')
self.assertEqual(response['X-Robots-Tag'], 'noindex, noodp, noarchive') self.assertEqual(response['X-Robots-Tag'], 'noindex, noodp, noarchive')
response = self.client.get('/simple/sitemap.xml') response = self.client.get('/simple/sitemap.xml')

View File

@ -15,7 +15,15 @@ class HTTPSSitemapTests(SitemapTestsBase):
def test_secure_sitemap_index(self): def test_secure_sitemap_index(self):
"A secure sitemap index can be rendered" "A secure sitemap index can be rendered"
response = self.client.get('/secure/index.xml') with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
# The URL for views.sitemap in tests/urls/https.py has been updated
# with a name but since reversing by Python path is tried first
# before reversing by name and works since we're giving
# name='django.contrib.sitemaps.views.sitemap', we need to silence
# the erroneous warning until reversing by dotted path is removed.
# The test will work without modification when it's removed.
response = self.client.get('/secure/index.xml')
expected_content = """<?xml version="1.0" encoding="UTF-8"?> expected_content = """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/secure/sitemap-simple.xml</loc></sitemap> <sitemap><loc>%s/secure/sitemap-simple.xml</loc></sitemap>
@ -42,7 +50,7 @@ class HTTPSDetectionSitemapTests(SitemapTestsBase):
"A sitemap index requested in HTTPS is rendered with HTTPS links" "A sitemap index requested in HTTPS is rendered with HTTPS links"
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RemovedInDjango20Warning) warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
# The URL for views.sitemap in tests/urls/http.py has been updated # The URL for views.sitemap in tests/urls/https.py has been updated
# with a name but since reversing by Python path is tried first # with a name but since reversing by Python path is tried first
# before reversing by name and works since we're giving # before reversing by name and works since we're giving
# name='django.contrib.sitemaps.views.sitemap', we need to silence # name='django.contrib.sitemaps.views.sitemap', we need to silence

View File

@ -100,20 +100,38 @@ urlpatterns = [
url(r'^simple/custom-index\.xml$', views.index, url(r'^simple/custom-index\.xml$', views.index,
{'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}), {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}),
url(r'^simple/sitemap-(?P<section>.+)\.xml$', views.sitemap, url(r'^simple/sitemap-(?P<section>.+)\.xml$', views.sitemap,
{'sitemaps': simple_sitemaps}, name='django.contrib.sitemaps.views.sitemap'), {'sitemaps': simple_sitemaps},
url(r'^simple/sitemap\.xml$', views.sitemap, {'sitemaps': simple_sitemaps}), name='django.contrib.sitemaps.views.sitemap'),
url(r'^simple/i18n\.xml$', views.sitemap, {'sitemaps': simple_i18nsitemaps}), url(r'^simple/sitemap\.xml$', views.sitemap,
{'sitemaps': simple_sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^simple/i18n\.xml$', views.sitemap,
{'sitemaps': simple_i18nsitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^simple/custom-sitemap\.xml$', views.sitemap, url(r'^simple/custom-sitemap\.xml$', views.sitemap,
{'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}), {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'},
url(r'^empty/sitemap\.xml$', views.sitemap, {'sitemaps': empty_sitemaps}), name='django.contrib.sitemaps.views.sitemap'),
url(r'^lastmod/sitemap\.xml$', views.sitemap, {'sitemaps': fixed_lastmod_sitemaps}), url(r'^empty/sitemap\.xml$', views.sitemap,
url(r'^lastmod-mixed/sitemap\.xml$', views.sitemap, {'sitemaps': fixed_lastmod__mixed_sitemaps}), {'sitemaps': empty_sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^lastmod/sitemap\.xml$', views.sitemap,
{'sitemaps': fixed_lastmod_sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^lastmod-mixed/sitemap\.xml$', views.sitemap,
{'sitemaps': fixed_lastmod__mixed_sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^lastmod/date-sitemap.xml$', views.sitemap, url(r'^lastmod/date-sitemap.xml$', views.sitemap,
{'sitemaps': {'date-sitemap': DateSiteMap}}), {'sitemaps': {'date-sitemap': DateSiteMap}},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^lastmod/tz-sitemap.xml$', views.sitemap, url(r'^lastmod/tz-sitemap.xml$', views.sitemap,
{'sitemaps': {'tz-sitemap': TimezoneSiteMap}}), {'sitemaps': {'tz-sitemap': TimezoneSiteMap}},
url(r'^generic/sitemap\.xml$', views.sitemap, {'sitemaps': generic_sitemaps}), name='django.contrib.sitemaps.views.sitemap'),
url(r'^flatpages/sitemap\.xml$', views.sitemap, {'sitemaps': flatpage_sitemaps}), url(r'^generic/sitemap\.xml$', views.sitemap,
{'sitemaps': generic_sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^flatpages/sitemap\.xml$', views.sitemap,
{'sitemaps': flatpage_sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
url(r'^cached/index\.xml$', cache_page(1)(views.index), url(r'^cached/index\.xml$', cache_page(1)(views.index),
{'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}), {'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}),
url(r'^cached/sitemap-(?P<section>.+)\.xml', cache_page(1)(views.sitemap), url(r'^cached/sitemap-(?P<section>.+)\.xml', cache_page(1)(views.sitemap),

View File

@ -14,5 +14,6 @@ secure_sitemaps = {
urlpatterns = [ urlpatterns = [
url(r'^secure/index\.xml$', views.index, {'sitemaps': secure_sitemaps}), url(r'^secure/index\.xml$', views.index, {'sitemaps': secure_sitemaps}),
url(r'^secure/sitemap-(?P<section>.+)\.xml$', views.sitemap, url(r'^secure/sitemap-(?P<section>.+)\.xml$', views.sitemap,
{'sitemaps': secure_sitemaps}), {'sitemaps': secure_sitemaps},
name='django.contrib.sitemaps.views.sitemap'),
] ]