Fixed #11358: Don't include private flatpages in sitemap. Thanks dburke and mlavin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13734 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey 2010-09-10 23:30:46 +00:00
parent 364583b894
commit 5912903852
4 changed files with 33 additions and 4 deletions

View File

@ -79,7 +79,7 @@ class FlatPageSitemap(Sitemap):
def items(self): def items(self):
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
current_site = Site.objects.get_current() current_site = Site.objects.get_current()
return current_site.flatpage_set.all() return current_site.flatpage_set.filter(registration_required=False)
class GenericSitemap(Sitemap): class GenericSitemap(Sitemap):
priority = None priority = None

View File

@ -1,6 +1,7 @@
from datetime import date from datetime import date
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.flatpages.models import FlatPage
from django.test import TestCase from django.test import TestCase
from django.utils.formats import localize from django.utils.formats import localize
from django.utils.translation import activate from django.utils.translation import activate
@ -51,3 +52,26 @@ class SitemapTests(TestCase):
<url><loc>http://example.com/users/testuser/</loc></url> <url><loc>http://example.com/users/testuser/</loc></url>
</urlset> </urlset>
""") """)
def test_flatpage_sitemap(self):
"Basic FlatPage sitemap test"
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/',
title=u'Public Page',
enable_comments=True,
registration_required=True
)
private.sites.add(settings.SITE_ID)
response = self.client.get('/flatpages/sitemap.xml')
# Public flatpage should be in the sitemap
self.assertContains(response, '<loc>http://example.com%s</loc>' % public.url)
# Private flatpage should not be in the sitemap
self.assertNotContains(response, '<loc>http://example.com%s</loc>' % private.url)

View File

@ -1,6 +1,6 @@
from datetime import datetime from datetime import datetime
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
from django.contrib.sitemaps import Sitemap, GenericSitemap from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap
from django.contrib.auth.models import User from django.contrib.auth.models import User
class SimpleSitemap(Sitemap): class SimpleSitemap(Sitemap):
@ -22,7 +22,12 @@ generic_sitemaps = {
}), }),
} }
flatpage_sitemaps = {
'flatpages': FlatPageSitemap,
}
urlpatterns = patterns('django.contrib.sitemaps.views', urlpatterns = patterns('django.contrib.sitemaps.views',
(r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
(r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}), (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}),
(r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}),
) )

View File

@ -215,8 +215,8 @@ The sitemap framework provides a couple convenience classes for common cases:
.. class:: FlatPageSitemap .. class:: FlatPageSitemap
The :class:`django.contrib.sitemaps.FlatPageSitemap` class looks at all The :class:`django.contrib.sitemaps.FlatPageSitemap` class looks at all
:mod:`flatpages <django.contrib.flatpages>` defined for the current publicly visible :mod:`flatpages <django.contrib.flatpages>`
:setting:`SITE_ID` (see the defined for the current :setting:`SITE_ID` (see the
:mod:`sites documentation <django.contrib.sites>`) and :mod:`sites documentation <django.contrib.sites>`) and
creates an entry in the sitemap. These entries include only the creates an entry in the sitemap. These entries include only the
:attr:`~Sitemap.location` attribute -- not :attr:`~Sitemap.lastmod`, :attr:`~Sitemap.location` attribute -- not :attr:`~Sitemap.lastmod`,