|
| 1 | +""" |
| 2 | +Sitemap classes that power the dynamic ``/sitemap.xml`` (issue #1252). |
| 3 | +
|
| 4 | +Django's ``django.contrib.sitemaps`` framework builds the XML on every request |
| 5 | +straight from our querysets, so the sitemap is always current — no static file |
| 6 | +to regenerate when content changes. |
| 7 | +
|
| 8 | +Domain handling: we do NOT use ``django.contrib.sites``. When it isn't |
| 9 | +installed, the framework falls back to ``RequestSite``, which takes the domain |
| 10 | +from the incoming request host. That means the same code emits |
| 11 | +``makeabilitylab.cs.washington.edu`` in prod, ``makeabilitylab-test...`` on the |
| 12 | +test server, and ``localhost`` in dev — no per-environment configuration. |
| 13 | +
|
| 14 | +We map only the pages that have real, indexable URLs: |
| 15 | + - static listing pages (home, people, publications, projects, awards, news) |
| 16 | + - one entry per visible Project -> /project/<short_name>/ |
| 17 | + - one entry per public Person -> /member/<url_name>/ |
| 18 | + - one entry per News item -> /news/<slug>/ |
| 19 | +
|
| 20 | +Publications have no per-publication detail page (only the ``/publications/`` |
| 21 | +listing), so they are covered by the static sitemap and not enumerated here. |
| 22 | +""" |
| 23 | + |
| 24 | +from django.contrib.sitemaps import Sitemap |
| 25 | +from django.urls import reverse |
| 26 | + |
| 27 | +from website.models import Project, Person, News |
| 28 | + |
| 29 | + |
| 30 | +class StaticViewSitemap(Sitemap): |
| 31 | + """Top-level listing pages that aren't tied to a single model instance.""" |
| 32 | + |
| 33 | + changefreq = "weekly" |
| 34 | + priority = 0.8 |
| 35 | + |
| 36 | + def items(self): |
| 37 | + # URL names (in the ``website`` namespace) for the public landing pages. |
| 38 | + return [ |
| 39 | + "website:index", |
| 40 | + "website:people", |
| 41 | + "website:publications", |
| 42 | + "website:projects", |
| 43 | + "website:awards", |
| 44 | + "website:news_listing", |
| 45 | + ] |
| 46 | + |
| 47 | + def location(self, item): |
| 48 | + return reverse(item) |
| 49 | + |
| 50 | + |
| 51 | +class ProjectSitemap(Sitemap): |
| 52 | + """Public project pages: /project/<short_name>/.""" |
| 53 | + |
| 54 | + changefreq = "weekly" |
| 55 | + priority = 0.7 |
| 56 | + |
| 57 | + def items(self): |
| 58 | + # Mirror the project view's visibility rule: only is_visible projects |
| 59 | + # are reachable by the public. Explicit ordering keeps sitemap |
| 60 | + # pagination stable (avoids UnorderedObjectListWarning). |
| 61 | + return Project.objects.filter(is_visible=True).order_by("short_name") |
| 62 | + |
| 63 | + def location(self, obj): |
| 64 | + # The project view resolves short_name (see website/views/project.py). |
| 65 | + return reverse("website:project", args=[obj.short_name]) |
| 66 | + |
| 67 | + def lastmod(self, obj): |
| 68 | + # auto_now DateField, updated on every save. |
| 69 | + return obj.updated |
| 70 | + |
| 71 | + |
| 72 | +class PersonSitemap(Sitemap): |
| 73 | + """Public people pages: /member/<url_name>/.""" |
| 74 | + |
| 75 | + changefreq = "monthly" |
| 76 | + priority = 0.6 |
| 77 | + |
| 78 | + def items(self): |
| 79 | + # Anyone who has held a position appears on the /people/ page and has a |
| 80 | + # public member page. Exclude the 'placeholder' default url_name (people |
| 81 | + # whose url_name was never generated) since those won't resolve. |
| 82 | + return ( |
| 83 | + Person.objects.filter(position__isnull=False) |
| 84 | + .exclude(url_name="placeholder") |
| 85 | + .order_by("url_name") |
| 86 | + .distinct() |
| 87 | + ) |
| 88 | + |
| 89 | + def location(self, obj): |
| 90 | + return reverse("website:member_by_name", args=[obj.url_name]) |
| 91 | + |
| 92 | + def lastmod(self, obj): |
| 93 | + # May be None for people whose bio was never edited; the framework |
| 94 | + # simply omits <lastmod> in that case. |
| 95 | + return obj.bio_datetime_modified |
| 96 | + |
| 97 | + |
| 98 | +class NewsSitemap(Sitemap): |
| 99 | + """News item pages: /news/<slug>/.""" |
| 100 | + |
| 101 | + changefreq = "monthly" |
| 102 | + priority = 0.5 |
| 103 | + |
| 104 | + def items(self): |
| 105 | + return News.objects.exclude(slug__isnull=True) |
| 106 | + |
| 107 | + def location(self, obj): |
| 108 | + return reverse("website:news_item_by_slug", args=[obj.slug]) |
| 109 | + |
| 110 | + def lastmod(self, obj): |
| 111 | + return obj.date |
| 112 | + |
| 113 | + |
| 114 | +# Registry passed to django.contrib.sitemaps.views.sitemap in the root URLconf. |
| 115 | +sitemaps = { |
| 116 | + "static": StaticViewSitemap, |
| 117 | + "projects": ProjectSitemap, |
| 118 | + "people": PersonSitemap, |
| 119 | + "news": NewsSitemap, |
| 120 | +} |
0 commit comments