87 lines
2.2 KiB
Python
87 lines
2.2 KiB
Python
from django.contrib.sitemaps.views import index as sitemap_index_view
|
|
from django.contrib.sitemaps.views import sitemap as sitemap_section_view
|
|
from django.http import HttpResponse
|
|
|
|
from wagtail.models import Locale, Page
|
|
|
|
from ocyan.plugin.wagtail_oscar_integration.constants import CACHE_DURATION
|
|
from ocyan.plugin.wagtail_oscar_integration.sitemap import (
|
|
CategorySitemap,
|
|
ProductSitemap,
|
|
ShopSitemap,
|
|
)
|
|
from ocyan.plugin.wagtail_oscar_integration.sitemap import (
|
|
WagtailSitemap as BaseWagtailSitemap,
|
|
)
|
|
|
|
|
|
class WagtailSitemap(BaseWagtailSitemap):
|
|
def items(self):
|
|
page_ids = []
|
|
|
|
for locale in Locale.objects.all():
|
|
translated_root_page = (
|
|
self.get_wagtail_site().root_page.get_translation_or_none(locale)
|
|
)
|
|
if translated_root_page is None:
|
|
continue
|
|
|
|
locale_page_ids = (
|
|
translated_root_page.get_descendants(inclusive=True)
|
|
.live()
|
|
.public()
|
|
.order_by()
|
|
.values_list("pk", flat=True)
|
|
)
|
|
page_ids.extend(locale_page_ids)
|
|
|
|
if not page_ids:
|
|
return []
|
|
|
|
return (
|
|
Page.objects.filter(pk__in=page_ids)
|
|
.live()
|
|
.public()
|
|
.defer_streamfields()
|
|
.order_by("path")
|
|
.specific()
|
|
)
|
|
|
|
|
|
def gather_sitemaps():
|
|
return {
|
|
"pages": WagtailSitemap,
|
|
"shop": ShopSitemap,
|
|
"products": ProductSitemap,
|
|
"categories": CategorySitemap,
|
|
}
|
|
|
|
|
|
def sitemap_index(request):
|
|
return sitemap_index_view(
|
|
request,
|
|
sitemaps=gather_sitemaps(),
|
|
sitemap_url_name="sitemaps",
|
|
)
|
|
|
|
|
|
def sitemap_section(request, section=None):
|
|
return sitemap_section_view(
|
|
request,
|
|
sitemaps=gather_sitemaps(),
|
|
section=section,
|
|
)
|
|
|
|
|
|
def robots_txt(request):
|
|
sitemap_url = request.build_absolute_uri("/sitemap.xml")
|
|
content = "\n".join(
|
|
[
|
|
"User-agent: *",
|
|
"Allow: /",
|
|
f"Sitemap: {sitemap_url}",
|
|
"",
|
|
]
|
|
)
|
|
return HttpResponse(content, content_type="text/plain; charset=utf-8")
|