Skip to content

Commit

Permalink
feat: divide object dating field in two - era & period
Browse files Browse the repository at this point in the history
  • Loading branch information
wiwski committed Jun 25, 2024
1 parent ca72aae commit af991ea
Show file tree
Hide file tree
Showing 29 changed files with 566 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Result } from "../type-ahead-list.component";
import {
OpenThesoTypeAhead,
SearchType,
OpenThesoResult,
} from "./open-theso-type-ahead";

export class DatingOpenThesoTypeAhead extends OpenThesoTypeAhead {
searchType: SearchType = "fullpathSearch";

connectedCallback(): void {
this.thesorusId = this.getAttribute("thesorus-id") || "";
super.connectedCallback();
}

async fetchResults(query: string): Promise<Result[]> {
const data = await this.doFetch<OpenThesoResult[]>(query);
return data.map((item: OpenThesoResult[]) => ({
label: item.map((i) => i.label).join(" > "),
id: item.slice(-1)[0].id,
})) as Result[];
}
}

customElements.define(
"dating-open-theso-type-ahead",
DatingOpenThesoTypeAhead,
{
extends: "div",
},
);
37 changes: 12 additions & 25 deletions euphrosyne/assets/js/web-components/material-type-ahead.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
import { TypeAheadList, Result } from "../type-ahead-list.component";

interface OpenThesoResult {
id: string;
arkId: string;
label: string;
}
import { Result } from "../type-ahead-list.component";
import {
OpenThesoResult,
OpenThesoTypeAhead,
SearchType,
} from "./open-theso-type-ahead";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
class MaterialTypeAhead extends TypeAheadList {
async fetchResults(query: string): Promise<Result[]> {
const q = encodeURIComponent(query);
const response = await fetch(
`https://opentheso.huma-num.fr/opentheso/openapi/v1/concept/th291/autocomplete/${q}?lang=fr&exactMatch=false`,
);
class MaterialTypeAhead extends OpenThesoTypeAhead {
thesorusId = "th291";
searchType: SearchType = "autocomplete";

if (response && response.status === 404) {
return [];
}
if (!response || !response.ok) {
throw new Error("Failed to fetch results");
}

const data = await response.json();
if (!data || !data.length) {
return [];
}
async fetchResults(query: string): Promise<Result[]> {
const data = await this.doFetch<OpenThesoResult>(query);
return data.map((item: OpenThesoResult) => ({
label: item.label,
id: item.id,
}));
})) as Result[];
}
}

Expand Down
44 changes: 44 additions & 0 deletions euphrosyne/assets/js/web-components/open-theso-type-ahead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TypeAheadList } from "../type-ahead-list.component";

export type SearchType = "fullpathSearch" | "autocomplete";

export interface OpenThesoResult {
id: string;
arkId: string;
label: string;
}

export abstract class OpenThesoTypeAhead extends TypeAheadList {
thesorusId?: string;
abstract searchType: SearchType;

async doFetch<T>(query: string): Promise<T[]> {
if (!this.thesorusId) {
throw new Error("thesorus-id attribute is required");
}
const q = encodeURIComponent(query);

let url;

if (this.searchType === "fullpathSearch") {
url = `https://opentheso.huma-num.fr/opentheso/openapi/v1/concept/${this.thesorusId}/search/fullpath?q=${q}&lang=fr&exactMatch=false`;
} else {
// autocomplete
url = `https://opentheso.huma-num.fr/opentheso/openapi/v1/concept/${this.thesorusId}/autocomplete/${q}?lang=fr&exactMatch=false`;
}
const response = await fetch(url);

if (response && response.status === 404) {
return [];
}
if (!response || !response.ok) {
throw new Error("Failed to fetch results");
}

const data = await response.json();
if (!data || !data.length) {
return [];
}
return data;
}
}
37 changes: 0 additions & 37 deletions euphrosyne/assets/js/web-components/period-type-ahead.ts

This file was deleted.

5 changes: 5 additions & 0 deletions lab/api_views/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ class Meta:


class _RunObjectGroupObjectGroupSerializer(serializers.ModelSerializer):
dating = serializers.SerializerMethodField()

class Meta:
model = ObjectGroup
fields = ("label", "id", "object_count", "dating", "materials")

def get_dating(self, obj: ObjectGroup):
return obj.dating_era.label if obj.dating_era else ""


class RunObjectGroupSerializer(serializers.ModelSerializer):
objectgroup = _RunObjectGroupObjectGroupSerializer()
Expand Down
58 changes: 40 additions & 18 deletions lab/elasticsearch/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

from lab.methods.dto import method_model_to_dto
from lab.models import ObjectGroup, Project
from lab.opentheso import fetch_epoques_parent_ids_from_id
from lab.participations.models import Participation
from lab.runs.models import Run
from lab.thesauri.opentheso import (
fetch_era_parent_ids_from_id,
fetch_period_parent_ids_from_id,
)

