Initial commit
This commit is contained in:
1
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__init__.py
vendored
Normal file
1
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__init__.py
vendored
Normal file
@@ -0,0 +1 @@
|
||||
from .sitemap_generator import Sitemap # noqa: F401
|
||||
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/__init__.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/__init__.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/apps.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/apps.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/models.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/models.cpython-310.pyc
vendored
Normal file
Binary file not shown.
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/tests.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/tests.cpython-310.pyc
vendored
Normal file
Binary file not shown.
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/views.cpython-310.pyc
vendored
Normal file
BIN
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/__pycache__/views.cpython-310.pyc
vendored
Normal file
Binary file not shown.
8
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/apps.py
vendored
Normal file
8
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/apps.py
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.apps import AppConfig
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class WagtailSitemapsAppConfig(AppConfig):
|
||||
name = "wagtail.contrib.sitemaps"
|
||||
label = "wagtailsitemaps"
|
||||
verbose_name = _("Wagtail sitemaps")
|
||||
0
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/models.py
vendored
Normal file
0
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/models.py
vendored
Normal file
54
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/sitemap_generator.py
vendored
Normal file
54
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/sitemap_generator.py
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
from django.contrib.sitemaps import Sitemap as DjangoSitemap
|
||||
|
||||
# Note: avoid importing models here. This module is imported from __init__.py
|
||||
# which causes it to be loaded early in startup if wagtail.contrib.sitemaps is
|
||||
# included in INSTALLED_APPS (not required, but developers are likely to add it
|
||||
# anyhow) leading to an AppRegistryNotReady exception.
|
||||
|
||||
|
||||
class Sitemap(DjangoSitemap):
|
||||
def __init__(self, request=None):
|
||||
self.request = request
|
||||
|
||||
def location(self, obj):
|
||||
return obj.get_full_url(self.request)
|
||||
|
||||
def lastmod(self, obj):
|
||||
# fall back on latest_revision_created_at if last_published_at is null
|
||||
# (for backwards compatibility from before last_published_at was added)
|
||||
return obj.last_published_at or obj.latest_revision_created_at
|
||||
|
||||
def get_wagtail_site(self):
|
||||
from wagtail.models import Site
|
||||
|
||||
site = Site.find_for_request(self.request)
|
||||
if site is None:
|
||||
return Site.objects.select_related("root_page").get(is_default_site=True)
|
||||
return site
|
||||
|
||||
def items(self):
|
||||
return (
|
||||
self.get_wagtail_site()
|
||||
.root_page.get_descendants(inclusive=True)
|
||||
.live()
|
||||
.public()
|
||||
.order_by("path")
|
||||
.defer_streamfields()
|
||||
.specific()
|
||||
)
|
||||
|
||||
def _urls(self, page, protocol, domain):
|
||||
urls = []
|
||||
last_mods = set()
|
||||
|
||||
for item in self.paginator.page(page).object_list.iterator():
|
||||
url_info_items = item.get_sitemap_urls(self.request)
|
||||
|
||||
for url_info in url_info_items:
|
||||
urls.append(url_info)
|
||||
last_mods.add(url_info.get("lastmod"))
|
||||
|
||||
# last_mods might be empty if the whole site is private
|
||||
if last_mods and None not in last_mods:
|
||||
self.latest_lastmod = max(last_mods)
|
||||
return urls
|
||||
286
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/tests.py
vendored
Normal file
286
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/tests.py
vendored
Normal file
@@ -0,0 +1,286 @@
|
||||
import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.sites.shortcuts import get_current_site
|
||||
from django.test import RequestFactory, TestCase, override_settings
|
||||
from django.utils import timezone
|
||||
|
||||
from wagtail.models import Page, PageViewRestriction, Site
|
||||
from wagtail.test.testapp.models import EventIndex, SimplePage
|
||||
|
||||
from .sitemap_generator import Sitemap
|
||||
|
||||
|
||||
class TestSitemapGenerator(TestCase):
|
||||
def setUp(self):
|
||||
self.home_page = Page.objects.get(id=2)
|
||||
|
||||
self.child_page = self.home_page.add_child(
|
||||
instance=SimplePage(
|
||||
title="Hello world!",
|
||||
slug="hello-world",
|
||||
content="hello",
|
||||
live=True,
|
||||
last_published_at=datetime.datetime(
|
||||
2017, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc
|
||||
),
|
||||
latest_revision_created_at=datetime.datetime(
|
||||
2017, 2, 1, 12, 0, 0, tzinfo=datetime.timezone.utc
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
self.unpublished_child_page = self.home_page.add_child(
|
||||
instance=SimplePage(
|
||||
title="Unpublished",
|
||||
slug="unpublished",
|
||||
content="hello",
|
||||
live=False,
|
||||
)
|
||||
)
|
||||
|
||||
self.protected_child_page = self.home_page.add_child(
|
||||
instance=SimplePage(
|
||||
title="Protected",
|
||||
slug="protected",
|
||||
content="hello",
|
||||
live=True,
|
||||
)
|
||||
)
|
||||
PageViewRestriction.objects.create(
|
||||
page=self.protected_child_page, password="hello"
|
||||
)
|
||||
|
||||
self.page_with_no_last_publish_date = self.home_page.add_child(
|
||||
instance=SimplePage(
|
||||
title="I have no last publish date :-(",
|
||||
slug="no-last-publish-date",
|
||||
content="hello",
|
||||
live=True,
|
||||
latest_revision_created_at=datetime.datetime(
|
||||
2017, 2, 1, 12, 0, 0, tzinfo=datetime.timezone.utc
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
self.site = Site.objects.get(is_default_site=True)
|
||||
|
||||
root_page = Page.objects.get(depth=1)
|
||||
self.other_site_homepage = root_page.add_child(
|
||||
instance=SimplePage(
|
||||
title="Another site", slug="another-site", content="bonjour", live=True
|
||||
)
|
||||
)
|
||||
Site.objects.create(
|
||||
hostname="other.example.com", port=80, root_page=self.other_site_homepage
|
||||
)
|
||||
|
||||
# Clear the cache to that runs are deterministic regarding the sql count
|
||||
ContentType.objects.clear_cache()
|
||||
|
||||
def get_request_and_django_site(self, url):
|
||||
request = RequestFactory().get(url)
|
||||
request.META["HTTP_HOST"] = self.site.hostname
|
||||
request.META["SERVER_PORT"] = self.site.port
|
||||
return request, get_current_site(request)
|
||||
|
||||
def assertDatesEqual(self, actual, expected):
|
||||
# Compare dates as naive or timezone-aware according to USE_TZ
|
||||
if not settings.USE_TZ:
|
||||
expected = timezone.make_naive(expected)
|
||||
return self.assertEqual(actual, expected)
|
||||
|
||||
def test_items(self):
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
|
||||
sitemap = Sitemap(request)
|
||||
pages = sitemap.items()
|
||||
|
||||
self.assertIn(self.child_page.page_ptr.specific, pages)
|
||||
self.assertNotIn(self.unpublished_child_page.page_ptr.specific, pages)
|
||||
self.assertNotIn(self.protected_child_page.page_ptr.specific, pages)
|
||||
|
||||
def test_get_urls_without_request(self):
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
req_protocol = request.scheme
|
||||
|
||||
sitemap = Sitemap()
|
||||
with self.assertNumQueries(17):
|
||||
urls = [
|
||||
url["location"]
|
||||
for url in sitemap.get_urls(1, django_site, req_protocol)
|
||||
]
|
||||
|
||||
self.assertIn("http://localhost/", urls) # Homepage
|
||||
self.assertIn("http://localhost/hello-world/", urls) # Child page
|
||||
|
||||
def test_get_urls_with_request_site_cache(self):
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
req_protocol = request.scheme
|
||||
|
||||
sitemap = Sitemap(request)
|
||||
|
||||
# pre-seed find_for_request cache, so that it's not counted towards the query count
|
||||
Site.find_for_request(request)
|
||||
|
||||
with self.assertNumQueries(14):
|
||||
urls = [
|
||||
url["location"]
|
||||
for url in sitemap.get_urls(1, django_site, req_protocol)
|
||||
]
|
||||
|
||||
self.assertIn("http://localhost/", urls) # Homepage
|
||||
self.assertIn("http://localhost/hello-world/", urls) # Child page
|
||||
|
||||
@override_settings(WAGTAIL_I18N_ENABLED=True)
|
||||
def test_get_urls_without_request_with_i18n(self):
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
req_protocol = request.scheme
|
||||
|
||||
sitemap = Sitemap()
|
||||
with self.assertNumQueries(19):
|
||||
urls = [
|
||||
url["location"]
|
||||
for url in sitemap.get_urls(1, django_site, req_protocol)
|
||||
]
|
||||
|
||||
self.assertIn("http://localhost/", urls) # Homepage
|
||||
self.assertIn("http://localhost/hello-world/", urls) # Child page
|
||||
|
||||
@override_settings(WAGTAIL_I18N_ENABLED=True)
|
||||
def test_get_urls_with_request_site_cache_with_i18n(self):
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
req_protocol = request.scheme
|
||||
|
||||
sitemap = Sitemap(request)
|
||||
|
||||
# pre-seed find_for_request cache, so that it's not counted towards the query count
|
||||
Site.find_for_request(request)
|
||||
|
||||
with self.assertNumQueries(16):
|
||||
urls = [
|
||||
url["location"]
|
||||
for url in sitemap.get_urls(1, django_site, req_protocol)
|
||||
]
|
||||
|
||||
self.assertIn("http://localhost/", urls) # Homepage
|
||||
self.assertIn("http://localhost/hello-world/", urls) # Child page
|
||||
|
||||
def test_get_urls_uses_specific(self):
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
req_protocol = request.scheme
|
||||
|
||||
# Add an event page which has an extra url in the sitemap
|
||||
self.home_page.add_child(
|
||||
instance=EventIndex(
|
||||
title="Events",
|
||||
slug="events",
|
||||
live=True,
|
||||
)
|
||||
)
|
||||
|
||||
sitemap = Sitemap(request)
|
||||
urls = [
|
||||
url["location"] for url in sitemap.get_urls(1, django_site, req_protocol)
|
||||
]
|
||||
|
||||
self.assertIn("http://localhost/events/", urls) # Main view
|
||||
self.assertIn("http://localhost/events/past/", urls) # Sub view
|
||||
|
||||
def test_lastmod_uses_last_published_date(self):
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
req_protocol = request.scheme
|
||||
|
||||
sitemap = Sitemap(request)
|
||||
urls = sitemap.get_urls(1, django_site, req_protocol)
|
||||
|
||||
child_page_lastmod = [
|
||||
url["lastmod"]
|
||||
for url in urls
|
||||
if url["location"] == "http://localhost/hello-world/"
|
||||
][0]
|
||||
self.assertDatesEqual(
|
||||
child_page_lastmod,
|
||||
datetime.datetime(2017, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc),
|
||||
)
|
||||
|
||||
# if no last_publish_date is defined, use latest revision date
|
||||
child_page_lastmod = [
|
||||
url["lastmod"]
|
||||
for url in urls
|
||||
if url["location"] == "http://localhost/no-last-publish-date/"
|
||||
][0]
|
||||
self.assertDatesEqual(
|
||||
child_page_lastmod,
|
||||
datetime.datetime(2017, 2, 1, 12, 0, 0, tzinfo=datetime.timezone.utc),
|
||||
)
|
||||
|
||||
def test_latest_lastmod(self):
|
||||
# give the homepage a lastmod
|
||||
self.home_page.last_published_at = datetime.datetime(
|
||||
2017, 3, 1, 12, 0, 0, tzinfo=datetime.timezone.utc
|
||||
)
|
||||
self.home_page.save()
|
||||
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
req_protocol = request.scheme
|
||||
|
||||
sitemap = Sitemap(request)
|
||||
sitemap.get_urls(1, django_site, req_protocol)
|
||||
|
||||
self.assertDatesEqual(
|
||||
sitemap.latest_lastmod,
|
||||
datetime.datetime(2017, 3, 1, 12, 0, 0, tzinfo=datetime.timezone.utc),
|
||||
)
|
||||
|
||||
def test_latest_lastmod_missing(self):
|
||||
# ensure homepage does not have lastmod
|
||||
self.home_page.last_published_at = None
|
||||
self.home_page.save()
|
||||
|
||||
request, django_site = self.get_request_and_django_site("/sitemap.xml")
|
||||
req_protocol = request.scheme
|
||||
|
||||
sitemap = Sitemap(request)
|
||||
sitemap.get_urls(1, django_site, req_protocol)
|
||||
|
||||
self.assertFalse(hasattr(sitemap, "latest_lastmod"))
|
||||
|
||||
def test_non_default_site(self):
|
||||
request = RequestFactory().get("/sitemap.xml")
|
||||
request.META["HTTP_HOST"] = "other.example.com"
|
||||
request.META["SERVER_PORT"] = 80
|
||||
|
||||
sitemap = Sitemap(request)
|
||||
pages = sitemap.items()
|
||||
|
||||
self.assertIn(self.other_site_homepage.page_ptr.specific, pages)
|
||||
self.assertNotIn(self.child_page.page_ptr.specific, pages)
|
||||
|
||||
|
||||
class TestIndexView(TestCase):
|
||||
def test_index_view(self):
|
||||
response = self.client.get("/sitemap-index.xml")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response["Content-Type"], "application/xml")
|
||||
|
||||
|
||||
class TestSitemapView(TestCase):
|
||||
def test_sitemap_view(self):
|
||||
response = self.client.get("/sitemap.xml")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response["Content-Type"], "application/xml")
|
||||
|
||||
def test_sitemap_view_with_current_site_middleware(self):
|
||||
with self.modify_settings(
|
||||
MIDDLEWARE={
|
||||
"append": "django.contrib.sites.middleware.CurrentSiteMiddleware",
|
||||
}
|
||||
):
|
||||
response = self.client.get("/sitemap.xml")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response["Content-Type"], "application/xml")
|
||||
28
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/views.py
vendored
Normal file
28
env/lib/python3.10/site-packages/wagtail/contrib/sitemaps/views.py
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
import inspect
|
||||
|
||||
from django.contrib.sitemaps import views as sitemap_views
|
||||
|
||||
from .sitemap_generator import Sitemap
|
||||
|
||||
|
||||
def index(request, sitemaps, **kwargs):
|
||||
sitemaps = prepare_sitemaps(request, sitemaps)
|
||||
return sitemap_views.index(request, sitemaps, **kwargs)
|
||||
|
||||
|
||||
def sitemap(request, sitemaps=None, **kwargs):
|
||||
if sitemaps:
|
||||
sitemaps = prepare_sitemaps(request, sitemaps)
|
||||
else:
|
||||
sitemaps = {"wagtail": Sitemap(request)}
|
||||
return sitemap_views.sitemap(request, sitemaps, **kwargs)
|
||||
|
||||
|
||||
def prepare_sitemaps(request, sitemaps):
|
||||
initialised_sitemaps = {}
|
||||
for name, sitemap_cls in sitemaps.items():
|
||||
if inspect.isclass(sitemap_cls) and issubclass(sitemap_cls, Sitemap):
|
||||
initialised_sitemaps[name] = sitemap_cls(request)
|
||||
else:
|
||||
initialised_sitemaps[name] = sitemap_cls
|
||||
return initialised_sitemaps
|
||||
Reference in New Issue
Block a user