2010-12-13 06:56:29 +08:00
|
|
|
import os
|
2010-08-27 23:16:04 +08:00
|
|
|
from datetime import date
|
|
|
|
from django.conf import settings
|
2010-08-30 23:09:12 +08:00
|
|
|
from django.contrib.auth.models import User
|
2011-06-28 18:16:34 +08:00
|
|
|
from django.contrib.sitemaps import Sitemap, GenericSitemap
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
from django.contrib.sites.models import Site
|
2010-10-11 22:34:42 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2010-08-27 23:16:04 +08:00
|
|
|
from django.test import TestCase
|
2010-10-12 15:15:47 +08:00
|
|
|
from django.utils.unittest import skipUnless
|
2010-08-27 23:16:04 +08:00
|
|
|
from django.utils.formats import localize
|
2010-10-09 22:36:18 +08:00
|
|
|
from django.utils.translation import activate, deactivate
|
2010-08-27 23:16:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SitemapTests(TestCase):
|
|
|
|
urls = 'django.contrib.sitemaps.tests.urls'
|
|
|
|
|
|
|
|
def setUp(self):
|
2011-02-05 11:49:03 +08:00
|
|
|
if Site._meta.installed:
|
|
|
|
self.base_url = 'http://example.com'
|
|
|
|
else:
|
|
|
|
self.base_url = 'http://testserver'
|
2010-08-27 23:16:04 +08:00
|
|
|
self.old_USE_L10N = settings.USE_L10N
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
self.old_Site_meta_installed = Site._meta.installed
|
2010-12-13 06:56:29 +08:00
|
|
|
self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
|
2011-02-05 11:49:03 +08:00
|
|
|
self.old_Site_meta_installed = Site._meta.installed
|
2010-12-13 06:56:29 +08:00
|
|
|
settings.TEMPLATE_DIRS = (
|
|
|
|
os.path.join(os.path.dirname(__file__), 'templates'),
|
|
|
|
)
|
2010-08-30 23:09:12 +08:00
|
|
|
# Create a user that will double as sitemap content
|
|
|
|
User.objects.create_user('testuser', 'test@example.com', 's3krit')
|
2010-08-27 23:16:04 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
settings.USE_L10N = self.old_USE_L10N
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
Site._meta.installed = self.old_Site_meta_installed
|
2010-12-13 06:56:29 +08:00
|
|
|
settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
|
2011-02-05 11:49:03 +08:00
|
|
|
Site._meta.installed = self.old_Site_meta_installed
|
2010-12-13 06:56:29 +08:00
|
|
|
|
|
|
|
def test_simple_sitemap_index(self):
|
|
|
|
"A simple sitemap index can be rendered"
|
|
|
|
# Retrieve the sitemap.
|
|
|
|
response = self.client.get('/simple/index.xml')
|
|
|
|
# Check for all the important bits:
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
|
2010-12-13 06:56:29 +08:00
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2011-02-05 11:49:03 +08:00
|
|
|
<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
|
2010-12-13 06:56:29 +08:00
|
|
|
</sitemapindex>
|
2011-02-05 11:49:03 +08:00
|
|
|
""" % self.base_url)
|
2010-12-13 06:56:29 +08:00
|
|
|
|
|
|
|
def test_simple_sitemap_custom_index(self):
|
|
|
|
"A simple sitemap index can be rendered with a custom template"
|
|
|
|
# Retrieve the sitemap.
|
|
|
|
response = self.client.get('/simple/custom-index.xml')
|
|
|
|
# Check for all the important bits:
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
|
2010-12-13 06:56:29 +08:00
|
|
|
<!-- This is a customised template -->
|
|
|
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2011-02-05 11:49:03 +08:00
|
|
|
<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
|
2010-12-13 06:56:29 +08:00
|
|
|
</sitemapindex>
|
2011-02-05 11:49:03 +08:00
|
|
|
""" % self.base_url)
|
2010-08-27 23:16:04 +08:00
|
|
|
|
|
|
|
def test_simple_sitemap(self):
|
|
|
|
"A simple sitemap can be rendered"
|
|
|
|
# Retrieve the sitemap.
|
2010-08-30 23:09:12 +08:00
|
|
|
response = self.client.get('/simple/sitemap.xml')
|
2010-08-27 23:16:04 +08:00
|
|
|
# Check for all the important bits:
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
|
2010-08-27 23:16:04 +08:00
|
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2011-02-05 11:49:03 +08:00
|
|
|
<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
|
2010-12-13 06:56:29 +08:00
|
|
|
</urlset>
|
2011-10-14 03:23:45 +08:00
|
|
|
""" % (self.base_url, date.today()))
|
2010-12-13 06:56:29 +08:00
|
|
|
|
|
|
|
def test_simple_custom_sitemap(self):
|
|
|
|
"A simple sitemap can be rendered with a custom template"
|
|
|
|
# Retrieve the sitemap.
|
|
|
|
response = self.client.get('/simple/custom-sitemap.xml')
|
|
|
|
# Check for all the important bits:
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
|
2010-12-13 06:56:29 +08:00
|
|
|
<!-- This is a customised template -->
|
|
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2011-02-05 11:49:03 +08:00
|
|
|
<url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
|
2010-08-27 23:16:04 +08:00
|
|
|
</urlset>
|
2011-10-14 03:23:45 +08:00
|
|
|
""" % (self.base_url, date.today()))
|
2010-08-27 23:16:04 +08:00
|
|
|
|
2010-12-05 10:07:46 +08:00
|
|
|
@skipUnless(settings.USE_I18N, "Internationalization is not enabled")
|
2010-08-27 23:16:04 +08:00
|
|
|
def test_localized_priority(self):
|
|
|
|
"The priority value should not be localized (Refs #14164)"
|
|
|
|
# Localization should be active
|
|
|
|
settings.USE_L10N = True
|
|
|
|
activate('fr')
|
|
|
|
self.assertEqual(u'0,3', localize(0.3))
|
|
|
|
|
|
|
|
# Retrieve the sitemap. Check that priorities
|
|
|
|
# haven't been rendered in localized format
|
2010-08-30 23:09:12 +08:00
|
|
|
response = self.client.get('/simple/sitemap.xml')
|
2010-08-27 23:16:04 +08:00
|
|
|
self.assertContains(response, '<priority>0.5</priority>')
|
2011-10-14 03:23:45 +08:00
|
|
|
self.assertContains(response, '<lastmod>%s</lastmod>' % date.today())
|
2010-10-09 22:36:18 +08:00
|
|
|
deactivate()
|
2010-08-30 23:09:12 +08:00
|
|
|
|
|
|
|
def test_generic_sitemap(self):
|
|
|
|
"A minimal generic sitemap can be rendered"
|
|
|
|
# Retrieve the sitemap.
|
|
|
|
response = self.client.get('/generic/sitemap.xml')
|
2010-10-12 15:15:47 +08:00
|
|
|
|
|
|
|
expected = ''
|
|
|
|
for username in User.objects.values_list("username", flat=True):
|
2011-02-05 11:49:03 +08:00
|
|
|
expected += "<url><loc>%s/users/%s/</loc></url>" % (self.base_url, username)
|
2010-08-30 23:09:12 +08:00
|
|
|
# Check for all the important bits:
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
|
2010-08-30 23:09:12 +08:00
|
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2010-10-12 15:15:47 +08:00
|
|
|
%s
|
2010-08-30 23:09:12 +08:00
|
|
|
</urlset>
|
2011-02-05 11:49:03 +08:00
|
|
|
""" % expected)
|
2010-09-11 07:30:46 +08:00
|
|
|
|
2010-10-12 15:15:47 +08:00
|
|
|
@skipUnless("django.contrib.flatpages" in settings.INSTALLED_APPS, "django.contrib.flatpages app not installed.")
|
2010-09-11 07:30:46 +08:00
|
|
|
def test_flatpage_sitemap(self):
|
|
|
|
"Basic FlatPage sitemap test"
|
2010-10-12 15:15:47 +08:00
|
|
|
|
|
|
|
# Import FlatPage inside the test so that when django.contrib.flatpages
|
|
|
|
# is not installed we don't get problems trying to delete Site
|
|
|
|
# objects (FlatPage has an M2M to Site, Site.delete() tries to
|
|
|
|
# delete related objects, but the M2M table doesn't exist.
|
|
|
|
from django.contrib.flatpages.models import FlatPage
|
|
|
|
|
2010-09-11 07:30:46 +08:00
|
|
|
public = FlatPage.objects.create(
|
|
|
|
url=u'/public/',
|
|
|
|
title=u'Public Page',
|
|
|
|
enable_comments=True,
|
|
|
|
registration_required=False,
|
|
|
|
)
|
|
|
|
public.sites.add(settings.SITE_ID)
|
|
|
|
private = FlatPage.objects.create(
|
|
|
|
url=u'/private/',
|
2010-10-11 11:39:02 +08:00
|
|
|
title=u'Private Page',
|
2010-09-11 07:30:46 +08:00
|
|
|
enable_comments=True,
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
registration_required=True
|
2010-09-11 07:30:46 +08:00
|
|
|
)
|
|
|
|
private.sites.add(settings.SITE_ID)
|
|
|
|
response = self.client.get('/flatpages/sitemap.xml')
|
|
|
|
# Public flatpage should be in the sitemap
|
2011-02-05 11:49:03 +08:00
|
|
|
self.assertContains(response, '<loc>%s%s</loc>' % (self.base_url, public.url))
|
2010-09-11 07:30:46 +08:00
|
|
|
# Private flatpage should not be in the sitemap
|
2011-02-05 11:49:03 +08:00
|
|
|
self.assertNotContains(response, '<loc>%s%s</loc>' % (self.base_url, private.url))
|
2010-09-11 07:30:46 +08:00
|
|
|
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
def test_requestsite_sitemap(self):
|
|
|
|
# Make sure hitting the flatpages sitemap without the sites framework
|
|
|
|
# installed doesn't raise an exception
|
|
|
|
Site._meta.installed = False
|
|
|
|
# Retrieve the sitemap.
|
|
|
|
response = self.client.get('/simple/sitemap.xml')
|
|
|
|
# Check for all the important bits:
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
|
|
<url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
|
|
|
|
</urlset>
|
2011-10-14 03:23:45 +08:00
|
|
|
""" % date.today())
|
2010-10-11 22:34:42 +08:00
|
|
|
|
2011-02-05 11:49:03 +08:00
|
|
|
@skipUnless("django.contrib.sites" in settings.INSTALLED_APPS, "django.contrib.sites app not installed.")
|
2010-10-11 22:34:42 +08:00
|
|
|
def test_sitemap_get_urls_no_site_1(self):
|
|
|
|
"""
|
|
|
|
Check we get ImproperlyConfigured if we don't pass a site object to
|
|
|
|
Sitemap.get_urls and no Site objects exist
|
|
|
|
"""
|
|
|
|
Site.objects.all().delete()
|
|
|
|
self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
|
|
|
|
|
|
|
|
def test_sitemap_get_urls_no_site_2(self):
|
|
|
|
"""
|
|
|
|
Check we get ImproperlyConfigured when we don't pass a site object to
|
|
|
|
Sitemap.get_urls if Site objects exists, but the sites framework is not
|
|
|
|
actually installed.
|
|
|
|
"""
|
|
|
|
Site._meta.installed = False
|
|
|
|
self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
|
2011-06-28 18:16:34 +08:00
|
|
|
|
|
|
|
def test_sitemap_item(self):
|
|
|
|
"""
|
|
|
|
Check to make sure that the raw item is included with each
|
|
|
|
Sitemap.get_url() url result.
|
|
|
|
"""
|
|
|
|
user_sitemap = GenericSitemap({'queryset': User.objects.all()})
|
|
|
|
def is_user(url):
|
|
|
|
return isinstance(url['item'], User)
|
|
|
|
item_in_url_info = all(map(is_user, user_sitemap.get_urls()))
|
|
|
|
self.assertTrue(item_in_url_info)
|