from .documents import (
CatalogItem,
Expand All @@ -26,6 +29,15 @@ class LocationDict(TypedDict):
lon: float


class DatingDict(TypedDict, total=False):
dating_period_label: str | None
dating_period_theso_huma_num_id: str | None
dating_period_theso_huma_num_parent_ids: list[str] | None
dating_era_label: str | None
dating_era_theso_huma_num_id: str | None
dating_era_theso_huma_num_parent_ids: list[str] | None


def _create_leader_doc(leader: Participation):
doc = LeaderDoc(
user_first_name=leader.user.first_name,
Expand Down Expand Up @@ -55,9 +67,12 @@ def _create_project_page_data(
)
for object_group in object_groups:

dating_label: str | None = None
if object_group.dating:
dating_label = object_group.dating.label
dating_era_label: str | None = None
dating_period_label: str | None = None
if object_group.dating_period:
dating_period_label = object_group.dating_period.label
if object_group.dating_era:
dating_era_label = object_group.dating_era.label

discovery_place_label: str | None = None
if object_group.discovery_place_location:
Expand All @@ -71,8 +86,9 @@ def _create_project_page_data(
"materials": object_group.materials,
"discovery_place_label": discovery_place_label,
"collection": object_group.collection,
"dating_label": dating_label,
"inventory": object_group.inventory,
"dating_period_label": dating_period_label,
"dating_era_label": dating_era_label,
}
),
objects=list(
Expand Down Expand Up @@ -162,17 +178,25 @@ def build_object_group_catalog_document(
locations = [location_geopoint]

# Dating
dating_label: str | None = None
dating_theso_huma_num_id: str | None = None
dating_theso_huma_num_parent_ids: list[str] | None = []
if object_group.dating:
dating_label = object_group.dating.label
dating_theso_huma_num_id = object_group.dating.theso_joconde_id
dating_theso_huma_num_parent_ids = None
if dating_theso_huma_num_id:
dating_theso_huma_num_parent_ids = fetch_epoques_parent_ids_from_id(
object_group.dating.theso_joconde_id
dating_dict = {}
for field_name in ["dating_period", "dating_era"]:
fetch_parent_ids_fn = (
fetch_era_parent_ids_from_id
if field_name == "dating_era"
else fetch_period_parent_ids_from_id
)
if getattr(object_group, field_name):
theso_huma_num_parent_ids = fetch_parent_ids_fn(
getattr(object_group, field_name).concept_id
)
dating_dict = {
**dating_dict,
f"{field_name}_label": getattr(object_group, field_name).label,
f"{field_name}_theso_huma_num_id": getattr(
object_group, field_name
).concept_id,
f"{field_name}_theso_huma_num_parent_ids": theso_huma_num_parent_ids,
}
_id = f"object-{object_group.id}"
catalog_item = CatalogItem(
meta={"id": _id},
Expand All @@ -191,9 +215,7 @@ def build_object_group_catalog_document(
discovery_place_label=location_label,
discovery_place_point=location_geopoint,
discovery_place_points=locations,
dating_label=dating_label,
dating_theso_huma_num_id=dating_theso_huma_num_id,
dating_theso_huma_num_parent_ids=dating_theso_huma_num_parent_ids,
**dating_dict,
)

collections = []
Expand Down
13 changes: 9 additions & 4 deletions lab/elasticsearch/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class ObjectGroupDoc(os.InnerDoc):
discovery_place_label = os.Text()
collection = os.Keyword()
inventory = os.Keyword()
dating_label = os.Text()
dating_period_label = os.Text()
dating_era_label = os.Text()
objects = os.Object(ObjectDoc, multi=True)

def add_object(self, label: str, collection: str, inventory: str):
Expand Down Expand Up @@ -159,13 +160,17 @@ class Index:
discovery_place_point = os.GeoPoint()
collection = os.Text()
inventory_number = os.Keyword()
dating_label = os.Text()
dating_theso_huma_num_id = os.Keyword()
dating_theso_huma_num_parent_ids = os.Keyword(multi=True)
objects = os.Object(ObjectDoc, multi=True)
collections = os.Keyword(multi=True)
inventory_numbers = os.Keyword(multi=True)

dating_period_label = os.Text()
dating_period_theso_huma_num_id = os.Keyword()
dating_period_theso_huma_num_parent_ids = os.Keyword(multi=True)
dating_era_label = os.Text()
dating_era_theso_huma_num_id = os.Keyword()
dating_era_theso_huma_num_parent_ids = os.Keyword(multi=True)

def add_object(
self,
label: str,
Expand Down
14 changes: 10 additions & 4 deletions lab/elasticsearch/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ def _match_param_to_query(
return _status_query(value)
case "materials":
return _materials_query(value)
case "period_ids":
return _period_query(value)
case "dating_period_ids":
return _dating_period_query(value)
case "dating_era_ids":
return _dating_era_query(value)
case "category":
return _category_filter(value)
case "c2rmf_id":
Expand Down Expand Up @@ -192,8 +194,12 @@ def _materials_query(materials: list[str]):
return _terms_query("materials", materials)


def _period_query(dating_ids: list[str]):
return _terms_query("dating_theso_huma_num_parent_ids", dating_ids)
def _dating_era_query(dating_ids: list[str]):
return _terms_query("dating_era_theso_huma_num_parent_ids", dating_ids)


def _dating_period_query(dating_ids: list[str]):
return _terms_query("dating_period_theso_huma_num_parent_ids", dating_ids)


def _category_filter(category: Literal["project", "object"]):
Expand Down
Loading

0 comments on commit af991ea

Please sign in to comment.