diff --git a/core/settings.py b/core/settings.py index 6a6f7f17..bff03cc3 100644 --- a/core/settings.py +++ b/core/settings.py @@ -51,6 +51,7 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "django.contrib.sitemaps", "django.contrib.humanize", "data_registry", "markdownx", diff --git a/data_registry/sitemaps.py b/data_registry/sitemaps.py new file mode 100644 index 00000000..8205fe15 --- /dev/null +++ b/data_registry/sitemaps.py @@ -0,0 +1,32 @@ +from django.contrib.sitemaps import Sitemap +from django.urls import reverse + +from data_registry import models + + +class StaticViewSitemap(Sitemap): + i18n = True + alternates = True + protocol = "https" + + def items(self): + return ["index", "search"] + + def location(self, item): + return reverse(item) + + +class CollectionSitemap(Sitemap): + i18n = True + alternates = True + protocol = "https" + + # See data_registry.util.collection_queryset(). + def items(self): + return models.Collection.objects.visible() + + def location(self, item): + return reverse("detail", kwargs={"id": item.pk}) + + def lastmod(self, obj): + return obj.modified diff --git a/data_registry/urls.py b/data_registry/urls.py index 2d6ac3de..8a274733 100644 --- a/data_registry/urls.py +++ b/data_registry/urls.py @@ -1,15 +1,22 @@ +from django.contrib.sitemaps.views import sitemap from django.urls import path -from data_registry import i18n, views +from data_registry import i18n, sitemaps, views + +maps = { + "static": sitemaps.StaticViewSitemap, + "detail": sitemaps.CollectionSitemap, +} urlpatterns = [ path("", views.index, name="index"), path("search/", views.search, name="search"), path("publication/", views.detail, name="detail"), path("publication//download", views.download_export, name="download"), - # https://code.djangoproject.com/ticket/26556 - path("i18n/setlang/", i18n.set_language, name="set-language"), # Uncomment after re-integrating Spoonbill. # path("excel-data//", views.excel_data, name="excel-data"), # path("excel-data/", views.excel_data, name="all-excel-data"), + # https://code.djangoproject.com/ticket/26556 + path("i18n/setlang/", i18n.set_language, name="set-language"), + path("sitemap.xml", sitemap, {"sitemaps": maps}, name="django.contrib.sitemaps.views.sitemap"), ]