Skip to content

Commit

Permalink
Add more record details
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Jan 11, 2024
1 parent d446f89 commit 353ca65
Show file tree
Hide file tree
Showing 14 changed files with 606 additions and 56 deletions.
193 changes: 183 additions & 10 deletions app/catalogue/routes.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,195 @@
import urllib.parse

from app.catalogue import bp
from app.lib import cache, cache_key_prefix
from app.lib import cache, cache_key_prefix, parse_cdata
from flask import current_app, render_template

from .api import RecordsAPI


@bp.route("/id/<id>/")
@cache.cached(key_prefix=cache_key_prefix)
def record(id):
def info(id):
records_api = RecordsAPI()
records_api.set_record_id(id)
records_api.add_parameter("includeSource", True)
record_data = records_api.get_results()
data = record_data["metadata"][0]["detail"]["@template"]["details"]
if data["type"] == "record":
return render_template("catalogue/record.html", id=id, data=data)
if data["type"] == "archive":
return render_template("catalogue/archive.html", id=id, data=data)
current_app.logger.error(f"Template for {data['type']} not handled")
type = record_data["metadata"][0]["_source"]["@datatype"]["base"]
if type == "record":
return render_record(id, record_data)
if type == "archive":
return render_archive(id, record_data)
if type == "agent":
return render_agent(id, record_data)

current_app.logger.error(f"Template for {type} not handled")
return render_template("errors/page-not-found.html"), 404


def render_record(id, record_data):
data = record_data["metadata"][0]["detail"]["@template"]["details"]
return render_template("catalogue/record.html", id=id, data=data)


def render_archive(id, record_data):
data = record_data["metadata"][0]["detail"]["@template"]["details"]
source = record_data["metadata"][0]["_source"]
place = source["place"][0]
agents = source["agent"]
businesses = [
agent for agent in agents if agent["identifier"][0]["value"] == "B"
]
diaries = [
agent for agent in agents if agent["identifier"][0]["value"] == "D"
]
families = [
agent for agent in agents if agent["identifier"][0]["value"] == "F"
]
organisations = [
agent for agent in agents if agent["identifier"][0]["value"] == "O"
]
persons = [
agent for agent in agents if agent["identifier"][0]["value"] == "P"
]
manifestations = source["manifestations"]
return render_template(
"catalogue/archive.html",
id=id,
data=data,
place=place,
businesses=businesses,
diaries=diaries,
families=families,
organisations=organisations,
persons=persons,
manifestations=manifestations,
)


def render_agent(id, record_data):
data = record_data["metadata"][0]["detail"]["@template"]["details"]
source = record_data["metadata"][0]["_source"]
details = {}

if "name" in source:
name = next(
(
item
for item in source["name"]
if "primary" in item and item["primary"]
),
None,
)
if name:
if "last" in name:
details["Surname"] = name["last"]
if "first" in name:
details["Forenames"] = " ".join(name["first"])
if "title" in name:
details["Title"] = name["title"]
if "title_prefix" in name:
details["Pretitle"] = name["title_prefix"]

aka = next(
(
item["value"]
for item in source["name"]
if "type" in item and item["type"] == "also known as"
),
None,
)
if aka:
details["Alternative name(s)"] = aka

if "birth" in source or "death" in source:
date_from = (
source["birth"]["date"]["value"] if "birth" in source else ""
)
date_to = source["death"]["date"]["value"] if "death" in source else ""
details["Date"] = f"{date_from}&ndash;{date_to}"
elif "start" in source or "end" in source:
date_from = (
source["start"]["date"][0]["value"] if "start" in source else ""
)
date_to = source["end"]["date"][0]["value"] if "end" in source else ""
details["Date"] = f"{date_from}&ndash;{date_to}"

if "place" in source:
details["Places"] = "<br>".join(
place["name"][0]["value"] for place in source["place"]
)

if "gender" in source:
details["Gender"] = (
"Male"
if source["gender"] == "M"
else "Female"
if source["gender"] == "F"
else source["gender"]
)

if "description" in source:
functions = next(
(
item["value"]
for item in source["description"]
if "type" in item
and item["type"] == "functions, occupations and activities"
),
None,
)
if functions:
details["Functions, occupations and activities"] = parse_cdata(
functions
)

history = next(
(
item["value"]
for item in source["description"]
if "type" in item and item["type"] == "history"
),
None,
)
if history:
details["History"] = history

biography = next(
(
item
for item in source["description"]
if "type" in item and item["type"] == "biography"
),
None,
)
if biography:
url = biography["url"]
text = biography["value"]
url = f'<a href="{url}">{text}</a>'
details["Biography"] = url

