Skip to content

Commit

Permalink
Merge pull request #15 from nationalarchives/feature/rss
Browse files Browse the repository at this point in the history
Add Atom feeds
  • Loading branch information
ahosgood authored Oct 21, 2024
2 parents 3645df4 + d80e1d1 commit 84d0cdc
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 8 deletions.
86 changes: 78 additions & 8 deletions app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
from urllib.parse import quote, unquote, urlparse

from app.lib import cache, cache_key_prefix
from app.lib.api import ApiResourceNotFound
from app.lib.util import strtobool
from app.main import bp
from app.wagtail.api import all_pages, global_alerts
from flask import (
current_app,
make_response,
redirect,
render_template,
request,
url_for,
from app.wagtail.api import (
all_pages,
blog_posts_paginated,
global_alerts,
page_details_by_type,
)
from flask import current_app, make_response, redirect, render_template, request
from flask_caching import CachedResponse


@bp.route("/healthcheck/live/")
Expand Down Expand Up @@ -134,6 +134,76 @@ def sitemap():
return response


@bp.route("/rss/all.xml")
@cache.cached(key_prefix=cache_key_prefix)
def blog_all_rss():
try:
blog_data = page_details_by_type("blog.BlogIndexPage")
blog_data = blog_data["items"][0]
blog_posts = blog_posts_paginated(1, limit=100)
blog_posts = blog_posts["items"]
except ConnectionError:
return CachedResponse(
response=make_response(render_template("errors/api.html"), 502),
timeout=1,
)
except ApiResourceNotFound:
return CachedResponse(
response=make_response(
render_template("errors/page-not-found.html"), 404
),
timeout=1,
)
except Exception:
return CachedResponse(
response=make_response(render_template("errors/api.html"), 502),
timeout=1,
)
xml_rss = render_template(
"main/rss.xml",
blog_data=blog_data,
blog_posts=blog_posts,
)
response = make_response(xml_rss)
response.headers["Content-Type"] = "application/atom+xml; charset=UTF-8"
return response


@bp.route("/rss/<int:blog_id>.xml")
@cache.cached(key_prefix=cache_key_prefix)
def blog_rss(blog_id):
try:
blog_data = page_details_by_type("blog.BlogIndexPage")
blog_data = blog_data["items"][0]
blog_posts = blog_posts_paginated(1, blog_id=blog_id, limit=100)
blog_posts = blog_posts["items"]
except ConnectionError:
return CachedResponse(
response=make_response(render_template("errors/api.html"), 502),
timeout=1,
)
except ApiResourceNotFound:
return CachedResponse(
response=make_response(
render_template("errors/page-not-found.html"), 404
),
timeout=1,
)
except Exception:
return CachedResponse(
response=make_response(render_template("errors/api.html"), 502),
timeout=1,
)
xml_rss = render_template(
"main/rss.xml",
blog_data=blog_data,
blog_posts=blog_posts,
)
response = make_response(xml_rss)
response.headers["Content-Type"] = "application/atom+xml; charset=UTF-8"
return response


@bp.route("/new-homepage/")
@cache.cached(key_prefix=cache_key_prefix)
def new_homepage():
Expand Down
5 changes: 5 additions & 0 deletions app/templates/blog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
{% else %}
<link rel="canonical" href="{{ request.url }}?page={{ page }}">
{% endif %}
{%- if page_data.meta.type == 'blog.BlogIndexPage' %}
<link rel="alternate" type="application/atom+xml" href="{{ url_for('main.blog_all_rss', _external=True) }}" title="Sitewide Atom feed">
{%- elif page_data.meta.type == 'blog.BlogPage' %}
<link rel="alternate" type="application/atom+xml" href="{{ url_for('main.blog_rss', blog_id=page_data.id, _external=True) }}" title="Atom feed for {{ page_data.title }}">
{%- endif %}
{% endblock %}

{% block stylesheets %}
Expand Down
32 changes: 32 additions & 0 deletions app/templates/main/rss.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{ blog_data.title }}</title>
<subtitle>{{ blog_data.teaser_text }}</subtitle>
<link href="{{ request.url }}" rel="self" />
<link href="{{ blog_data.full_url }}" />
<updated>{{ now_iso_8601() }}</updated>
<id>{{ url_for('wagtail.page_permalink', page_id=blog_data.id, _external=True) }}</id>
<icon>{{ url_for('static', filename='assets/images/icon-128x128.png', _external=True) }}</icon>
{% for blog_post in blog_posts %}
<entry>
<title>{{ blog_post.title }}</title>
<link href="{{ blog_post.full_url }}" />
<published>{{ blog_post.published_date.value }}</published>
<updated>{{ blog_post.last_published_at }}</updated>
<id>{{ url_for('wagtail.page_permalink', page_id=blog_post.id, _external=True) }}</id>
<summary>{{ blog_post.teaser_text }}</summary>
{% if blog_post.authors %}
{% for author in blog_post.authors %}
<author>
<name>{{ author.title }}</name>
<uri>{{ author.full_url }}</uri>
</author>
{% endfor %}
{% else %}
<author>
<name>The National Archives</name>
</author>
{% endif %}
</entry>
{% endfor %}
</feed>
8 changes: 8 additions & 0 deletions app/wagtail/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def page_details_by_uri(page_uri, params={}):
return wagtail_request_handler(uri, params)


def page_details_by_type(type, params={}):
uri = "pages/"
params = params | {
"type": type,
}
return wagtail_request_handler(uri, params)


def page_children(page_id, params={}, limit=None):
uri = "pages/"
params = params | {
Expand Down
34 changes: 34 additions & 0 deletions app/wagtail/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,40 @@ def preview_protected_page(id):
)


@bp.route("/page/<int:page_id>/")
def page_permalink(page_id):
try:
page_data = page_details(page_id)
except ConnectionError:
return CachedResponse(
response=make_response(render_template("errors/api.html"), 502),
timeout=1,
)
except ApiResourceNotFound:
return CachedResponse(
response=make_response(
render_template("errors/page-not-found.html"), 404
),
timeout=1,
)
except Exception as e:
current_app.logger.error(e)
return CachedResponse(
response=make_response(render_template("errors/api.html"), 502),
timeout=1,
)
if "meta" in page_data and "url" in page_data["meta"]:
return redirect(
url_for("wagtail.page", path=page_data["meta"]["url"].strip("/")),
code=302,
)
current_app.logger.error(f"Cannot generate permalink for page: {page_id}")
return CachedResponse(
response=make_response(render_template("errors/api.html"), 502),
timeout=1,
)


@bp.route("/")
@cache.cached(key_prefix=cache_key_prefix)
def index():
Expand Down

0 comments on commit 84d0cdc

Please sign in to comment.