diff options
Diffstat (limited to 'django/contrib/sitemaps')
-rw-r--r-- | django/contrib/sitemaps/__init__.py | 5 | ||||
-rw-r--r-- | django/contrib/sitemaps/models.py | 1 | ||||
-rw-r--r-- | django/contrib/sitemaps/tests/__init__.py | 1 | ||||
-rw-r--r-- | django/contrib/sitemaps/tests/basic.py | 77 | ||||
-rw-r--r-- | django/contrib/sitemaps/tests/urls.py | 33 |
5 files changed, 115 insertions, 2 deletions
diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py index f877317f16..eaa7f85de9 100644 --- a/django/contrib/sitemaps/__init__.py +++ b/django/contrib/sitemaps/__init__.py @@ -65,11 +65,12 @@ class Sitemap(object): urls = [] for item in self.paginator.page(page).object_list: loc = "http://%s%s" % (current_site.domain, self.__get('location', item)) + priority = self.__get('priority', item, None) url_info = { 'location': loc, 'lastmod': self.__get('lastmod', item, None), 'changefreq': self.__get('changefreq', item, None), - 'priority': self.__get('priority', item, None) + 'priority': str(priority is not None and priority or '') } urls.append(url_info) return urls @@ -78,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 diff --git a/django/contrib/sitemaps/models.py b/django/contrib/sitemaps/models.py new file mode 100644 index 0000000000..7ff128fa69 --- /dev/null +++ b/django/contrib/sitemaps/models.py @@ -0,0 +1 @@ +# This file intentionally left blank
\ No newline at end of file diff --git a/django/contrib/sitemaps/tests/__init__.py b/django/contrib/sitemaps/tests/__init__.py new file mode 100644 index 0000000000..c5b483cde2 --- /dev/null +++ b/django/contrib/sitemaps/tests/__init__.py @@ -0,0 +1 @@ +from django.contrib.sitemaps.tests.basic import * diff --git a/django/contrib/sitemaps/tests/basic.py b/django/contrib/sitemaps/tests/basic.py new file mode 100644 index 0000000000..ad04db258f --- /dev/null +++ b/django/contrib/sitemaps/tests/basic.py @@ -0,0 +1,77 @@ +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 + + +class SitemapTests(TestCase): + urls = 'django.contrib.sitemaps.tests.urls' + + def setUp(self): + self.old_USE_L10N = settings.USE_L10N + # Create a user that will double as sitemap content + User.objects.create_user('testuser', 'test@example.com', 's3krit') + + def tearDown(self): + settings.USE_L10N = self.old_USE_L10N + + def test_simple_sitemap(self): + "A simple sitemap can be rendered" + # Retrieve the sitemap. + response = self.client.get('/simple/sitemap.xml') + # Check for all the important bits: + self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> +</urlset> +""" % date.today().strftime('%Y-%m-%d')) + + 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 + response = self.client.get('/simple/sitemap.xml') + self.assertContains(response, '<priority>0.5</priority>') + self.assertContains(response, '<lastmod>%s</lastmod>' % date.today().strftime('%Y-%m-%d')) + + def test_generic_sitemap(self): + "A minimal generic sitemap can be rendered" + # Retrieve the sitemap. + response = self.client.get('/generic/sitemap.xml') + # Check for all the important bits: + self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<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) + diff --git a/django/contrib/sitemaps/tests/urls.py b/django/contrib/sitemaps/tests/urls.py new file mode 100644 index 0000000000..6cdba36b02 --- /dev/null +++ b/django/contrib/sitemaps/tests/urls.py @@ -0,0 +1,33 @@ +from datetime import datetime +from django.conf.urls.defaults import * +from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap +from django.contrib.auth.models import User + +class SimpleSitemap(Sitemap): + changefreq = "never" + priority = 0.5 + location = '/location/' + lastmod = datetime.now() + + def items(self): + return [object()] + +simple_sitemaps = { + 'simple': SimpleSitemap, +} + +generic_sitemaps = { + 'generic': GenericSitemap({ + 'queryset': User.objects.all() + }), +} + +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}), +) |