if "identifier" in source:
primary_identifier = next(
(
item["value"]
for item in source["identifier"]
if "type" in item and item["type"] == "name authority reference"
),
None,
)
former_identifier = next(
(
item["value"]
for item in source["identifier"]
if "type" in item
and item["type"] == "former name authority reference"
),
None,
)
details["Name authority identifier"] = (
f"{primary_identifier} (Former ISAAR ref: {former_identifier})"
if former_identifier
else primary_identifier
)

return render_template(
"catalogue/agent.html", id=id, data=data, details=details
)
1 change: 1 addition & 0 deletions app/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .cache_key_prefix import cache_key_prefix
from .pagination import pagination_list, pagination_object
from .query import parse_args, remove_arg
from .xml import parse_cdata
3 changes: 2 additions & 1 deletion app/lib/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
def parse_args(args):
args_dict = args.to_dict(flat=False)
return_args = {}
print(args_dict)
for arg_key in args_dict:
if arg_key.endswith("[]"):
arg_key_clipped = arg_key.removesuffix("[]")
return_args[arg_key_clipped] = args_dict[arg_key]
else:
return_args[arg_key] = args_dict[arg_key]
return_args[arg_key] = args_dict[arg_key][0]
return return_args


Expand Down
9 changes: 9 additions & 0 deletions app/lib/xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pyquery import PyQuery


def parse_cdata(val):
document = PyQuery(val)
for tag in ("foa", "function"):
if doc_value := document(tag).text():
return doc_value
return val
4 changes: 4 additions & 0 deletions app/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class RecordsAPI(BaseSearchAPI):
api_path = "/records/"


class RecordFiltersAPI(BaseSearchAPI):
api_path = "/records/filters/"


class ArticlesAPI(BaseSearchAPI):
api_path = "/articles/"

Expand Down
76 changes: 59 additions & 17 deletions app/search/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from app.search import bp
from flask import render_template, request

from .api import ArticleFiltersAPI, ArticlesAPI, RecordsAPI
from .api import ArticleFiltersAPI, ArticlesAPI, RecordFiltersAPI, RecordsAPI


@bp.route("/")
Expand Down Expand Up @@ -34,6 +34,7 @@ def featured():
def catalogue():
query = request.args["q"] if "q" in request.args else ""
page = int(request.args["page"]) if "page" in request.args else 1
args = parse_args(request.args)
records_api = RecordsAPI()
records_api.query(query)
records_api.add_parameter("highlight", True)
Expand All @@ -43,12 +44,15 @@ def catalogue():
return render_template("errors/api.html"), 502
except Exception:
return render_template("errors/page-not-found.html"), 404
filters = get_filters(RecordFiltersAPI, args)
selected_filters = get_selected_filters(filters)
return render_template(
"search/catalogue.html",
query=query,
search_path="/search/catalogue/",
results=results,
# filters=filters,
filters=filters,
selected_filters=selected_filters,
page=page,
pages=results["pages"],
pagination=pagination_object(page, results["pages"], request.args),
Expand Down Expand Up @@ -89,14 +93,34 @@ def website():
return render_template("errors/api.html"), 502
except Exception:
return render_template("errors/page-not-found.html"), 404
article_filters_api = ArticleFiltersAPI()
article_filters_api.params = (
filters = get_filters(ArticleFiltersAPI, args)
selected_filters = get_selected_filters(filters)
return render_template(
"search/website.html",
query=query,
search_path="/search/website/",
results=results,
filters=filters,
selected_filters=selected_filters,
page=page,
pages=results["pages"],
pagination=pagination_object(page, results["pages"], request.args),
)


def get_filters(api, args):
filters_api = api()
filters_api.params = (
{}
) # TODO: Why do I need to do this? Things are persisting...
filters = [
return [
{
"title": filter["title"],
"type": filter["type"],
"slug": slugify(filter["title"]),
"value": args[slugify(filter["title"])]
if filter["type"] == "text" and slugify(filter["title"]) in args
else "",
"options": [
{
"text": option["name"],
Expand All @@ -111,17 +135,35 @@ def website():
),
}
for option in filter["options"]
],
]
if filter["type"] == "multiple"
else [],
}
for filter in article_filters_api.get_results()
for filter in filters_api.get_results()
]
return render_template(
"search/website.html",
query=query,
search_path="/search/website/",
results=results,
filters=filters,
page=page,
pages=results["pages"],
pagination=pagination_object(page, results["pages"], request.args),
)


def get_selected_filters(filters):
selected_filters = []
for filter in filters:
if filter["type"] == "multiple":
for option in filter["options"]:
if option["checked"]:
selected_filters.append(
{
"group": filter["title"],
"filter": option["text"],
"remove_url": option["remove_url"],
}
)
if filter["type"] == "text" and filter["value"]:
selected_filters.append(
{
"group": filter["title"],
"filter": filter["value"],
"remove_url": remove_arg(
request.args, slugify(filter["title"])
),
}
)
return selected_filters
Loading

0 comments on commit 353ca65

Please sign in to comment.