Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion website/sitemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@
from django.contrib.sitemaps import Sitemap
from django.urls import reverse

from website.models import Project, Person, News
from website.models import Project, Person, News, Publication, Award


def _latest(model, field):
"""Return the most recent non-null value of ``field`` across ``model``, or None."""
return (
model.objects.filter(**{f"{field}__isnull": False})
.order_by(f"-{field}")
.values_list(field, flat=True)
.first()
)


class _HttpsSitemap(Sitemap):
Expand Down Expand Up @@ -62,6 +72,39 @@ def items(self):
def location(self, item):
return reverse(item)

def lastmod(self, item):
"""
Most-recent content date for each listing page, so the sitemap signals
when a section last changed (helps crawlers prioritize re-crawls).

Returns ``None`` for an empty section; the framework then simply omits
``<lastmod>`` for that URL. Some sections expose a ``date`` (plain date)
and people a datetime — Django's ``get_latest_lastmod`` catches the
resulting mixed-type ``max()`` and just drops the sitemap-level lastmod,
so this is safe.
"""
if item == "website:people":
return _latest(Person, "bio_datetime_modified")
if item == "website:publications":
return _latest(Publication, "date")
if item == "website:projects":
return _latest(Project, "updated")
if item == "website:awards":
return _latest(Award, "date")
if item == "website:news_listing":
return _latest(News, "date")
if item == "website:index":
# Home page surfaces recent content across sections; use the most
# recent of news, publications, and project updates.
candidates = [
_latest(News, "date"),
_latest(Publication, "date"),
_latest(Project, "updated"),
]
candidates = [c for c in candidates if c is not None]
return max(candidates) if candidates else None
return None


class ProjectSitemap(_HttpsSitemap):
"""Public project pages: /project/<short_name>/."""
Expand Down
13 changes: 13 additions & 0 deletions website/tests/test_sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ def test_sitemap_includes_news_item(self):
body = self.client.get("/sitemap.xml").content.decode()
self.assertIn(f"/news/{news.slug}/", body)

def test_static_listing_pages_have_lastmod(self):
# The listing pages should advertise a <lastmod> sourced from their
# most-recent content, not be the only entries with none. Create a news
# item so the news/home/listing sections are non-empty.
self.make_news_item(title="Dated News")
body = self.client.get("/sitemap.xml").content.decode()
# Pull the <url> block for the /news/ listing and assert it carries a
# <lastmod>. (Detail-page news URLs look like /news/<slug>/.)
url_blocks = re.findall(r"<url>(.*?)</url>", body, re.DOTALL)
listing = [b for b in url_blocks if re.search(r"<loc>[^<]*/news/</loc>", b)]
self.assertTrue(listing, "expected a /news/ listing entry in the sitemap")
self.assertIn("<lastmod>", listing[0])

def test_sitemap_uses_https_scheme(self):
# Apache proxies to Django over plain HTTP, so without a pinned
# protocol the <loc> URLs would be http:// and only 302-redirect to
Expand Down
Loading