fix: preserve imported publication enrichment during crawls

This commit is contained in:
codex committed 2026-10-05 11:57:44 +03:00
1 parent 05a62d18e6
commit 72afb31049
7 files changed
+160 -22

No files matched your search

+53 -9
View File
@@ -2,7 +2,8 @@ import gzip
import hashlib
import json
import re
import time
import time
import unicodedata
from datetime import datetime, timezone
import requests
@@ -398,8 +399,11 @@ def _employee_publications_table_exists(db: Session) -> bool:
return inspect(db.connection()).has_table(EmployeePublication.__tablename__)
def _sync_employee_publications(db: Session, employee: Employee, parsed: dict) -> None:
publications = _publication_payloads(parsed)
def _sync_employee_publications(db: Session, employee: Employee, parsed: dict) -> None:
publications = _publication_payloads(parsed)
imported = [item for item in db.scalars(select(EmployeePublication).where(
EmployeePublication.employee_id == employee.id
)).all() if _csv_enrichment(item) is not None]
seen_hashes = set()
for publication in publications:
source_hash = _publication_hash(publication)
@@ -420,8 +424,10 @@ def _sync_employee_publications(db: Session, employee: Employee, parsed: dict) -
EmployeePublication.source_hash == source_hash,
)
)
if not existing:
existing = EmployeePublication(employee_id=employee.id, source_hash=source_hash, title=_publication_title(publication))
if not existing:
existing = _match_imported_publication(imported, publication)
if not existing:
existing = EmployeePublication(employee_id=employee.id, source_hash=source_hash, title=_publication_title(publication))
db.add(existing)
_apply_publication(existing, publication, source_hash)
@@ -432,8 +438,39 @@ def _sync_employee_publications(db: Session, employee: Employee, parsed: dict) -
EmployeePublication.source_hash.not_in(seen_hashes),
)
).all()
for item in stale:
db.delete(item)
for item in stale:
if _csv_enrichment(item) is None:
db.delete(item)
def _csv_enrichment(publication: EmployeePublication) -> dict | None:
raw = publication.raw_data
metadata = raw.get("csv_enrichment") if isinstance(raw, dict) else None
return metadata if isinstance(metadata, dict) else None
def _match_imported_publication(imported: list[EmployeePublication], publication: dict) -> EmployeePublication | None:
def doi(value):
return re.sub(r"^(?:https?://(?:dx\.)?doi\.org/|doi:\s*)", "", (value or "").strip(), flags=re.I).lower()
def title(value):
return " ".join(unicodedata.normalize("NFKC", value or "").casefold().replace("ё", "е").split())
identifier = doi(publication.get("doi_url"))
year = _int_or_none(publication.get("year"))
name = title(publication.get("title") or publication.get("text"))
matches = [item for item in imported if
(identifier and doi(item.doi_url) == identifier) or
(name and title(item.title) == name and item.year in (None, year))]
if len(matches) > 1:
raise ValueError("Ambiguous imported publication; synchronization rolled back")
if not matches:
return None
match = matches[0]
if ((identifier and doi(match.doi_url) and identifier != doi(match.doi_url)) or
(year and match.year and year != match.year)):
raise ValueError("Conflicting imported publication; synchronization rolled back")
return match
def _publication_payloads(parsed: dict) -> list[dict]:
@@ -447,7 +484,9 @@ def _publication_payloads(parsed: dict) -> list[dict]:
return publications
def _apply_publication(target: EmployeePublication, publication: dict, source_hash: str) -> None:
def _apply_publication(target: EmployeePublication, publication: dict, source_hash: str) -> None:
enrichment = _csv_enrichment(target)
preserved = {field: getattr(target, field) for field in ("title", "year", "doi_url")}
target.publication_id = _clean_optional(publication.get("publication_id") or publication.get("id"))
target.title = _publication_title(publication)
target.year = _int_or_none(publication.get("year"))
@@ -462,7 +501,12 @@ def _apply_publication(target: EmployeePublication, publication: dict, source_ha
target.annotation = publication.get("annotation") if isinstance(publication.get("annotation"), dict) else None
target.description = publication.get("description") if isinstance(publication.get("description"), dict) else None
target.authors = publication.get("authors") if isinstance(publication.get("authors"), list) else None
target.raw_data = publication.get("raw_data") if isinstance(publication.get("raw_data"), dict) else publication
target.raw_data = publication.get("raw_data") if isinstance(publication.get("raw_data"), dict) else publication
if enrichment is not None:
target.raw_data = {**target.raw_data, "csv_enrichment": enrichment}
for field in ("title", "year", "doi_url"):
if not getattr(target, field) or (field == "title" and not (publication.get("title") or publication.get("text"))):
setattr(target, field, preserved[field])
target.source_hash = source_hash
+3 -3
View File
@@ -1,3 +1,3 @@
APP_VERSION = "0.10.0"
FRONTEND_VERSION = "0.10.0"
BACKEND_VERSION = "0.10.0"
APP_VERSION = "0.10.1"
FRONTEND_VERSION = "0.10.1"
BACKEND_VERSION = "0.10.1"