From 9aeb917dad2c3d4c2830f38d3bfc63699e0ba837 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Mon, 8 Dec 2014 01:21:07 +0200 Subject: [PATCH] Fixed #23884 -- Moved FlatPageSitemap into django.contrib.flatpages. --- django/contrib/flatpages/sitemaps.py | 12 ++++++ .../contrib/flatpages/tests/test_sitemaps.py | 25 ++++++++++++ django/contrib/flatpages/tests/urls.py | 6 +++ django/contrib/sitemaps/__init__.py | 15 +++++++ .../contrib/sitemaps/tests/test_flatpages.py | 18 +++++++++ docs/internals/deprecation.txt | 3 ++ docs/ref/contrib/flatpages.txt | 40 +++++++++++++++++++ docs/ref/contrib/sitemaps.txt | 17 ++++---- docs/releases/1.8.txt | 7 ++++ 9 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 django/contrib/flatpages/sitemaps.py create mode 100644 django/contrib/flatpages/tests/test_sitemaps.py diff --git a/django/contrib/flatpages/sitemaps.py b/django/contrib/flatpages/sitemaps.py new file mode 100644 index 0000000000..a144023178 --- /dev/null +++ b/django/contrib/flatpages/sitemaps.py @@ -0,0 +1,12 @@ +from django.apps import apps as django_apps +from django.contrib.sitemaps import Sitemap +from django.core.exceptions import ImproperlyConfigured + + +class FlatPageSitemap(Sitemap): + def items(self): + if not django_apps.is_installed('django.contrib.sites'): + raise ImproperlyConfigured("FlatPageSitemap requires django.contrib.sites, which isn't installed.") + Site = django_apps.get_model('sites.Site') + current_site = Site.objects.get_current() + return current_site.flatpage_set.filter(registration_required=False) diff --git a/django/contrib/flatpages/tests/test_sitemaps.py b/django/contrib/flatpages/tests/test_sitemaps.py new file mode 100644 index 0000000000..3b8e57d174 --- /dev/null +++ b/django/contrib/flatpages/tests/test_sitemaps.py @@ -0,0 +1,25 @@ +from __future__ import unicode_literals + +from django.apps import apps +from django.test import TestCase +from django.test.utils import modify_settings, override_settings + + +@override_settings( + ROOT_URLCONF='django.contrib.flatpages.tests.urls', + SITE_ID=1, +) +@modify_settings(INSTALLED_APPS={'append': ['django.contrib.sitemaps']},) +class FlatpagesSitemapTests(TestCase): + + @classmethod + def setUpTestData(cls): + Site = apps.get_model('sites.Site') + current_site = Site.objects.get_current() + current_site.flatpage_set.create(url="/foo/", title="foo") + current_site.flatpage_set.create(url="/private-foo/", title="private foo", registration_required=True) + + def test_flatpage_sitemap(self): + response = self.client.get('/flatpages/sitemap.xml') + self.assertIn(b'http://example.com/foo/', response.getvalue()) + self.assertNotIn(b'http://example.com/private-foo/', response.getvalue()) diff --git a/django/contrib/flatpages/tests/urls.py b/django/contrib/flatpages/tests/urls.py index 44975c1685..5b2c576b14 100644 --- a/django/contrib/flatpages/tests/urls.py +++ b/django/contrib/flatpages/tests/urls.py @@ -1,7 +1,13 @@ from django.conf.urls import include, url +from django.contrib.flatpages.sitemaps import FlatPageSitemap +from django.contrib.sitemaps import views # special urls for flatpage test cases urlpatterns = [ + url(r'^flatpages/sitemap\.xml$', views.sitemap, + {'sitemaps': {'flatpages': FlatPageSitemap}}, + name='django.contrib.sitemaps.views.sitemap'), + url(r'^flatpage_root', include('django.contrib.flatpages.urls')), url(r'^accounts/', include('django.contrib.auth.urls')), ] diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py index 25877871f0..3db4ef53a4 100644 --- a/django/contrib/sitemaps/__init__.py +++ b/django/contrib/sitemaps/__init__.py @@ -1,8 +1,11 @@ +import warnings + from django.apps import apps as django_apps from django.conf import settings from django.core import urlresolvers, paginator from django.core.exceptions import ImproperlyConfigured from django.utils import translation +from django.utils.deprecation import RemovedInDjango19Warning from django.utils.six.moves.urllib.parse import urlencode from django.utils.six.moves.urllib.request import urlopen @@ -133,6 +136,18 @@ class Sitemap(object): class FlatPageSitemap(Sitemap): + # This class is not a subclass of + # django.contrib.flatpages.sitemaps.FlatPageSitemap to avoid a + # circular import problem. + + def __init__(self): + warnings.warn( + "'django.contrib.sitemaps.FlatPageSitemap' is deprecated. " + "Use 'django.contrib.flatpages.sitemaps.FlatPageSitemap' instead.", + RemovedInDjango19Warning, + stacklevel=2 + ) + def items(self): if not django_apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured("FlatPageSitemap requires django.contrib.sites, which isn't installed.") diff --git a/django/contrib/sitemaps/tests/test_flatpages.py b/django/contrib/sitemaps/tests/test_flatpages.py index c284c66c0d..71949ae4d0 100644 --- a/django/contrib/sitemaps/tests/test_flatpages.py +++ b/django/contrib/sitemaps/tests/test_flatpages.py @@ -1,9 +1,12 @@ from __future__ import unicode_literals from unittest import skipUnless +import warnings from django.apps import apps from django.conf import settings +from django.contrib.sitemaps import FlatPageSitemap +from django.test import SimpleTestCase from .base import SitemapTestsBase @@ -40,3 +43,18 @@ class FlatpagesSitemapTests(SitemapTestsBase): self.assertContains(response, '%s%s' % (self.base_url, public.url)) # Private flatpage should not be in the sitemap self.assertNotContains(response, '%s%s' % (self.base_url, private.url)) + + +class FlatpagesSitemapDeprecationTests(SimpleTestCase): + + def test_deprecation(self): + with warnings.catch_warnings(record=True) as warns: + warnings.simplefilter('always') + FlatPageSitemap() + + self.assertEqual(len(warns), 1) + self.assertEqual( + str(warns[0].message), + "'django.contrib.sitemaps.FlatPageSitemap' is deprecated. " + "Use 'django.contrib.flatpages.sitemaps.FlatPageSitemap' instead.", + ) diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 15fbed1565..54d86cbf23 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -222,6 +222,9 @@ details on these changes. :attr:`RedirectView.permanent ` attribute will change from ``True`` to ``False``. +* ``django.contrib.sitemaps.FlatPageSitemap`` will be removed in favor of + ``django.contrib.flatpages.sitemaps.FlatPageSitemap``. + .. _deprecation-removed-in-1.8: 1.8 diff --git a/docs/ref/contrib/flatpages.txt b/docs/ref/contrib/flatpages.txt index b280e0ddc4..a40e912bd8 100644 --- a/docs/ref/contrib/flatpages.txt +++ b/docs/ref/contrib/flatpages.txt @@ -293,3 +293,43 @@ For example: {% get_flatpages '/about/' as about_pages %} {% get_flatpages about_prefix as about_pages %} {% get_flatpages '/about/' for someuser as about_pages %} + +Integrating with :mod:`django.contrib.sitemaps` +=============================================== + +.. currentmodule:: django.contrib.flatpages.sitemaps + +.. class:: FlatPageSitemap + + The :class:`sitemaps.FlatPageSitemap + ` class looks at all + publicly visible :mod:`~django.contrib.flatpages` defined for the current + :setting:`SITE_ID` (see the :mod:`sites documentation + `) and creates an entry in the sitemap. These entries + include only the :attr:`~django.contrib.sitemaps.Sitemap.location` + attribute -- not :attr:`~django.contrib.sitemaps.Sitemap.lastmod`, + :attr:`~django.contrib.sitemaps.Sitemap.changefreq` or + :attr:`~django.contrib.sitemaps.Sitemap.priority`. + + .. versionchanged:: 1.8 + + This class is available from ``django.contrib.sitemaps.FlatPageSitemap`` + in older version of Django. + +Example +------- + +Here's an example of a URLconf using :class:`FlatPageSitemap`:: + + from django.conf.urls import url + from django.contrib.flatpages.sitemaps import FlatPageSitemap + from django.contrib.sitemaps.views import sitemap + + urlpatterns = [ + # ... + + # the sitemap + url(r'^sitemap\.xml$', sitemap, + {'sitemaps': {'flatpages': FlatPageSitemap}}, + name='django.contrib.sitemaps.views.sitemap'), + ] diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt index 5902a131a3..8279d8e93e 100644 --- a/docs/ref/contrib/sitemaps.txt +++ b/docs/ref/contrib/sitemaps.txt @@ -251,6 +251,10 @@ The sitemap framework provides a couple convenience classes for common cases: .. class:: FlatPageSitemap + .. deprecated:: 1.8 + + Use :class:`django.contrib.flatpages.sitemaps.FlatPageSitemap` instead. + The :class:`django.contrib.sitemaps.FlatPageSitemap` class looks at all publicly visible :mod:`flatpages ` defined for the current :setting:`SITE_ID` (see the @@ -275,10 +279,11 @@ The sitemap framework provides a couple convenience classes for common cases: Example ------- -Here's an example of a :doc:`URLconf ` using both:: +Here's an example of a :doc:`URLconf ` using +:class:`GenericSitemap`:: from django.conf.urls import url - from django.contrib.sitemaps import FlatPageSitemap, GenericSitemap + from django.contrib.sitemaps import GenericSitemap from django.contrib.sitemaps.views import sitemap from blog.models import Entry @@ -287,17 +292,13 @@ Here's an example of a :doc:`URLconf ` using both:: 'date_field': 'pub_date', } - sitemaps = { - 'flatpages': FlatPageSitemap, - 'blog': GenericSitemap(info_dict, priority=0.6), - } - urlpatterns = [ # some generic view using info_dict # ... # the sitemap - url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, + url(r'^sitemap\.xml$', sitemap, + {'sitemaps': {'blog': GenericSitemap(info_dict, priority=0.6)}}, name='django.contrib.sitemaps.views.sitemap'), ] diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 0637bdc206..5b4d8d2df3 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -1204,6 +1204,13 @@ In Django 2.0, session verification will be enabled regardless of whether or not to your ``MIDDLEWARE_CLASSES`` sometime before then to opt-in. Please read the :ref:`upgrade considerations ` first. +``django.contrib.sitemaps.FlatPageSitemap`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``django.contrib.sitemaps.FlatPageSitemap`` has moved to +``django.contrib.flatpages.sitemaps.FlatPageSitemap``. The old import location +is deprecated and will be removed in Django 1.9. + .. removed-features-1.8: Features removed in 1.8