unified fachgebiete and subject_classifications. Added subject classifications to dfg detail project page
This commit is contained in:
@@ -170,25 +170,6 @@ class DfgProjectPublicationSummary(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
class DfgProjectSubjectArea(BaseModel):
|
||||
"""A single subject-area classification entry (DFG 'Fachsystematik')."""
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
fach: str | None = None
|
||||
fachgebiet: str | None = None
|
||||
fachkollegium: str | None = None
|
||||
wissenschaftsbereich: str | None = None
|
||||
|
||||
@classmethod
|
||||
def from_inner(cls, f: DFGProjectFachgebieteInner) -> "DfgProjectSubjectArea":
|
||||
return cls(
|
||||
fach=f.fach,
|
||||
fachgebiet=f.fachgebiet,
|
||||
fachkollegium=f.fachkollegium,
|
||||
wissenschaftsbereich=f.wissenschaftsbereich,
|
||||
)
|
||||
|
||||
|
||||
class DfgProjectDetails(BaseModel):
|
||||
"""
|
||||
@@ -253,7 +234,7 @@ class DfgProjectDetails(BaseModel):
|
||||
|
||||
subject_area_de: str | None = Field(default=None, description="Subject area, summary text (German)")
|
||||
subject_area_en: str | None = Field(default=None, description="Subject area, summary text (English)")
|
||||
subject_classifications: list[DfgProjectSubjectArea] = Field(
|
||||
subject_classifications: list[DFGProjectFachgebieteInner] = Field(
|
||||
default_factory=list,
|
||||
description="Detailed DFG subject-area classification entries (Fachsystematik)",
|
||||
)
|
||||
@@ -319,7 +300,7 @@ class DfgProjectDetails(BaseModel):
|
||||
DfgProjectPublicationSummary.from_inner(p) for p in (project.publications or [])
|
||||
]
|
||||
subject_classifications = [
|
||||
DfgProjectSubjectArea.from_inner(f) for f in (project.fachgebiete or [])
|
||||
DFGProjectFachgebieteInner.from_dict(f) for f in (project.subject_classifications or [])
|
||||
]
|
||||
|
||||
# De-duplicated flat views, order-preserving
|
||||
@@ -383,8 +364,6 @@ class DfgProjectDetails(BaseModel):
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, obj: dict) -> "DfgProjectDetails":
|
||||
"""Convenience constructor from a raw dict (e.g. a JSON API response body)."""
|
||||
obj = cls._sanitize_raw_project_dict(obj)
|
||||
return cls.from_dfg_project(DFGProject.from_dict(obj))
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -49,7 +49,6 @@ class DFGProject(BaseModel):
|
||||
equipment_group: StrictStr | None = None
|
||||
equipment_name: StrictStr | None = None
|
||||
external_link: StrictStr | None = None
|
||||
fachgebiete: list[DFGProjectFachgebieteInner] | None = None
|
||||
fetched_at: datetime | None = None
|
||||
final_report_abstract_de: StrictStr | None = None
|
||||
final_report_abstract_en: StrictStr | None = None
|
||||
@@ -81,7 +80,7 @@ class DFGProject(BaseModel):
|
||||
publications: list[DFGProjectPublicationsInner] | None = None
|
||||
subject_area_de: StrictStr | None = None
|
||||
subject_area_en: StrictStr | None = None
|
||||
subject_classifications: DFGProjectFachgebieteInner | None = None
|
||||
subject_classifications: list[DFGProjectFachgebieteInner] | None = None
|
||||
subproject_order: list[StrictStr] | None = None
|
||||
subprojects: list[DFGProjectSubprojectsInner] | None = None
|
||||
template_type: StrictStr | None = None
|
||||
@@ -90,7 +89,7 @@ class DFGProject(BaseModel):
|
||||
website_html_de: StrictStr | None = None
|
||||
website_html_en: StrictStr | None = None
|
||||
topics: list[Topic] | None = None
|
||||
__properties: ClassVar[list[str]] = ["abstract_de", "abstract_en", "equipment_group", "equipment_name", "external_link", "fachgebiete", "fetched_at", "final_report_abstract_de", "final_report_abstract_en", "funding_end", "funding_period_de", "funding_period_en", "funding_start", "funding_type_de", "funding_type_en", "funding_type_key", "funding_type_sort", "id", "institution_roles_order", "institutions", "international_relation_de", "international_relation_en", "is_transfer_project", "link", "page_url", "parent_project_id", "parent_title_de", "parent_title_en", "person_roles_order", "persons", "project", "project_id", "project_identifier_de", "project_identifier_en", "publications", "subject_area_de", "subject_area_en", "subject_classifications", "subproject_order", "subprojects", "template_type", "title_de", "title_en", "website_html_de", "website_html_en"]
|
||||
__properties: ClassVar[list[str]] = ["abstract_de", "abstract_en", "equipment_group", "equipment_name", "external_link","fetched_at", "final_report_abstract_de", "final_report_abstract_en", "funding_end", "funding_period_de", "funding_period_en", "funding_start", "funding_type_de", "funding_type_en", "funding_type_key", "funding_type_sort", "id", "institution_roles_order", "institutions", "international_relation_de", "international_relation_en", "is_transfer_project", "link", "page_url", "parent_project_id", "parent_title_de", "parent_title_en", "person_roles_order", "persons", "project", "project_id", "project_identifier_de", "project_identifier_en", "publications", "subject_area_de", "subject_area_en", "subject_classifications", "subproject_order", "subprojects", "template_type", "title_de", "title_en", "website_html_de", "website_html_en"]
|
||||
|
||||
model_config = ConfigDict(
|
||||
validate_by_name=True,
|
||||
@@ -131,13 +130,6 @@ class DFGProject(BaseModel):
|
||||
exclude=excluded_fields,
|
||||
exclude_none=True,
|
||||
)
|
||||
# override the default output from pydantic by calling `to_dict()` of each item in fachgebiete (list)
|
||||
_items = []
|
||||
if self.fachgebiete:
|
||||
for _item_fachgebiete in self.fachgebiete:
|
||||
if _item_fachgebiete:
|
||||
_items.append(_item_fachgebiete.to_dict())
|
||||
_dict['fachgebiete'] = _items
|
||||
# override the default output from pydantic by calling `to_dict()` of each item in institutions (list)
|
||||
_items = []
|
||||
if self.institutions:
|
||||
@@ -164,7 +156,7 @@ class DFGProject(BaseModel):
|
||||
_dict['publications'] = _items
|
||||
# override the default output from pydantic by calling `to_dict()` of subject_classifications
|
||||
if self.subject_classifications:
|
||||
_dict['subject_classifications'] = self.subject_classifications.to_dict()
|
||||
_dict['subject_classifications'] = [f.to_dict() for f in self.subject_classifications]
|
||||
# override the default output from pydantic by calling `to_dict()` of each item in subprojects (list)
|
||||
_items = []
|
||||
if self.subprojects:
|
||||
@@ -191,14 +183,12 @@ class DFGProject(BaseModel):
|
||||
key=lambda t: t.get("rank") if t.get("rank") is not None else float("inf"),
|
||||
)
|
||||
topics = [cls._topic_from_match(t) for t in sorted_topics_raw]
|
||||
|
||||
_obj = cls.model_validate({
|
||||
"abstract_de": obj.get("abstract_de"),
|
||||
"abstract_en": obj.get("abstract_en"),
|
||||
"equipment_group": obj.get("equipment_group"),
|
||||
"equipment_name": obj.get("equipment_name"),
|
||||
"external_link": obj.get("external_link"),
|
||||
"fachgebiete": [DFGProjectFachgebieteInner.from_dict(_item) for _item in obj["fachgebiete"]] if obj.get("fachgebiete") is not None else None,
|
||||
"fetched_at": obj.get("fetched_at"),
|
||||
"final_report_abstract_de": obj.get("final_report_abstract_de"),
|
||||
"final_report_abstract_en": obj.get("final_report_abstract_en"),
|
||||
@@ -230,7 +220,7 @@ class DFGProject(BaseModel):
|
||||
"publications": [DFGProjectPublicationsInner.from_dict(_item) for _item in obj["publications"]] if obj.get("publications") is not None else None,
|
||||
"subject_area_de": obj.get("subject_area_de"),
|
||||
"subject_area_en": obj.get("subject_area_en"),
|
||||
"subject_classifications": DFGProjectFachgebieteInner.from_dict(obj["subject_classifications"]) if obj.get("subject_classifications") is not None else None,
|
||||
"subject_classifications": [DFGProjectFachgebieteInner.from_dict(f) for f in (obj.get("subject_classifications") or [])],
|
||||
"subproject_order": obj.get("subproject_order"),
|
||||
"subprojects": [DFGProjectSubprojectsInner.from_dict(_item) for _item in obj["subprojects"]] if obj.get("subprojects") is not None else None,
|
||||
"template_type": obj.get("template_type"),
|
||||
|
||||
@@ -654,12 +654,128 @@ async def get_dfg_project_by_id(project_id: str) -> DfgProjectDetails | None:
|
||||
)
|
||||
hits = response.get("hits", {}).get("hits", [])
|
||||
|
||||
if hits:
|
||||
hit = hits[0]
|
||||
hit["_source"]["topics"] = topic_lookup.get(project_id, [])
|
||||
details = DfgProjectDetails.from_elastic_hit(hit=hit)
|
||||
return details
|
||||
return None
|
||||
if not hits:
|
||||
return None
|
||||
|
||||
hit = hits[0]
|
||||
source = hit["_source"]
|
||||
source["topics"] = topic_lookup.get(project_id, [])
|
||||
|
||||
persons = source.get("persons", [])
|
||||
|
||||
# --- Enrich persons with institute/root info (mirrors search_dfg_projects) ---
|
||||
person_ids = {
|
||||
person["person_id"]
|
||||
for person in persons
|
||||
if isinstance(person, dict) and person.get("person_id") is not None
|
||||
}
|
||||
person_lookup = await get_person_institutes(person_ids=list(person_ids))
|
||||
|
||||
institutions: dict[str, dict] = {}
|
||||
|
||||
for person in persons:
|
||||
person_id = person.get("person_id")
|
||||
institute = person_lookup.get(person_id)
|
||||
if not institute:
|
||||
continue
|
||||
|
||||
person["root_id"] = institute.get("root_id")
|
||||
person["institute_id"] = institute.get("institute_id")
|
||||
person["root_name_de"] = institute.get("root_name_de")
|
||||
person["root_name_en"] = institute.get("root_name_en")
|
||||
person["institute_name_de"] = institute.get("institute_name_de")
|
||||
person["institute_name_en"] = institute.get("institute_name_en")
|
||||
person["address_country_de"] = institute.get("address_country_de")
|
||||
person["address_country_en"] = institute.get("address_country_en")
|
||||
|
||||
if institute.get("institute_id") is not None:
|
||||
institutions[f"institute:{institute['institute_id']}"] = {
|
||||
"institute_id": institute["institute_id"],
|
||||
"institute_name_de": institute.get("institute_name_de"),
|
||||
"institute_name_en": institute.get("institute_name_en"),
|
||||
}
|
||||
|
||||
if institute.get("root_id") is not None:
|
||||
institutions[f"root:{institute['root_id']}"] = {
|
||||
"institute_id": institute["root_id"],
|
||||
"institute_name_de": institute.get("root_name_de"),
|
||||
"institute_name_en": institute.get("root_name_en"),
|
||||
}
|
||||
|
||||
source["institutions"] = list(institutions.values())
|
||||
|
||||
return DfgProjectDetails.from_elastic_hit(hit=hit)
|
||||
|
||||
async def get_dfg_project_by_id(project_id: str) -> DfgProjectDetails | None:
|
||||
"""Fetches the details of a single DFG project based on its project ID.
|
||||
|
||||
Args:
|
||||
project_id (str): The project ID to fetch.
|
||||
|
||||
Returns:
|
||||
Optional[DfgProjectDetails]: The project details or None if not found.
|
||||
"""
|
||||
topic_lookup = await get_openalex_topics_for_dfg_projects(project_ids=[project_id])
|
||||
query = {"ids": {"values": [project_id]}}
|
||||
|
||||
response = await elasticsearch.search(
|
||||
index=ESIndex.DFG_PROJECTS,
|
||||
query=query,
|
||||
size=1,
|
||||
)
|
||||
hits = response.get("hits", {}).get("hits", [])
|
||||
|
||||
if not hits:
|
||||
return None
|
||||
|
||||
hit = hits[0]
|
||||
source = hit["_source"]
|
||||
source["topics"] = topic_lookup.get(project_id, [])
|
||||
|
||||
persons = source.get("persons", [])
|
||||
|
||||
#Enrich persons with institute/root info
|
||||
person_ids = {
|
||||
person["person_id"]
|
||||
for person in persons
|
||||
if isinstance(person, dict) and person.get("person_id") is not None
|
||||
}
|
||||
person_lookup = await get_person_institutes(person_ids=list(person_ids))
|
||||
|
||||
institutions: dict[str, dict] = {}
|
||||
|
||||
for person in persons:
|
||||
person_id = person.get("person_id")
|
||||
institute = person_lookup.get(person_id)
|
||||
if not institute:
|
||||
continue
|
||||
|
||||
person["root_id"] = institute.get("root_id")
|
||||
person["institute_id"] = institute.get("institute_id")
|
||||
person["root_name_de"] = institute.get("root_name_de")
|
||||
person["root_name_en"] = institute.get("root_name_en")
|
||||
person["institute_name_de"] = institute.get("institute_name_de")
|
||||
person["institute_name_en"] = institute.get("institute_name_en")
|
||||
person["address_country_de"] = institute.get("address_country_de")
|
||||
person["address_country_en"] = institute.get("address_country_en")
|
||||
|
||||
if institute.get("institute_id") is not None:
|
||||
institutions[f"institute:{institute['institute_id']}"] = {
|
||||
"institute_id": institute["institute_id"],
|
||||
"institute_name_de": institute.get("institute_name_de"),
|
||||
"institute_name_en": institute.get("institute_name_en"),
|
||||
}
|
||||
|
||||
if institute.get("root_id") is not None:
|
||||
institutions[f"root:{institute['root_id']}"] = {
|
||||
"institute_id": institute["root_id"],
|
||||
"institute_name_de": institute.get("root_name_de"),
|
||||
"institute_name_en": institute.get("root_name_en"),
|
||||
}
|
||||
|
||||
source["institutions"] = list(institutions.values())
|
||||
|
||||
return DfgProjectDetails.from_elastic_hit(hit=hit)
|
||||
|
||||
#BUND
|
||||
|
||||
|
||||
Reference in New Issue
Block a user