mirror of
https://github.com/django/django.git
synced 2025-01-22 00:02:15 +00:00
Fixed #23884 -- Moved FlatPageSitemap into django.contrib.flatpages.
This commit is contained in:
parent
aa5ef0d4fc
commit
9aeb917dad
12
django/contrib/flatpages/sitemaps.py
Normal file
12
django/contrib/flatpages/sitemaps.py
Normal file
@ -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)
|
25
django/contrib/flatpages/tests/test_sitemaps.py
Normal file
25
django/contrib/flatpages/tests/test_sitemaps.py
Normal file
@ -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'<url><loc>http://example.com/foo/</loc></url>', response.getvalue())
|
||||
self.assertNotIn(b'<url><loc>http://example.com/private-foo/</loc></url>', response.getvalue())
|
@ -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')),
|
||||
]
|
||||
|
@ -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.")
|
||||
|
@ -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, '<loc>%s%s</loc>' % (self.base_url, public.url))
|
||||
# Private flatpage should not be in the sitemap
|
||||
self.assertNotContains(response, '<loc>%s%s</loc>' % (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.",
|
||||
)
|
||||
|
@ -222,6 +222,9 @@ details on these changes.
|
||||
:attr:`RedirectView.permanent <django.views.generic.base.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
|
||||
|
@ -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
|
||||
<django.contrib.flatpages.sitemaps.FlatPageSitemap>` class looks at all
|
||||
publicly visible :mod:`~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:`~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'),
|
||||
]
|
||||
|
@ -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 <django.contrib.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 </topics/http/urls>` using both::
|
||||
Here's an example of a :doc:`URLconf </topics/http/urls>` 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 </topics/http/urls>` 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'),
|
||||
]
|
||||
|
||||
|
@ -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 <session-invalidation-on-password-change>` 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user