From c4dc5cd4c890bc073778434ac5f3f8b4a3f36131 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Thu, 11 Jan 2024 16:15:34 +0000 Subject: [PATCH] Add RecordParser --- app/catalogue/api.py | 162 +++++++++++++++++++++++++++++++++++++++- app/catalogue/routes.py | 130 +------------------------------- 2 files changed, 165 insertions(+), 127 deletions(-) diff --git a/app/catalogue/api.py b/app/catalogue/api.py index d5888f09..65835a81 100644 --- a/app/catalogue/api.py +++ b/app/catalogue/api.py @@ -1,7 +1,7 @@ import json import requests -from app.lib import BaseAPI +from app.lib import BaseAPI, parse_cdata from config import Config from flask import current_app @@ -25,3 +25,163 @@ def parse_response(self, response): raise Exception("Resource not found") except requests.exceptions.JSONDecodeError: raise ConnectionError("API provided non-JSON response") + + +class RecordParser: + def __init__(self, rosetta_data): + self.data = rosetta_data + self.source = self.data["metadata"][0]["_source"] + + def all(self): + return ( + self.names() + | self.date() + | self.places() + | self.gender() + | self.descriptions() + | self.identifiers() + ) + + def names(self): + names = {} + if "name" in self.source: + name_data = next( + ( + item + for item in self.source["name"] + if "primary" in item and item["primary"] + ), + None, + ) + if name_data: + if "last" in name_data: + names["Surname"] = name_data["last"] + if "first" in name_data: + names["Forenames"] = " ".join(name_data["first"]) + if "title" in name_data: + names["Title"] = name_data["title"] + if "title_prefix" in name_data: + names["Pretitle"] = name_data["title_prefix"] + aka = next( + ( + item["value"] + for item in self.source["name"] + if "type" in item and item["type"] == "also known as" + ), + None, + ) + if aka: + names["Alternative name(s)"] = aka + return names + + def date(self): + date = {} + source = self.source + 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 "" + ) + date["Date"] = f"{date_from}–{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 "" + ) + date["Date"] = f"{date_from}–{date_to}" + return date + + def places(self): + places = {} + if "place" in self.source: + places["Places"] = "
".join( + place["name"][0]["value"] for place in self.source["place"] + ) + return places + + def gender(self): + gender = {} + if "gender" in self.source: + gender["Gender"] = ( + "Male" + if self.source["gender"] == "M" + else "Female" + if self.source["gender"] == "F" + else self.source["gender"] + ) + return gender + + def descriptions(self): + descriptions = {} + if "description" in self.source: + functions = next( + ( + item["value"] + for item in self.source["description"] + if "type" in item + and item["type"] == "functions, occupations and activities" + ), + None, + ) + if functions: + descriptions[ + "Functions, occupations and activities" + ] = parse_cdata(functions) + + history = next( + ( + item["value"] + for item in self.source["description"] + if "type" in item and item["type"] == "history" + ), + None, + ) + if history: + descriptions["History"] = history + + biography = next( + ( + item + for item in self.source["description"] + if "type" in item and item["type"] == "biography" + ), + None, + ) + if biography: + url = biography["url"] + text = biography["value"] + url = f'{text}' + descriptions["Biography"] = url + return descriptions + + def identifiers(self): + identifiers = {} + if "identifier" in self.source: + primary_identifier = next( + ( + item["value"] + for item in self.source["identifier"] + if "type" in item + and item["type"] == "name authority reference" + ), + None, + ) + former_identifier = next( + ( + item["value"] + for item in self.source["identifier"] + if "type" in item + and item["type"] == "former name authority reference" + ), + None, + ) + identifiers["Name authority identifier"] = ( + f"{primary_identifier} (Former ISAAR ref: {former_identifier})" + if former_identifier + else primary_identifier + ) + return identifiers diff --git a/app/catalogue/routes.py b/app/catalogue/routes.py index 37165b06..b8eac2b5 100644 --- a/app/catalogue/routes.py +++ b/app/catalogue/routes.py @@ -1,8 +1,8 @@ from app.catalogue import bp -from app.lib import cache, cache_key_prefix, parse_cdata +from app.lib import cache, cache_key_prefix from flask import current_app, render_template -from .api import RecordsAPI +from .api import RecordParser, RecordsAPI @bp.route("/id//") @@ -65,131 +65,9 @@ def render_archive(id, record_data): def render_agent(id, record_data): + record = RecordParser(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}–{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}–{date_to}" - - if "place" in source: - details["Places"] = "
".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'{text}' - 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 - ) - + details = record.all() return render_template( "catalogue/agent.html", id=id, data=data, details=details )