Skip to content

Commit 1d2744c

Browse files
committed
Drive the news entry on the topbar from PinnedNewsArticle
This will generate a query for every single page on the site in development mode, but that's OK - in production we're using ESI and will cache the result of it.
1 parent b140725 commit 1d2744c

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

pgweb/news/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def purge_urls(self):
5858
yield '/news/.*.rss'
5959
# FIXME: when to expire the front page?
6060
yield '/$'
61+
# If this is the pinned article, we need to purge the include
62+
if self.pinnednewsarticle_set.exists():
63+
yield '/include/topbar/'
6164

6265
def __str__(self):
6366
return "%s: %s" % (self.date, self.title)
@@ -132,6 +135,8 @@ class PinnedNewsArticle(models.Model):
132135
pinnedarticle = models.ForeignKey(NewsArticle, null=True, blank=True, on_delete=models.SET_NULL)
133136
pinnedtoproviders = models.JSONField(null=False, blank=True, default=dict)
134137

138+
purge_urls = ('/include/topbar/', )
139+
135140
def save(self, *args, **kwargs):
136141
if not self.pk and PinnedNewsArticle.objects.exists():
137142
raise ValidationError("Only one PinnedNewsArticle may exist!")

pgweb/util/contexts.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from django.shortcuts import render
33
from django.conf import settings
44

5+
from functools import cache
6+
57
# This is the whole site navigation structure. Stick in a smarter file?
68
sitenav = {
79
'about': [
@@ -128,16 +130,23 @@ def _get_gitrev():
128130
# the current git revision. git revision is returned as a lazy object so
129131
# we don't spend effort trying to load it if we don't need it (though
130132
# all general pages will need it since it's used to render the css urls)
133+
#
134+
# Topbarnews needs to be available for the include topbar in cases when ESI
135+
# is not available. It is also evaluated lazily so on production website it
136+
# will only actually cause a query on the include page. PinnedNewsArticle has to
137+
# be imported here, to avoid recursive imports.
131138
def PGWebContextProcessor(request):
139+
from pgweb.news.models import PinnedNewsArticle
140+
132141
gitrev = SimpleLazyObject(_get_gitrev)
142+
ctx = {
143+
'gitrev': gitrev,
144+
'do_esi': settings.DO_ESI,
145+
'topbarnews': cache(lambda: PinnedNewsArticle.objects.select_related('pinnedarticle').only('pinnedarticle__id', 'pinnedarticle__date', 'pinnedarticle__title').first().pinnedarticle),
146+
}
133147
if request.is_secure():
134-
return {
148+
return ctx | {
135149
'link_root': settings.SITE_ROOT,
136-
'do_esi': settings.DO_ESI,
137-
'gitrev': gitrev,
138150
}
139151
else:
140-
return {
141-
'gitrev': gitrev,
142-
'do_esi': settings.DO_ESI,
143-
}
152+
return ctx
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
June 4, 2026: <a href="/about/news/postgresql-19-beta-1-released-3313/">
2-
PostgreSQL 19 Beta 1 Released!
3-
</a>
1+
{% if topbarnews %}{{ topbarnews.date | date:"F j, Y" }}: <a href="{{ topbarnews.permanenturl }}">{{ topbarnews.title }}</a>{% endif %}

0 commit comments

Comments
 (0)