Browse Source

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
Karen Tracey 14 years ago
parent
commit
5912903852

+ 1 - 1
django/contrib/sitemaps/__init__.py

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

+ 24 - 0
django/contrib/sitemaps/tests/basic.py

@@ -1,6 +1,7 @@
 from datetime import date
 from django.conf import settings
 from django.contrib.auth.models import User
+from django.contrib.flatpages.models import FlatPage
 from django.test import TestCase
 from django.utils.formats import localize
 from django.utils.translation import activate
@@ -51,3 +52,26 @@ class SitemapTests(TestCase):
 <url><loc>http://example.com/users/testuser/</loc></url>
 </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)
+

+ 6 - 1
django/contrib/sitemaps/tests/urls.py

@@ -1,6 +1,6 @@
 from datetime import datetime
 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
 
 class SimpleSitemap(Sitemap):
@@ -22,7 +22,12 @@ generic_sitemaps = {
     }),
 }
 
+flatpage_sitemaps = {
+    'flatpages': FlatPageSitemap,
+}
+
 urlpatterns = patterns('django.contrib.sitemaps.views',
     (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
     (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}),
+    (r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}),
 )

+ 2 - 2
docs/ref/contrib/sitemaps.txt

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