Skip to content

Commit

Permalink
Add RecordParser
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Jan 11, 2024
1 parent 353ca65 commit c4dc5cd
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 127 deletions.
162 changes: 161 additions & 1 deletion app/catalogue/api.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"] = "<br>".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'<a href="{url}">{text}</a>'
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
130 changes: 4 additions & 126 deletions app/catalogue/routes.py
Original file line number Diff line number Diff line change
@@ -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/<id>/")
Expand Down Expand Up @@ -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}&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
)

details = record.all()
return render_template(
"catalogue/agent.html", id=id, data=data, details=details
)

0 comments on commit c4dc5cd

Please sign in to comment.