Skip to content

Commit

Permalink
Merge pull request #17 from nationalarchives/feature/person-articles
Browse files Browse the repository at this point in the history
Paginated authored articles
  • Loading branch information
ahosgood authored Nov 25, 2024
2 parents b32eb34 + 8c26f92 commit 66f2511
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
18 changes: 10 additions & 8 deletions app/templates/people/person.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ <h1 class="tna-heading-xl" itemprop="name">{{ page_data.title }}</h1>
'icon': 'arrow-left'
}) }}
</div>
<h2 class="tna-heading-l tna-!--no-margin-top">Articles by {{ page_data.first_name }}, page {{ page }} of {{ pages }}</h2>
<h2 class="tna-heading-l tna-!--no-margin-top">Articles by {{ page_data.first_name }}{% if pages > 1 %}, page {{ page }} of {{ pages }}{% endif %}</h2>
</div>
</div>
<ul class="tna-container">
{%- for article in page_data.authored_focused_articles %}
{%- for article in articles %}
<li class="tna-column tna-column--width-1-3 tna-column--width-1-2-medium tna-column--small-tiny tna-column--full-tiny tna-!--margin-top-l">
{% set card_content = {
'supertitle': article.type_label,
Expand Down Expand Up @@ -124,10 +124,12 @@ <h2 class="tna-heading-l tna-!--no-margin-top">Articles by {{ page_data.first_na
</li>
{%- endfor %}
</ul>
<div class="tna-container">
<div class="tna-column tna-column--full tna-!--margin-top-xl">
<div class="tna-container tna-!--margin-top-xl">
{% if pages > 1 %}
<div class="tna-column tna-column--full">
{{ tnaPagination(pagination) }}
</div>
{% endif %}
</div>
{% else %}
<div class="tna-container tna-container--centred">
Expand All @@ -142,7 +144,7 @@ <h2 class="tna-heading-l tna-!--no-margin-top">Articles by {{ page_data.first_na
'href': '#research-activity'
}] %}
{%- endif %}
{%- if page_data.authored_focused_articles %}
{%- if articles %}
{% set sidebar_items = sidebar_items + [{
'text': 'Articles',
'href': '#articles'
Expand Down Expand Up @@ -178,12 +180,12 @@ <h2 id="research-activity" class="tna-heading-l tna-!--padding-top-s">Research a
{{ wagtail_body(page_data.research_summary, 3, page_data.footnotes) }}
</section>
{%- endif %}
{%- if page_data.authored_focused_articles %}
{%- if articles %}
<hr class="tna-!--margin-top-s">
<section class="tna-section tna-!--no-padding-bottom etna-article__section">
<h2 id="articles" class="tna-heading-l tna-!--padding-bottom-m">Articles</h2>
<ul class="tna-container tna-container--nested">
{%- for article in page_data.authored_focused_articles %}
{%- for article in articles %}
<li class="tna-column tna-column--width-1-2 tna-column--full-tiny tna-!--margin-bottom-l">
{% set card_content = {
'supertitle': article.type_label,
Expand Down Expand Up @@ -219,7 +221,7 @@ <h2 id="articles" class="tna-heading-l tna-!--padding-bottom-m">Articles</h2>
</li>
{%- endfor %}
</ul>
{% if pages %}
{% if more_articles %}
<div class="tna-button-group tna-button-group--centred tna-!--margin-bottom-l">
{{ tnaButton({
'text': 'Browse all ' + page_data.first_name + '’s articles',
Expand Down
13 changes: 13 additions & 0 deletions app/wagtail/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ def page_children_paginated(
)


def authored_pages_paginated(
author_id,
page,
limit=None,
params={},
):
return pages_paginated(
page=page,
limit=limit,
params=params | {"author": author_id},
)


def page_descendants(
page_id,
params={},
Expand Down
32 changes: 29 additions & 3 deletions app/wagtail/pages/person_page.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import math

from app.lib.pagination import pagination_object
from app.wagtail.api import breadcrumbs
from flask import render_template, request
from app.wagtail.api import authored_pages_paginated, breadcrumbs
from flask import current_app, render_template, request
from pydash import objects


def person_page(page_data):
articles_preview_list = 4
articles_per_page = 12
page = (
int(request.args.get("page"))
if request.args.get("page") and request.args.get("page").isnumeric()
else 0
)
pages = 0 # TODO
try:
articles = authored_pages_paginated(
page_data["id"],
page or 1,
articles_per_page if page else articles_preview_list,
)
except ConnectionError:
current_app.logger.error(
f"API error getting author articles for page {page_data['id']}"
)
return render_template("errors/api.html"), 502
except Exception:
current_app.logger.error(
f"Exception getting author articles for page {page_data['id']}"
)
return render_template("errors/server.html"), 500
total_article_count = objects.get(articles, "meta.total_count", 0)
print(total_article_count)
articles = objects.get(articles, "items", [])
pages = math.ceil(total_article_count / articles_per_page)
if page > pages:
return render_template("errors/page-not-found.html"), 404
return render_template(
Expand All @@ -19,4 +43,6 @@ def person_page(page_data):
pagination=pagination_object(page, pages, request.args),
page=page,
pages=pages,
articles=articles,
more_articles=total_article_count > articles_preview_list,
)

0 comments on commit 66f2511

Please sign in to comment.